PDA

View Full Version : Selecting object from nodes?


GreatFurnace
10-02-2010, 03:40 AM
How do I select the objects from a list of nodes and object? I tried using
listRelatives( parent=True ) but it returns an error when an object is part of the list.

mccollom73
10-02-2010, 06:05 AM
How do I select the objects from a list of nodes and object? I tried using
listRelatives( parent=True ) but it returns an error when an object is part of the list.

What language?

Maybe try ( parent == true )

ginodauri
10-02-2010, 06:12 AM
Try command:objectType

GreatFurnace
10-02-2010, 04:18 PM
What language?

Maybe try ( parent == true )

This would work only if my selection consists of children [nodes]. It returns an error if I have an entry that's a parent [node] (ex. camera1 ).

I was just wondering if there's a way to check if its a parent or child node. It may appear "weird" but I'm trying to fully automate the script.

Thanks!

NateH
10-02-2010, 05:38 PM
Here you go, this justs checks each item in the list, and if it's not a transform, it swaps the item for its parent in the list:

def findObjectTransforms(objectsAndShapes):
'''Return a list of transforms for the given list of objects and/or shapes'''
#copy the input array, since lists are mutable
objectsAndShapes = objectsAndShapes[:]

for i, item in enumerate(objectsAndShapes):
if cmds.objectType(item)!='transform':
objectsAndShapes[i] = cmds.listRelatives(item, parent=True)[0]

#If you need it to not have any duplicates, uncomment the next line
#objectsAndShapes = list(set(objectsAndShapes))
return objectsAndShapes

#try it out
print findObjectTransforms( cmds.ls(sl=True) )

I wasn't sure if you needed it to be a unique item list or not, so i put in the code to do that but commented it out. Note though, using list(set()) to remove duplicate items can potentially jumble the order of objects. so if your item order matters, remove the duplicates another way.

Also, if you have some special object in your list that has no parent, it will probably throw an error (Like defaultRenderGlobals or something) you could just wrap the listRelatives in a try: except block to work around that.

CGTalk Moderation
10-02-2010, 05:38 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.