MetaFusion/scripts/Reference/SGImportSkinCluster.py

55 lines
1.7 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
创建日期: 2023/08/08
"""
import os
import maya.cmds as cmds
def sg_import_skin_cluster():
"""
导入蒙皮权重数据
支持两种模式
1. 选中单个模型时通过文件对话框选择单个权重文件导入
2. 未选中或选中多个模型时可以选择多个权重文件根据文件名自动匹配模型
"""
# 获取当前选中的物体
selection = cmds.ls(sl=True)
# 设置文件过滤器
basic_filter = "Maya Skin (*.skin)"
# 打开文件选择对话框
skin_files = cmds.fileDialog2(fileFilter=basic_filter,
fileMode=4, # 允许选择多个文件
dialogStyle=1)
# 如果用户取消了选择,直接返回
if not skin_files:
return
# 处理单个选中物体的情况
if len(selection) == 1:
cmds.SGSkinCluster(selection[0], skin_files[0], if_=True)
else:
# 处理多个文件的情况
for skin_file in skin_files:
# 从文件路径中提取模型名称
file_name = os.path.basename(skin_file)
mesh_name = os.path.splitext(file_name)[0]
# 检查模型是否存在
if cmds.objExists(mesh_name):
# 检查是否已经存在蒙皮变形器
skin_cluster = cmds.findRelatedSkinCluster(mesh_name)
if not skin_cluster:
# 导入权重数据
cmds.SGSkinCluster(mesh_name, skin_file, if_=True)
# 如果直接运行此脚本
if __name__ == '__main__':
sg_import_skin_cluster()