import os
import json
current_dir = ‘.’ # 当前目录,可以替换成你的目标路径
for item in os.listdir(current_dir):
item_path = os.path.join(current_dir, item)
if not os.path.isdir(item_path):
continue
# 处理 .json -> .moc3 的转换
main_json = os.path.join(item_path, f"{item}.json")
if os.path.exists(main_json):
with open(main_json, 'r', encoding='utf-8') as f1:
data = json.load(f1)
# 写入 moc3 文件
moc3_path = os.path.join(item_path, f"{item}.moc3")
with open(moc3_path, 'wb') as f2:
f2.write(bytes(data['_bytes']))
print(f"{item}.moc3 文件已提取完成!")
# 处理 model3.json 的更新
model_json = os.path.join(item_path, f"{item}.model3.json")
if os.path.exists(model_json):
with open(model_json, 'r', encoding='utf-8') as f3:
model_data = json.load(f3)
# 初始化数据结构
file_refs = model_data.setdefault("FileReferences", {})
expressions= file_refs.setdefault("Expressions", [])
motions = file_refs.setdefault("Motions", {}) # 确保是字典类型
# 扫描 motions 目录
motions_dir = os.path.join(item_path, "motions")
if os.path.exists(motions_dir) and os.path.isdir(motions_dir):
for motion_file in os.listdir(motions_dir):
if motion_file.endswith('.motion3.json'):
motion_name = motion_file.replace('.motion3.json', '')
motion_path = os.path.join("motions", motion_file).replace('\\', '/')
# 如果动作不存在则添加(使用setdefault)
motions.setdefault(motion_name, [{"File": motion_path}])
#扫描expressions目录
expression_dir = os.path.join(item_path, "expressions")
if os.path.exists(expression_dir) and os.path.isdir(expression_dir):
for exp_file in os.listdir(expression_dir):
if exp_file.endswith('.exp3.json'):
exp_name = exp_file.replace('.exp3.json', '')
exp_path = os.path.join("expressions", exp_file).replace('\\', '/')
expressions.append({"Name": exp_name,"File": exp_path })
# 写回更新后的数据
with open(model_json, 'w', encoding='utf-8') as f4:
json.dump(model_data, f4, ensure_ascii=False, indent=2)
print(“所有操作已完成!”)
python脚本,放在live2d目录的上级,子文件夹名要和里面的xxxx.json名字一样,跑完会生成moc3,并修改model3.json