MetaFusion/scripts/utils/UpdateCtrl.py
2025-02-07 05:10:30 +08:00

68 lines
1.8 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import maya.cmds as cmds
from Core import ReadJson, WriteJson
def update_ctrl(json_file, index):
"""
更新控制器的属性到JSON文件
Args:
json_file (str): JSON文件路径
index (int): 对象索引
"""
# 获取当前选择的对象
sel_list = cmds.ls(selection=True)
# 读取JSON文件中的对象列表
object_list = ReadJson(f=json_file, t="object")
# 检查JSON对象数量是否足够
if len(object_list) < 61:
raise RuntimeError("Insufficient number of JSON file objects.")
return
# 定义需要检查的属性列表
attrs = [
"translateX", "translateY", "translateZ",
"rotateX", "rotateY", "rotateZ"
]
# 初始化数据字典
data = "{}"
# 处理每个选中的对象
for sel in sel_list:
# 获取对象的可键控属性
attributes = cmds.listAttr(sel, keyable=True)
# 检查每个目标属性
for attr in attrs:
attribute = f"{sel}.{attr}"
if attr in attributes:
# 获取属性值
value = cmds.getAttr(attribute)
# 如果值超过阈值,记录到数据中
if value > 0.001 or value < -0.001:
data = WriteJson(
d=data,
k=attribute,
t="double",
value=value
)
# 更新对象列表中的数据
object_list[index] = data
# 将更新后的数据写回JSON文件
WriteJson(
of=json_file,
sf=json_file,
t="object",
value=object_list
)
# 如果直接运行此脚本
if __name__ == '__main__':
pass