【问题标题】:How to get the list of root parents created in a scene - Autodesk Maya / Python?如何获取在场景中创建的根父级列表 - Autodesk Maya / Python?
【发布时间】:2020-12-09 06:30:27
【问题描述】:

我对python有点陌生,我正在尝试获取一个列表,其中包含joint类型的场景中存在的所有根父级。 例如,我的场景大纲是这样的:

group1>>group2>>joint1>>joint2>>joint3

group3>>joint4>>joint5

joint16>>joint17>>joint18

在我的示例中,我想要一个遍历大纲并返回列表的脚本:

[joint1, joint4, joint16]

任何提示将不胜感激。非常感谢。

【问题讨论】:

    标签: python maya


    【解决方案1】:

    我不确定它是否有用,Haggi Krey 解决方案可以正常工作,但是 您还可以使用标志:-long from cmds.ls

    # list all the joints from the scene
    mjoints = cmds.ls(type='joint', l=True)
    # list of the top joints from chain
    output = []
    # list to optimise the loop counter
    exclusion = []
    # lets iterate joints
    for jnt in mjoints:
        # convert all hierarchy into a list
        pars = jnt.split('|')[1:]
        # lets see if our hierarchy is in the exclusion list
        # we put [1:] because maya root is represented by ''
        if not set(pars) & set(exclusion):
            # we parse the hierarchy until we reach the top joint
            # then we add it to the output
            # we add everything else to the exclusion list to avoid 
            for p in pars:
                if cmds.nodeType(p) == 'joint':
                    output.append(p)
                    exclusion+=pars
                    break
    print(output)
    

    我之所以这么说是因为没有一条路可以走。我希望这段代码的构建可以帮助你的 Python 技能。完全一样,只是找到父节点的方式不同!

    【讨论】:

    • 非常感谢您的时间和帮助。我也会试一试,让你知道^^
    • 您好Dian Lan Luu,请注意,将问题标记为已接受与获得最佳答案不同。从理论上讲,您应该将其交给 Haggi Key 并为您认为更相关的新答案投票
    • 请注意,您可能无法投票,因为您是新手,但这有点像 stackoverflow 的规则;]
    【解决方案2】:

    在您通过对象的长名称遍历层次结构之前,我使用了 DrWeeny 的想法。这个答案的不同之处在于,如果场景中有重复名称的对象,脚本不会崩溃。我的意思是,假设您有 2 个层次结构:

    group1>>joint1>>joint2>>group2>>joint3

    group3>>joint1>>joint2>>group2>>joint3

    Maya 很容易做到这一点,例如在复制顶部节点时,因此我们需要防止脚本在这种情况下崩溃。当有多个具有重复名称的对象时,如果您尝试访问对象的短名称,Maya 将崩溃(它不知道您指的是哪个!),因此我们必须始终使用它的长名称:

    import maya.cmds as cmds
    
    
    jnts = cmds.ls(type="joint", l=True)  # Collect all joints in the scene by their long names.
    output = set()  # Use a set to avoid adding the same joint.
    
    for jnt in jnts:
        pars = jnt.split("|")  # Split long name so we can traverse its hierarchy.
    
        root_jnt = None
    
        while pars:
            obj = "|".join(pars)
            del pars[-1]  # Remove last word to "traverse" up hierarchy on next loop.
    
            # If this is a joint, mark it as the new root joint.
            if obj and cmds.nodeType(obj) == "joint":
                root_jnt = obj
    
        # If a root joint was found, append it to our final list.
        if root_jnt is not None:
            output.add(root_jnt)
    
    print(list(output))
    

    在上面的层次结构上使用这个脚本会返回

    [u'|group1|joint1', u'|group3|joint1']

    【讨论】:

    • 假设它们将是双重名称有点可悲,但总有一个疯狂的科学家试图这样做......我喜欢你做你的 while 循环的 wya,我总是避免它们因为我最终循环了玛雅:S
    • 我完全同意,但它发生的频率比我关心的要多!尤其是在与小辈打交道时。
    • 非常感谢您的时间和宝贵的帮助,对于我迟到的回复感到抱歉,因为我无法在 Maya 中测试您的解决方案。像魅力一样工作。谢谢你一百万次 Haggi、DrWeeny 和 Green Cell :)
    【解决方案3】:

    我建议列出所有关节,对于每个关节,您可以检查它的父节点是否不是关节。在您的定义中,这些关节应该是您的根关节。

    【讨论】:

    • 我会试一试告诉你,非常感谢你的提示!!!
    • 非常感谢,工作就像一个魅力。 import maya.cmds as cmds allParentsList = [] myItems= cmds.ls(type='joint') for myItems 中的项目: theParent = cmds.listRelatives(item, parent=True) if cmds.nodeType(theParent) != "joint ": allParentsList.append(item) 打印 allParentsList
    • @DienLanLuu 这不起作用。想一想您有另一种对象类型破坏关节链的情况,例如:joint1>>joint2>>group1>>joint3。它将返回 joint1joint3 而不仅仅是 joint1
    • @GreenCell 是的,这是一个快速脚本来测试 haggi 建议我做什么,但现在在您和其他所有人的帮助下,我得到了它的工作。感谢一百万次:)
    【解决方案4】:

    我使用这种方法来获得联合层次结构。我已经放弃了寻找更性感的方法来做到这一点。

    myItems = cmds.ls(selection = True, type='joint') 
    theParentJnt = cmds.listRelatives(myItems, parent = True)
    jntRel = cmds.listRelatives(myItems, allDescendents = True)
    allJnt = jntRel + myItems
    

    @绿色细胞

    您的方法曾经奏效,但再也没有奏效。重新启动 Maya 2020 超过 5 次,仅显示到顶部节点关节,不再返回一个列表中的所有关节。

    【讨论】:

      猜你喜欢
      • 2019-05-10
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      相关资源
      最近更新 更多