Using Sliders in Maya with Python to control Shapes - intSliderGrp

前端 未结 2 923
余生分开走
余生分开走 2021-01-27 08:55

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 s

相关标签:
2条回答
  • 2021-01-27 09:19

    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.

    0 讨论(0)
  • 2021-01-27 09:39

    If you want to know more about sliders and various ui :

    How to store the value of floatSliderGrp in Python Maya

    How to create Maya sliders to move objects in interface

    Material and Texture Change Python Script

    Maya 2015 Python - textField input, radioButton selection not working in UI

    UI in maya is the most ask question, check topics I've answered You can check also Theodox answers : https://stackoverflow.com/users/1936075/theodox

    0 讨论(0)
提交回复
热议问题