仅显示所选对象的层次结构

时间:2014-03-07 04:46:53

标签: python hierarchy maya

我正在创建一个用户选择对象的UI, UI将显示其所选的层次结构 宾语。 它有点类似于大纲,但我无法做到 找到任何文件/类似的结果我 试图获得。顺便说一句,我正在使用python进行编码......

即便如此,这是否有可能在第一次做到这一点 放置?

请允许我在下面提供一个简单示例: 假如我选择testCtrl,它只会显示 testCtrl,loc和jnt没有显示Parent(Grp 01)

EG。 Grp 01 - > testCtrl - > loc - > JNT

import maya.cmds as cmds

def customOutliner():
if cmds.ls( sl=True ):
    # Create the window/UI for the custom Oultiner 
    newOutliner = cmds.window(title="Outliner (Custom)", iconName="Outliner*", widthHeight=(250,100))
    frame = cmds.frameLayout(labelVisible = False)
    customOutliner = cmds.outlinerEditor()

    # Create the selection connection network; Selects the active selection
    inputList = cmds.selectionConnection( activeList=True )
    fromEditor = cmds.selectionConnection()

    cmds.outlinerEditor( customOutliner, edit=True, mainListConnection=inputList )
    cmds.outlinerEditor( customOutliner, edit=True, selectionConnection=fromEditor )

    cmds.showWindow( newOutliner )

else:
    cmds.warning('Nothing is selected. Custom Outliner will not be created.')

1 个答案:

答案 0 :(得分:3)

制作窗口:

您希望使用treeView命令(documentation)。为方便起见,我将它放在formLayout中。

from maya import cmds
from collections import defaultdict

window = cmds.window()
layout = cmds.formLayout()

control = cmds.treeView(parent=layout)

cmds.formLayout(layout, e=True, attachForm=[(control,'top', 2),
                                            (control,'left', 2),
                                            (control,'bottom', 2),
                                            (control,'right', 2)])
cmds.showWindow(window)

填充树视图:

为此,我们将使用递归函数,以便您可以使用嵌套的listRelatives调用(documentation)构建层次结构。从旧忠实ls -sl

的结果开始
def populateTreeView(control, parent, parentname, counter):
    # list all the children of the parent node
    children = cmds.listRelatives(parent, children=True, path=True) or []

    # loop over the children
    for child in children:
        # childname is the string after the last '|'
        childname = child.rsplit('|')[-1]

        # increment the number of spaces
        counter[childname] += 1
        # make a new string with counter spaces following the name
        childname = '{0} {1}'.format(childname, ' '*counter[childname])

        # create the leaf in the treeView, named childname, parent parentname
        cmds.treeView(control, e=True, addItem=(childname, parentname))

        # call this function again, with child as the parent. recursion!
        populateTreeView(control, child, childname, counter)


# find the selected object
selection = cmds.ls(sl=True)[0]

# create the root node in the treeView
cmds.treeView(control, e=True, addItem=(selection, ''), hideButtons=True)

# enter the recursive function
populateTreeView(control, selection, '', defaultdict(int))

窗口与大纲的比较。

我已用X替换了空格,以便您可以看到正在发生的事情。运行此代码将使用空格:

enter image description here


您需要阅读文档以改进这一点,但这应该是一个很好的起点。如果您想要选择实时连接,请创建一个scriptJob来跟踪它,并确保在重新填充之前清除treeView。