Check if object exists in world before using parent command

1,604 views
Skip to first unread message

likage

unread,
Feb 23, 2017, 3:42:20 PM2/23/17
to Python Programming for Autodesk Maya
This is my outliner, eg. '|hie1|cube1' and 'cube2'

If I select cube1 and did:
cmds.parent("|hie1|cube1", world=True)


This will unparent my cube1 out of hie1 and into 'world' level? Sorry, can't think of a better term.

But if I did the same thing to cube2:
cmds.parent("cube2", world=True)

I will get an error:
# Warning: Object, 'cube2', skipped. It is already a child of the parent, 'world'. #
# Error: Maya command error
# Traceback (most recent call last):
#   File "<maya console>", line 2, in <module>
# RuntimeError: Maya command error #


Even as I used `try...except...` it does not stops my tool function but you can explicitly see in the status line that there is a quick flash of red color before it disappears.

Is there any command that enables me to check if the object is already in 'world' or better ways to get around this problem?

Justin Israel

unread,
Feb 23, 2017, 3:51:06 PM2/23/17
to python_in...@googlegroups.com
You could check if it has a parent first:

    hasParent = bool(cmds.listRelatives("cube2", parent=True))

Justin

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3405f3cc-b1f3-4fd5-9f41-4f091f6f5013%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages