MetaFusion/scripts/Reference/SGUpdateCtrl.py

73 lines
1.9 KiB
Python
Raw Normal View History

2025-01-17 02:30:36 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
版权所有: 深圳时光科技有限公司
联系方式: q.100@qq.com
创建日期: 2024/10/28
"""
import maya.cmds as cmds
def sg_update_ctrl(json_file, index):
"""
更新控制器的属性到JSON文件
Args:
json_file (str): JSON文件路径
index (int): 对象索引
"""
# 获取当前选择的对象
sel_list = cmds.ls(selection=True)
# 读取JSON文件中的对象列表
object_list = cmds.SGReadJson(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 = cmds.SGWriteJson(
d=data,
k=attribute,
t="double",
value=value
)
# 更新对象列表中的数据
object_list[index] = data
# 将更新后的数据写回JSON文件
cmds.SGWriteJson(
of=json_file,
sf=json_file,
t="object",
value=object_list
)
# 如果直接运行此脚本
if __name__ == '__main__':
pass