Go to: Synopsis. Return value. Flags. Python examples.

Synopsis

setParent( [string] , [defineTemplate=string], [menu=boolean], [topLevel=boolean], [upLevel=boolean], [useTemplate=string])

Note: Strings representing object names and arguments must be separated by commas. This is not depicted in the synopsis.

setParent is undoable, queryable, and NOT editable.

This command changes the default parent to be the specified parent. Two special parents are "/" which indicates the top of the hierarchy, or ".." which indicates one level up in the hierarchy. Trying to move above the top level has no effect.

A control must be parented to a control layout. A control layout may be parented to another control layout or a window. A menu may be parented to a window or a menu bar layout. For all of these cases the setParent command (with no flags) will indicate the current default parent.

A menu item must be parented to a menu. To specify the default menu parent use the command setParent -m/menu. Note that all menu item objects created using the -sm/subMenu may also be treated as menu objects.

The default parent is ignored by any object that explicitly sets the -p/parent flag when it is created.

Return value

stringName of the parent if the parent changes. Empty string if the parent doesn't change.

In query mode, return type is based on queried flag.

Flags

defineTemplate, menu, topLevel, upLevel, useTemplate
Long name (short name) Argument types Properties
upLevel(u) boolean create
Move up one level in the hierarchy. Equivalent to use ".."
topLevel(top) boolean create
Move to the top level in the hierarchy. Equivalent to use "/"
menu(m) boolean createquery
Parent menu for menu items.
defineTemplate(dt) string create
Put a command in a mode where any other flags and args are parsed and added to the command template with the given name. They will be used as default arguments in any subsequent invocations of the command when templateName is set as the current template.
useTemplate(ut) string create
Will force the command to use a command template given by the name other than the current one.

Flag can appear in Create mode of command Flag can appear in Edit mode of command
Flag can appear in Query mode of command Flag can have multiple arguments, passed either as a tuple or a list.

Python examples

import maya.cmds as cmds

#  Create a window with a menu bar and two menu bar layouts.
#
window = cmds.window(menuBar=True, widthHeight=(300, 200) )
fileMenu = cmds.menu( label='File')
cmds.menuItem( label='Open' )
cmds.paneLayout( configuration='vertical2' )
leftMenuBarLayout = cmds.menuBarLayout()
leftMenu = cmds.menu( label='Left' )
cmds.menuItem( label='One' )
cmds.setParent( '..' )
cmds.menuBarLayout()
cmds.menu( label='Right' )
rightSubMenu = cmds.menuItem(subMenu=True, label='Colors' )
cmds.setParent( '..' )
cmds.showWindow( window )
#  Add item to the "File" menu.
#
cmds.setParent( fileMenu, menu=True )
cmds.menuItem( label='Save' )
#  Add item to the "Left" menu, explicitly ignore default parent
#    by setting -p/parent flag.
#
cmds.menuItem( parent=leftMenu, label='Two' )
#  Add more items to the "File" menu because it is still the
#    default parent.
#
cmds.menuItem( divider=True )
cmds.menuItem( label='Close' )
#  Add another menu to the left menu bar layout.
#
cmds.setParent( leftMenuBarLayout )
cmds.menu( label='Middle' )
cmds.menuItem( label='Three' )
#  Add items to right sub menu.
#
cmds.setParent( rightSubMenu, menu=True )
cmds.menuItem( label='Red' )
cmds.menuItem( label='Blue' )
cmds.menuItem( label='Green' )