MetaFusion/scripts/Reference/SGRefreshGeoLineEdit.py
2025-01-17 02:30:36 +08:00

95 lines
2.9 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
版权所有: 深圳时光科技有限公司
联系方式: q.100@qq.com
创建日期: 2023/08/08
"""
import maya.cmds as cmds
def sg_meshes_list():
"""
获取网格列表
Returns:
list: 网格名称列表
"""
meshes = []
edition = cmds.SGDescriptor(ed=True)
if edition >= 2:
# 获取LOD0的网格索引
mesh_indices = cmds.SGGetMeshes(lod=0)
for index in mesh_indices:
meshes.append(cmds.SGGetMeshes(i=index))
else:
# 获取所有网格
meshes = cmds.SGGetMeshes()
return meshes
def sg_set_mesh_line_edit(index, mesh):
"""
设置网格选项变量
Args:
index (int): 网格索引
mesh (str): 网格名称
"""
meshes = cmds.SGGetMeshes()
if mesh:
# 设置选项变量
cmds.optionVar(stringValue=[meshes[index], mesh])
else:
# 移除选项变量
cmds.optionVar(remove=meshes[index])
def sg_refresh_geo_line_edit():
"""
刷新几何体行编辑器
"""
meshes = sg_meshes_list()
for i, mesh_name in enumerate(meshes):
if cmds.optionVar(exists=mesh_name):
# 如果存在选项变量
mesh = cmds.optionVar(query=mesh_name)
if cmds.objExists(mesh):
# 如果对象存在,设置网格
if mesh_name == "body_lod0_mesh":
cmds.SGSetMeshes(m=50, value=mesh)
else:
cmds.SGSetMeshes(m=i, value=mesh)
else:
# 如果对象不存在,尝试通过名称模式查找
selection = cmds.ls(f"*{mesh_name}*", type="transform")
if selection:
for sel in selection:
if cmds.objectType(sel) == "transform":
if mesh_name == "body_lod0_mesh":
cmds.SGSetMeshes(m=50, value=sel)
else:
cmds.SGSetMeshes(m=i, value=sel)
break
else:
# 如果找不到匹配的对象,清除设置
cmds.SGSetMeshes(m=i, value="")
else:
# 如果不存在选项变量,尝试通过名称模式查找
selection = cmds.ls(f"*{mesh_name}*", type="transform")
if selection:
for sel in selection:
if cmds.objectType(sel) == "transform":
if mesh_name == "body_lod0_mesh":
cmds.SGSetMeshes(m=50, value=sel)
else:
cmds.SGSetMeshes(m=i, value=sel)
break
else:
# 如果找不到匹配的对象,清除设置
cmds.SGSetMeshes(m=i, value="")
# 如果直接运行此脚本
if __name__ == '__main__':
pass