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

92 lines
2.7 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import maya.cmds as cmds
from Core import GetMeshes
def duplicate_target(target, index):
"""
复制目标并按网格排列
参数:
target (str): 目标名称
index (int): 目标索引,用于确定位置
"""
# 获取头部模型
head = GetMeshes(m=0)
# 创建根目标组
root_targets_grp = "root_targets_grp"
if not cmds.objExists(root_targets_grp):
cmds.group(empty=True, name=root_targets_grp)
# 处理每个网格
for i in range(9):
# 获取部署网格名称
meshe_deploy = GetMeshes(i=i)
# 获取混合变形节点名称
blend_shape = f"{meshe_deploy}_blendShapes"
if not cmds.objExists(blend_shape):
continue
# 获取权重属性
attr_weight = f"{blend_shape}.weight"
# 获取权重数量
count = cmds.getAttr(attr_weight, size=True)
# 获取当前混合变形列表
current_blend_shape_list = cmds.listAttr(attr_weight, multi=True)
# 检查目标是否在列表中
if target not in current_blend_shape_list or count == 0:
continue
# 获取网格和目标名称
meshe = GetMeshes(m=i)
meshe_target = f"{meshe_deploy}_{target}"
# 复制网格
cmds.duplicate(meshe, returnRootsOnly=True, name=meshe_target)
# 创建目标组
target_grp = f"{target}_grp"
if not cmds.objExists(target_grp):
cmds.group(empty=True, name=target_grp)
cmds.parent(target_grp, root_targets_grp)
# 将目标放入组中
cmds.parent(meshe_target, target_grp)
# 解锁变换属性
for attr in ['tx', 'ty', 'tz', 'rx', 'ry', 'rz', 'sx', 'sy', 'sz']:
cmds.setAttr(f"{meshe_target}.{attr}", lock=False)
# 获取当前轴向
axis = cmds.upAxis(query=True, axis=True)
# 计算边界框
pos = cmds.polyEvaluate(head, boundingBox=True)
# 计算距离
distance_x = pos[1] - pos[0]
if axis == "y":
distance_u = pos[3] - pos[2]
else: # axis == "z"
distance_u = pos[5] - pos[4]
# 计算行列位置
row = index % 30
column = index // 30
# 计算移动距离
move_x = (row + 1) * distance_x
move_y = column * distance_u * -1
# 设置位置
cmds.setAttr(f"{meshe_target}.tx", move_x)
if axis == "y":
cmds.setAttr(f"{meshe_target}.ty", move_y)
else: # axis == "z"
cmds.setAttr(f"{meshe_target}.tz", move_y)