如何在maya中通过python设置对象的父对象

2024-04-28 22:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python有点陌生,我尝试在maya中根据对象的名称为对象建立父对象。我在场景中有“spineA_jnt”,“spineB_jnt”,“spineC_jnt”和“spineA_jnt”,“spineB_jb”,“spineC_jb”。如何将它们全部选中,并将“spineA_jnt”父代为“spineA_jb”等等。如有任何提示,将不胜感激,谢谢。在


Tags: 对象名称场景mayajntjb陌生spinec
2条回答

看起来你可以使用你的名字,因为他们共享相同的名字,但后缀不同,所以你不需要硬编码字符串。在

下面是一个为_jnt对象建立父子关系的示例。在

import maya.cmds as cmds

# Use * as a wildcard to get all spine joints.
jnts = cmds.ls("spine*_jnt")

for obj in jnts:
    # Build name to the object we want it to parent to.
    parent_obj = obj.replace("_jnt", "_jb")

    # But skip it if it doesn't exist.
    if not cmds.objExists(parent_obj):
        continue

    # Finally parent _jnt to _jb
    cmds.parent(obj, parent_obj)

如果您想使用不同的名称,您可以随时更改“jnt”或“jb”。在

您可以使用字典:

spines = "spineA_jnt", "spineB_jnt", "spineC_jnt", "spineA_jb", "spineB_jb", "spineC_jb"
def group_spines(spines):
    spine_dict = {}
    for spine in spines:
        parts = spine.split('_')
        if parts[0] in spine_dict:
            spine_dict[parts[0]].append(spine)
        else:
            spine_dict[parts[0]]=[spine]
    return spine_dict

spine_dict = group_spines(spines)
# results of group_spines function
{'spineA': ['spineA_jnt', 'spineA_jb'],
 'spineB': ['spineB_jnt', 'spineB_jb'],
 'spineC': ['spineC_jnt', 'spineC_jb']}

final_result = dict(spine_dict.values())
# now we have parented the spine parts together 
{'spineA_jnt': 'spineA_jb',
 'spineB_jnt': 'spineB_jb',
 'spineC_jnt': 'spineC_jb'}

相关问题 更多 >