First of all, this seems to be a great place to learn more about programming. I've written a maya python script, where both functions work, I'm having trouble getting the UI button to call the superExtrude() function, though. The first function does the geometric mesh manipulations and the second one should produce the UI for the user inputs:
import maya.cmds as cmds
def superExtrude(extrScale, extrDist):
"""Loops through a list of selected meshes and extrudes all of the mesh faces to produce a polygon frame, based on existing mesh tesselations"""
myObjectLt = cmds.ls(selection=True)
for i in range(len(myObjectLt)):
numFaces = cmds.polyEvaluate(face=True)
item = myObjectLt[i] + ".f[:]"
cmds.select(clear=True)
cmds.select(item, replace=True)
#extrude by scale
cmds.polyExtrudeFacet(constructionHistory=True, keepFacesTogether=False, localScaleX=extrScale, localScaleY=extrScale, localScaleZ=extrScale)
selFaces = cmds.ls(selection=True)
cmds.delete(selFaces)
#extrude by height
cmds.select(item, replace=True)
cmds.polyExtrudeFacet(constructionHistory=True, keepFacesTogether=True, localTranslateZ=extrDist)
def extrWindow():
"""Creates the user interface UI for the user input of the extrusion scale and height"""
windowID = "superExtrWindow"
if cmds.window(windowID, exists=True):
cmds.deleteUI(windowID)
cmds.window(windowID, title="SuperExtrude", sizeable=False, resizeToFitChildren=True)
cmds.rowColumnLayout(numberOfColumns=2, columnWidth=[(1,120),(2,120)], columnOffset=[1,"right",3])
cmds.text(label="Extrusion Scale:")
extrScaleVal = cmds.floatField(text=0.9)
cmds.text(label="Extrusion Height:")
extrDistVal = cmds.floatField(text=-0.3)
cmds.separator(height=10, style="none")
cmds.separator(height=10, style="none")
cmds.separator(height=10, style="none")
cmds.button(label="Apply", command=superExtrude(extrScaleVal, extrDistVal))
cmds.showWindow()
extrWindow()
I'm pretty new to python and maya scripting, so any help would be greatly appreciated. :)
Im not sure if this is the answer you want but what you have to know about maya "commands" flags :
If you want to put a function in a button call, you need to pass in the function name without any arguments (eg : command = myFunction) (get rid of the ending brackets "()" )
in your function, you need to add a "*args" because the maya button always passes an argument (I think it's "False") (eg : def myFunction(customArg1, customArg2, *args) )
If you want to pass arguments in the button signal, you need to use the partial function from the functools module (from functools import partial) and use it like this: cmds.button( command = partial(myFunction, arg1, arg2, kwarg1=value1, kwarg2=value2) )
One more thing, about pymel and cmds... it's probably a never ending story but pymel is not almighty... When you have to deal with a lot of informations (like getting a vertices list on a mesh), pymel can be something like 40x slower than a simple maya commands. It has its pros and its cons... If you've just started with python, I wouldn't recommend to get into pymel right now. Get familiar with the syntax and the commands, and when you're ok, switch to pymel (which is very useful when you are dealing with objects creation)
Hope this helped, Cheers
Edit :
Based on your first post, what you need to change in your code to make it work is :
so i switched everything to pymel which is what you should be learning in. cmds is garbage. take the time to look at the differences between yours and mine. scripts like these are what help you to get started. if any further explanation is needed let me know.
do this td a favor and learn pymel
pymel online docs = http://download.autodesk.com/global/docs/maya2014/en_us/PyMel/
St4rb0y
First, your floatField calls (lines 33, 35) are using an invalid flag, 'text'. You probably mean to use 'value', so change both lines.
Secondly, when building UI control types, the 'command' flag seeks a string, so you have to wrap the command and it's arguments in quotation:
Change those three lines and it should all work fine.
Tips:
To comment a single line of code, use # instead of enclosing the entire line in triple single quotes. The use of triple quotes is more handy for commenting out many lines of code.
Another tip for control command flags: You can define a string variable to pass commands, and use the variable instead of the string directly. This trick will come in handy when building dynamic controls, ie, assembling commands based on user selections:
This line calls
superExtrude
and assigns its return value tocommand
. SincesuperExtrude
doesn't return anything, the button effectively has a commnand ofNone
.Perhaps you meant to have
superExtrude
get called when the button is clicked, in which case you ought to wrap it in a lambda to prevent it from being called immediately: