在Maya中将滑块与Python一起使用以控制形状-intSliderGrp [英] Using Sliders in Maya with Python to control Shapes - intSliderGrp

查看:183
本文介绍了在Maya中将滑块与Python一起使用以控制形状-intSliderGrp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对我在此代码中具有的形状的颜色,平移,旋转和缩放使用以下滑块.有什么办法可以用这些滑块控制所有形状吗?我似乎无法弄清楚我已经尽力了.我想使用某个函数来完成此操作,但是任何解决方案都将有所帮助,谢谢您.

    #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()        

解决方案

对于您似乎要在此处尝试的非常特定的应用程序,您还可以使用此处详细的原因. 将需要确保控件都知道正在处理哪个对象并对其进行操作.尽管通常在这种情况下,一个类对于简单的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等.

I am trying to use the following sliders for the Colour, Translate, Rotate and Scale for the shapes I have in this code. Is there a way I can control all the shapes with these sliders? I can't seem to figure it out I've tried everything that I could. I would like to do this with maybe a function but any solution is helpful Any help would be appreciated, thank you.

    #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()        

解决方案

For the very specific application you seem to be trying here, you can also short-circuit some of the work using the attrFieldSliderGrp control which is precisely for passing numbers directly from a gui to an object attribute.

To make this work you'll need to make sure that when you want to edit an object, all of the sliders will need to know it's name. The lazy method would be to make sure that the function just works on the selected item -- though that's not likely to be what you want.

Passing information between the buttons is a bit tricky (for reasons detailed here. Basically you'll need to make sure that the controls all know which object is being worked on and do their work on it. Although in general a class is overkill for simple GUI in this case the class provides an pretty simple way to handle this.

Here's a cut-down example of how it can be done:

        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()

the basic trick is that all the functions use self so they have access to the variables self.active_shape , self.TranslateX and so on.

这篇关于在Maya中将滑块与Python一起使用以控制形状-intSliderGrp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆