将对象放在Python中的图层中 - Maya

时间:2014-12-11 09:03:41

标签: python button layer maya

请原谅我,我并不擅长Python,所以请尽量放轻松。 我已经有了一个基本程序,可以在Maya的装备上为控制形状创建NURBS圆圈,并且我将每个区域(头部,躯干等)分成不同的层。 首先,如何更改图层颜色。看着脚本编辑器,这应该可以工作,但它没有。可能只是菜鸟的语法错误: cmds.createDisplayLayer(name =' Head_Control_Shapes',cmds.setAttr(' Head_Control_Shapes')。color = 4)

我的主要问题是,一旦创建了这个(这是在脚本的启动时运行),我如何在其中放置一个对象? 这是相关的代码片段。 提前谢谢

import maya.cmds as cmds
if cmds.window("RyanWindow", exists = True):
    cmds.deleteUI("RyanWindow")

myWindow = cmds.window(t="Ryan Window",w = 300, h = 300)
form = cmds.formLayout()

cmds.createDisplayLayer (name = 'Head_Control_Shapes', cmds.setAttr ('Head_Control_Shapes').color=4)
cmds.createDisplayLayer (name = 'Torso_Control_Shapes', cmds.setAttr ('Torso_Control_Shapes').color=12)
cmds.createDisplayLayer (name = 'Arms_Control_Shapes', cmds.setAttr ('Arms_Control_Shapes').color=13)
cmds.createDisplayLayer (name = 'Legs_Control_Shapes', cmds.setAttr ('Legs_Control_Shapes').color=31)
cmds.createDisplayLayer (name = 'Other_Control_Shapes', cmds.setAttr ('Other_Control_Shapes').color=2)


cmds.columnLayout(adj = True)
cmds.text("                         Custom Control Shape Creator v1                         ")
cmds.separator(height = 15, style='double' )
cmds.separator(height = 15, style='double' )
cmds.setParent( '..' )

tabs = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5)
cmds.formLayout( form, edit=True, attachForm=((tabs, 'top', 40), (tabs, 'left', 0), (tabs, 'bottom', 0), (tabs, 'right', 0)) )

tab1 = cmds.rowColumnLayout(numberOfColumns=3)
cmds.button(label='Circle', command = "makeCircle()")
def makeCircle():
cmds.circle( nr=(0, 90, 1), c=(0, 0, 0) )
cmds.button(label='Square', command = "makeSquare()")
def makeSquare():
cmds.circle( nr=(0, 90, 1), c=(0, 0, 0) )
cmds.button(label='Oval')
cmds.setParent( '..' )

2 个答案:

答案 0 :(得分:3)

我稍微重构了你的代码以使其工作:(我将在代码下面添加一些指针)

import maya.cmds as cmds
if cmds.window("RyanWindow", exists=True):
    cmds.deleteUI("RyanWindow")

myWindow = cmds.window(t="Ryan Window", w=300, h=300)
form = cmds.formLayout()

print cmds.createDisplayLayer (name='Head_Control_Shapes', empty=True)
cmds.createDisplayLayer (name='Torso_Control_Shapes', empty=True)
cmds.createDisplayLayer (name='Arms_Control_Shapes', empty=True)
cmds.createDisplayLayer (name='Legs_Control_Shapes', empty=True)
cmds.createDisplayLayer (name='Other_Control_Shapes', empty=True)

cmds.setAttr ('Head_Control_Shapes.color', 4)
cmds.setAttr ('Torso_Control_Shapes.color', 12)
cmds.setAttr ('Arms_Control_Shapes.color', 13)
cmds.setAttr ('Legs_Control_Shapes.color', 31)
cmds.setAttr ('Other_Control_Shapes.color', 2)

cmds.columnLayout(adj=True)
cmds.text("                         Custom Control Shape Creator v1                         ")
cmds.separator(height = 15, style='double' )
cmds.separator(height = 15, style='double' )
cmds.setParent( '..' )

tabs = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5)
cmds.formLayout( form, edit=True, attachForm=((tabs, 'top', 40), (tabs, 'left', 0), (tabs, 'bottom', 0), (tabs, 'right', 0)) )

tab1 = cmds.rowColumnLayout(numberOfColumns=3)
cmds.button(label='Circle', command="makeCircle()")
cmds.button(label='Square', command="makeSquare()")
cmds.button(label='Oval')
cmds.setParent( '..' )


def makeCircle():
    cmds.circle( nr=(0, 90, 1), c=(0, 0, 0) )


def makeSquare():
    cmds.circle( nr=(0, 90, 1), c=(0, 0, 0) )


cmds.showWindow(myWindow)

你使用cmds.setAttr的方式是错误的。 setAttr不应该是cmds.createDisplayLayers电话的一部分。这是一个单独的命令。这就是我将这两个分开的原因:

cmds.createDisplayLayer (name='Torso_Control_Shapes', empty=True)
cmds.setAttr ('Head_Control_Shapes.color', 4)

应设置empty=True标志。否则,在运行脚本之前可能选择的任何对象都将添加到新创建的层中。

要在图层的创建过程中将对象添加到图层,您只需预先选择对象并使用cmds.createDisplayLayers创建图层,而无需设置empty标记。如果您设置了noRecurse=True标记,则只会添加所选对象,否则所有它的后代都会被添加。

要在创建图层后添加对象,您将使用editDisplayLayerMembers方法。你会这样做:

cmds.editDisplayLayerMembers('Torso_Control_Shapes', object1, object2)
# where object1 and object2 are strings containing the names of your objects

注意:我还删除了方法调用中'='符号周围的间距。 Pep8建议不要在方法调用中使用参数赋值。

另外,请查看文档以获取更多示例和选项: http://download.autodesk.com/us/maya/2010help/CommandsPython/editDisplayLayerMembers.html http://download.autodesk.com/us/maya/2010help/CommandsPython/createDisplayLayer.html

答案 1 :(得分:0)

mySelection = cmds.ls(sl = True)#create selection variable

cmds.createDisplayLayer(name =" hidden_​​item",number = 1,empty = True)#create empty display layer

cmds.editDisplayLayerMembers(' hidden_​​item',mySelection)#将我的选择存储到显示层

cmds.setAttr(" hidden_​​item.visibility",0)#更改显示图层的attr