问题
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()
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/50111472/using-sliders-in-maya-with-python-to-control-shapes-intslidergrp