Maya Python: cmds.button( ) with UI passing variables and calling a function?

后端 未结 4 847
难免孤独
难免孤独 2020-12-28 11:04

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

4条回答
  •  旧时难觅i
    2020-12-28 11:48

    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/

    import pymel.core as pm
    def superExtrude(*args):
        """Loops through a list of selected meshes and extrudes all of the mesh faces to produce a polygon frame, based on existing mesh tesselations"""
        #pymel uses python classes to make things easier
        #its ok to not understand what a class is but just think of it the same as if you were to add an attribute to a polycube. 
        #your code variable now has attributes 
    
        #so with that pymel ls returns a list of PyNodes that correspond to the objects
        #cmds ls returns a list of strings which is very unuseful
        #if you look at the help docs you can find most of whats available
        myObjectLt = pm.ls(selection=True)
    
    
        for i in myObjectLt:
            #instead of cycling through by a number were gonna cycle through the list itself
            #i is now the item in the list
    
            #its unnecessary to select the objects because we can specify it in the polyExtrude
            #cmds.select(item,  replace=True)
    
            #the extrude commands selects things but im not sure what your trying to achive here by seperating
            #the scale extrude and translate extrude
            pm.select(cl=True)
    
    
    
            #the way poly objects wrok is that you have a transform node and a shape node
            # if you graph it in the hypershade you'll see the two nodes
            #the faces are part of the shape node i like accessing things by this node but just know you can do it like this
            #i.f  <-- f is your attribute and i is the item
            #using i.getShape() returns the shape node
    
            # http://download.autodesk.com/global/docs/maya2014/en_us/PyMel/generated/classes/pymel.core.uitypes/pymel.core.uitypes.FloatField.html?highlight=floatfield#pymel.core.uitypes.FloatField
            #since were using pymel the extrScaleVal has function that lets you get the value
            thisScale = extrScaleVal.getValue()
    
    
            pm.polyExtrudeFacet(i.getShape().f, constructionHistory=True, keepFacesTogether=False, localScaleX=thisScale, localScaleY=thisScale, localScaleZ=thisScale)
            #selFaces = cmds.ls(selection=True)
            pm.delete()
    
            #same as before
            thisDist = extrDistVal.getValue()
            #extrude by height
            pm.polyExtrudeFacet(i.getShape().f, constructionHistory=True, keepFacesTogether=True, localTranslateZ=thisDist)
    
    def extrWindow():
        #global is a way to transfer variables from function to function the way you had it
        # you would have had to query the value from your parameters in superExtrude
        #instead do this
        global extrScaleVal, extrDistVal
        #which will makes these parameters to the other function
    
    
        """Creates the user interface UI for the user input of the extrusion scale and height"""
        windowID = "superExtrWindow"
    
        #instead of having a query run just use try except
        #which will just go to except when the try fails
        try:
            pm.deleteUI(windowID)
        except:
            pass
    
        pm.window(windowID, title="SuperExtrude", sizeable=False, resizeToFitChildren=True)
        pm.rowColumnLayout(numberOfColumns=2, columnWidth=[(1,120),(2,120)], columnOffset=[1,"right",3])
    
        pm.text(label="Extrusion Scale:")
        extrScaleVal = pm.floatField(v=0.9)
        pm.text(label="Extrusion Height:")
        extrDistVal = pm.floatField(v=-0.3)
        pm.separator(height=10, style="none")
        pm.separator(height=10, style="none")
        pm.separator(height=10, style="none")
    
        pm.button(label="Apply", c=superExtrude)
        pm.showWindow()
    
    extrWindow()
    

提交回复
热议问题