python - Display hierarchy of selected object only -


i creating ui user selects object, ui display hierarchy of selected object. similar outliner unable find documentation/ similar results trying obtain. , btw, coding using python...

even so, possible in first place?

allow me provide simple example below: if selects testctrl, displays testctrl, loc , jnt without showing parent (grp 01)

eg. grp 01 --> testctrl --> loc --> jnt

import maya.cmds cmds  def customoutliner(): if cmds.ls( sl=true ):     # create window/ui custom oultiner      newoutliner = cmds.window(title="outliner (custom)", iconname="outliner*", widthheight=(250,100))     frame = cmds.framelayout(labelvisible = false)     customoutliner = cmds.outlinereditor()      # create selection connection network; selects 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 selected. custom outliner not created.') 

make window:

you want use treeview command (documentation) this. i'm placing in formlayout convenience.

from maya import cmds 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) 

populate tree view:

for this, we'll use recursive function can build hierarchy nested listrelatives calls (documentation). start result of old faithful ls -sl:

def populatetreeview(control, parent, parentname, counter):     # list children of parent node     children = cmds.listrelatives(parent, children=true, path=true) or []      # loop on children     child in children:         # childname string after last '|'         childname = child.rsplit('|')[-1]          # increment number of spaces         counter[childname] += 1         # make new string counter spaces following name         childname = '{0} {1}'.format(childname, ' '*counter[childname])          # create leaf in treeview, named childname, parent parentname         cmds.treeview(control, e=true, additem=(childname, parentname))          # call function again, child parent. recursion!         populatetreeview(control, child, childname, counter)   # find selected object selection = cmds.ls(sl=true)[0]  # create root node in treeview cmds.treeview(control, e=true, additem=(selection, ''), hidebuttons=true)  # enter recursive function populatetreeview(control, selection, '', defaultdict(int)) 

comparison of window outliner.

i've replaced spaces x can see what's happening. running code use spaces though:

enter image description here


you'll want read on documentation improve on this, should great starting point. if want live connection selection, make scriptjob track that, , sure clear treeview before repopulating.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

What is the difference between data design and data model(ERD) -

ios - Can NSManagedObject conform to NSCoding -