62 lines
2.4 KiB
Python
62 lines
2.4 KiB
Python
|
#!/usr/bin/env python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
"""
|
||
|
版权所有: 清泉时代科技有限公司
|
||
|
联系方式: q.100@qq.com
|
||
|
创建日期: 2024/09/16
|
||
|
"""
|
||
|
|
||
|
import maya.cmds as cmds
|
||
|
|
||
|
def sg_add_blend_shape(target_name):
|
||
|
"""
|
||
|
为选中的模型添加混合变形目标
|
||
|
|
||
|
参数:
|
||
|
target_name (str): 要添加的混合变形目标名称
|
||
|
"""
|
||
|
# 获取当前选中的对象
|
||
|
selection = cmds.ls(sl=True)
|
||
|
|
||
|
for sel in selection:
|
||
|
# 遍历50个可能的网格体
|
||
|
for j in range(50):
|
||
|
mesh = cmds.SGGetMeshes(m=j) # 假设SGGetMeshes是一个自定义命令
|
||
|
|
||
|
if sel == mesh:
|
||
|
if target_name:
|
||
|
# 获取混合变形节点名称
|
||
|
blend_shape = cmds.SGGetBlendShape(mesh) # 假设SGGetBlendShape是一个自定义命令
|
||
|
|
||
|
# 如果混合变形节点不存在,创建一个新的
|
||
|
if not cmds.objExists(blend_shape):
|
||
|
blend_shape = cmds.SGGetMeshes(i=j) + "_blendShapes"
|
||
|
cmds.blendShape(mesh, automatic=True, name=blend_shape)
|
||
|
|
||
|
# 获取权重属性路径
|
||
|
attr_weight = f"{blend_shape}.weight"
|
||
|
|
||
|
# 获取当前混合变形目标列表
|
||
|
current_blend_shape_list = cmds.listAttr(attr_weight, multi=True) or []
|
||
|
|
||
|
# 如果目标名称不在当前列表中,添加新的混合变形目标
|
||
|
if target_name not in current_blend_shape_list:
|
||
|
# 复制原始模型作为目标
|
||
|
cmds.duplicate(mesh, returnRootsOnly=True, name=target_name)
|
||
|
|
||
|
# 获取目标索引并添加混合变形
|
||
|
target_index = cmds.getAttr(attr_weight, size=True)
|
||
|
cmds.blendShape(
|
||
|
blend_shape,
|
||
|
edit=True,
|
||
|
tangentSpace=True,
|
||
|
target=(mesh, target_index, target_name, 1.0),
|
||
|
weight=(target_index, 0)
|
||
|
)
|
||
|
|
||
|
# 删除临时创建的目标模型
|
||
|
cmds.delete(target_name)
|
||
|
|
||
|
# 恢复原始选择
|
||
|
cmds.select(selection, replace=True)
|