问题
Im super new to python and i have this little spare time project going on. And i cant find a solution to the following problem:
I set up a GUI like this:
flWin = mc.window(title="Foot Locker", wh=(210,85))
mc.columnLayout()
mc.text(label='Frame Range')
rangeField = mc.intFieldGrp(numberOfFields=2,value1=0, value2=0)
mc.rowColumnLayout(numberOfRows=2)
translateBox = mc.checkBox(label='Translation',value=True)
mc.button(label="Bake it!", w=60, command="Bake()")
rotateBox = mc.checkBox(label='Rotation',value=True)
mc.button(label='Key it!', w=60, command='Key()')
scaleBox = mc.checkBox(label='Scale')
mc.showWindow(flWin)
and then later, inside the function 'Bake' id like to query the checkboxes to do different stuff, depending on what boxes are checked... like this:
translateValue = mc.checkBox(translateBox, query=True)
rotateValue = mc.checkBox(rotateBox, query=True)
scaleValue = mc.checkBox(scaleBox, query=True)
if scaleValue = True:
if rotateValue = True:
if translateValue = True:
mc.parentConstraint ('LockCator', Selection, n='LockCatorConstraint')
mc.scaleConstraint('LockCator', Selection, n='selectionScale')
else:
mc.parentConstraint ('LockCator', Selection, n='LockCatorConstraint', skipTranslate=True)
mc.scaleConstraint('LockCator', Selection, n='selectionScale')
bla bla bla... you get the trick...
when i try to run the script, i get a error saying that there is invalid syntax on the line if scaleValue = True:
i also tried using this:
mc.attributeQuery(translateBox,value=True)
but that gives me an error, saying that 'value' is an invalid flag... i dont know what that means.
Some help here would be greatly appreciated!! Thanks guys!
回答1:
You were close, the query flag simply tells the command you want to get the data, rather than set, whatever you're queering, has to also appear in the same command, you're just missing the v=True
flag for the fields.
translateValue = mc.checkBox(translateBox, query=True, value=True)
rotateValue = mc.checkBox(rotateBox, query=True, value=True)
scaleValue = mc.checkBox(scaleBox, query=True, value=True)
Also, where you're chaining your if commands, seeing as your value can only be true or false, you can simply write if (scaleValue):
which is the same as writing if scaleValue == True:
if (scaleValue):
if (rotateValue):
if (translateValue):
mc.parentConstraint ('LockCator', Selection, n='LockCatorConstraint')
mc.scaleConstraint('LockCator', Selection, n='selectionScale')
else:
mc.parentConstraint ('LockCator', Selection, n='LockCatorConstraint', skipTranslate=True)
mc.scaleConstraint('LockCator', Selection, n='selectionScale')
Better yet, seeing as you're doing basically the same thing for these chains, we can simplify this:
skipTrans = True if scaleValue and rotateValue and translateValue else False
mc.parentConstraint ('LockCator', Selection, n='LockCatorConstraint', skipTranslate=skipTrans)
mc.scaleConstraint('LockCator', Selection, n='selectionScale')
The above is exactly the same as the code above this code.
Hope this helps, as @jonathon has also provided, the way you've written your UI can get very messy and hard to read, definitely read into QT Designer, it's a brilliant program.
回答2:
If I understand your question correctly all you need to do is include both query
and value
flags, e.g:
import maya.cmds as mc
flWin = mc.window(title="Foot Locker", wh=(210,85))
mc.columnLayout()
mc.text(label='Frame Range')
rangeField = mc.intFieldGrp(numberOfFields=2,value1=0, value2=0)
mc.rowColumnLayout(numberOfRows=2)
translateBox = mc.checkBox(label='Translation',value=True)
mc.button(label="Bake it!", w=60, command="Bake()")
rotateBox = mc.checkBox(label='Rotation',value=True)
mc.button(label='Key it!', w=60, command='Key()')
scaleBox = mc.checkBox(label='Scale')
mc.showWindow(flWin)
print mc.checkBox(scaleBox, q=True, v=True)
returns True
when querying a UI element you need to put the command in query mode and then also supply the value to query, in this case the value. So you had all the elements there just not at the same time!
This behaviour is weird I know but when you understand how MEL and its equivalent command works it makes more sense.
Also if I remember correctly you can now use PySide (a python Qt Library) inside Maya python which sounds like a much better idea for programatically creating a ui. If you are after a simpler way to create maya ui's you can also use Qt Designer to build a .ui
file that maya can load at runtime.
For example to create a window from a ui file:
# first delete window if it already exists
if (cmds.window('window_name', exists=True)):
cmds.deleteUI('window_name')
window = cmds.loadUI('my_window.ui'))
cmds.showWindow(window)
To query the ui just make sure to give the ui elements unique names inside Qt Designer then query them like you have been doing so far.
For more info on using Qt Designer with maya see this great page:
http://www.creativecrash.com/maya/tutorials/scripting/mel/c/using-qt-designer-for-mel-interfaces
来源:https://stackoverflow.com/questions/19099287/python-in-maya-query-checkbox-value