使用Maya中的滑块和Python来控制形状 - intSliderGrp

时间:2018-05-01 04:49:57

标签: python maya

我正在尝试使用以下滑块进行颜色,平移,旋转和缩放,以获取此代码中的形状。有没有办法用这些滑块控制所有形状?我似乎无法弄明白我已经尽了一切努力。我想这可能是一个功能,但任何解决方案都有帮助任何帮助将不胜感激,谢谢。

    #Importing all Maya commands to Python
import maya.cmds as cmds
from functools import partial

class CreateUI():
    def __init__(self):
        windowID = "myWindowID"

        if cmds.window(windowID, exists=True):
            cmds.deleteUI(windowID)

#-----------------------------------------------Layout of Interface 

        masterWindow = cmds.window(windowID, title="User Interface", w=500, h=800, sizeable=False, resizeToFitChildren=True)
        ShapeLayout = cmds.rowColumnLayout(parent=masterWindow, numberOfColumns=1, columnOffset=[(2,"left",0)])



#-----------------------------------------------Shape Functions       

#Functions for creating shapes        
        def mySphere(*args):
            cmds.polySphere()

        def myCube(*args):
            cmds.polyCube()

        def myCylinder(*args):
            cmds.polyCylinder()

        def myCone(*args):
            cmds.polyCone()

        def myTorus(*args):
            cmds.polyTorus()

        def myPlane(*args):
            cmds.polyPlane()

        def myDelete(*args):
            cmds.select()
            cmds.delete()

#Gets rid of any shapes in the scene so that the user doesn't have to every time the UI is launched or the sript is run
        ShapeList = cmds.ls("mySphere*", "myCube*", "myCylinder*", "myCone*", "myTorus*", "myPlane*","pSphere*", "pCube*", "pCylinder*", "pCone*", "pTorus*", "pPlane*")
        if len(ShapeList)>0:
            cmds.delete(ShapeList)

#-----------------------------------------------Shape Buttons         

#Buttons for creating shapes    
        cmds.button(label="Sphere", command=mySphere)
        cmds.button(label="Cube", command=myCube)
        cmds.button(label="Cylinder", command=myCylinder)
        cmds.button(label="Cone", command=myCone)
        cmds.button(label="Torus", command=myTorus)
        cmds.button(label="Plane", command=myPlane)
        cmds.button(label="Delete", command=myDelete)

        #COLOUR
        cmds.separator(h=20, style="none")
        cmds.colorSliderGrp('blockColour',label="Colour", hsv=(120, 1, 1))
        cmds.separator(h=20, style="none")

        #TRANSLATE
        TranslateX = cmds.intSliderGrp('TX',label="Translate X ", field=True, min=1, max=100, value=0)  
        TranslateY = cmds.intSliderGrp('TY',label="Translate Y ", field=True, min=1, max=100, value=0)
        TranslateZ = cmds.intSliderGrp('TZ',label="Translate Z ", field=True, min=1, max=100, value=0)
        cmds.separator(h=20, style="none")

        #ROTATE
        RotateX = cmds.intSliderGrp('RX',label="Rotate X ", field=True, min=1, max=100, value=0)  
        RotateY = cmds.intSliderGrp('RY',label="Rotate Y ", field=True, min=1, max=100, value=0)
        RotateZ = cmds.intSliderGrp('RZ',label="Rotate Z ", field=True, min=1, max=100, value=0)
        cmds.separator(h=20, style="none")

        #SCALE
        ScaleX = cmds.intSliderGrp('SX',label="Scale X ", field=True, min=1, max=100, value=0)  
        ScaleY = cmds.intSliderGrp('SY',label="Scale Y ", field=True, min=1, max=100, value=0)
        ScaleZ = cmds.intSliderGrp('SZ',label="Scale Z ", field=True, min=1, max=100, value=0)




        cmds.showWindow(windowID)
ui=CreateUI()        

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

对于您似乎在这里尝试的非常具体的应用程序,您还可以使用attrFieldSliderGrp控件来短路一些工作,这恰好是将数字直接从gui传递到对象属性。

要完成这项工作,您需要确保当您想要编辑对象时,所有滑块都需要知道它的名称。懒惰的方法是确保该函数仅适用于所选项目 - 尽管这可能不是您想要的。

在按钮之间传递信息有点棘手(原因详见here。基本上,您需要确保控件都知道正在处理哪个对象并对其进行操作。虽然在一般来说,对于简单的GUI来说,类是过度的,在这种情况下,类提供了一种非常简单的方法来处理它。

这是一个如何完成的简要示例:

        import maya.cmds as cmds

        class ShapeUI(object):   # remember to inherit from object in python 2 classes!
            WINDOW_NAME = "myWindowID"   # put this up here so all copies of this class share it

            def __init__(self):


                # you need to remember the name of the object you want to work on
                self.active_shape = None
                self.layout()

            def layout(self):

                if cmds.window(self.WINDOW_NAME, exists=True):
                    cmds.deleteUI(self.WINDOW_NAME)
                cmds.window(self.WINDOW_NAME, title="User Interface", resizeToFitChildren=True)
                cmds.columnLayout(adjustableColumn=True)

                cmds.button(label="Sphere", command=self.mySphere)
                cmds.button(label="Cube", command=self.myCube)
                cmds.button(label = 'delete active', command = self.myDelete)
                cmds.separator(h=20, style="none")

                # you need to remember the ids of the sliders so you can query their values
                self.TranslateX = cmds.intSliderGrp(
                    label="Translate X ", field=True, min=1, max=100, value=0, cc=self.move_active)
                self.TranslateY = cmds.intSliderGrp(
                    label="Translate Y ", field=True, min=1, max=100, value=0, cc=self.move_active)
                self.TranslateZ = cmds.intSliderGrp(
                    label="Translate Z ", field=True, min=1, max=100, value=0, cc=self.move_active)

                cmds.showWindow(self.WINDOW_NAME)

            def mySphere(self, *_):  # _ is python slang for "I will ignore this argument"
                self.active_shape = cmds.polySphere()[0]
                # for good measure, change the window title to the name of the active object
                cmds.window(self.WINDOW_NAME, e=true, title = self.active_shape)

            def myCube(self, *_):
                self.active_shape = cmds.polyCube()[0]
                cmds.window(self.WINDOW_NAME, e=true, title = self.active_shape)

            def myDelete(self, *_):
                if self.active_shape:
                    cmds.delete(self.active_shape)
                    self.active_shape = None
                    cmds.window(self.WINDOW_NAME, e=true, title = "nothing selected")

            def move_active(self, *_):
                # you could do separate functions for each slider,
                # but in this case it's easler to do the same one everwhere
                # using the stored widget names and the stored shape
                if self.active_shape:
                    tx = cmds.intSliderGrp(self.TranslateX, q=True, v=True)
                    ty = cmds.intSliderGrp(self.TranslateY, q=True, v=True)
                    tz = cmds.intSliderGrp(self.TranslateZ, q=True, v=True)

                    cmds.xform(self.active_shape, t=(tx, ty, tz))
                else:
                    print "create a shape before adjusting sliders"


        ui=ShapeUI()

基本诀窍是所有函数都使用self,因此他们可以访问变量self.active_shapeself.TranslateX等等。