상세 컨텐츠

본문 제목

ntFormulaCurveNode

Maya API/Maya_PythonAPI 구문들

by hwano 2014. 1. 28. 16:13

본문

  1. http://pastebin.com/DG1DrUcz#     에서 퍼옴.  

  1.  

  2.  

  3.  

  4.  

  5.  

  6.  

  7.  

  8.  

    #---------------------------------------------------+
  9. # ntFormulaCurve                                    |
  10. # Cinema 4D inspired expression curve generator     |
  11. # Nikolay Tashev - ntjahero@gmail.com               |
  12. #---------------------------------------------------+
  13. # Created for Maya 2011/2012                        |
  14. #---------------------------------------------------+
  15. # The following code is provided as is.             |
  16. # Modify at your own risk.                          |
  17. # Please do not redistribute without my permission, |
  18. # or at least notify me.                            |
  19. #---------------------------------------------------+
  20. # Usage:                                            |
  21. #   Create the node and connect the .outCurve attr  |
  22. #   to a nurbsCurve.create attribute                |
  23. #---------------------------------------------------+
  24. # Since MStatus is removed from the python api      |
  25. # Maya 2013 just return the proper integer:         |
  26. #   MStatus::kSuccess = 0                           |
  27. #   MStatus::kFailure = 1                           |
  28. #   MStatus::kInsufficientMemory = 2                |
  29. #   MStatus::kInvalidParameter = 3                  |
  30. #   MStatus::kLicenseFailure = 4                    |
  31. #   MStatus::kUnknownParameter = 5                  |
  32. #   MStatus::kNotImplemented = 6                    |
  33. #   MStatus::kNotFound = 7                          |
  34. #   MStatus::kEndOfFile = 8                         |
  35. #---------------------------------------------------+
  36.  
  37.  
  38. import sys
  39. import maya.OpenMaya as om
  40. import maya.OpenMayaMPx as ompx
  41.  
  42. nodeName = 'ntFormulaCurve'
  43. nodeID = om.MTypeId(0xCC001)
  44. nodeAuthor = "Nikolay Tashev"
  45. nodeVersion = "1.1"
  46. nodeApi = "Any"
  47.  
  48. def makeinput(attr, key=1):
  49.     attr.setWritable(1)
  50.     attr.setStorable(1)
  51.     attr.setKeyable(key)
  52. def makeoutput(attr):
  53.     attr.setReadable(1)
  54.     attr.setKeyable(0)
  55.  
  56. class Formula_Curve(ompx.MPxNode):
  57.         kLinear = 0
  58.         kCubic = 1
  59.         kOpen = 0
  60.         kClosed = 1
  61.         kPeriodic = 2
  62.         aTime = om.MObject()
  63.         aNumSpans = om.MObject()
  64.         aParamRangeMin = om.MObject()
  65.         aDegree = om.MObject()
  66.         aForm = om.MObject()
  67.         aExpression = om.MObject()
  68.         aHelpStr = om.MObject()
  69.         aOutCurve = om.MObject()
  70.  
  71.         @staticmethod
  72.         def AETemplate(nodeName):
  73.                 AEStr  = "global proc AE%sTemplate(string $nodeName)\n" % nodeName
  74.                 AEStr += "{\n"
  75.                 AEStr += "editorTemplate -beginScrollLayout;\n"
  76.                 AEStr += "  editorTemplate -addControl \"time\";\n"
  77.                 AEStr += "  editorTemplate -addControl \"spans\";\n"
  78.                 AEStr += "  editorTemplate -addControl \"parameterRange\";\n"
  79.                 AEStr += "  editorTemplate -addControl \"degree\";\n"
  80.                 AEStr += "  editorTemplate -addControl \"form\";\n"
  81.                 AEStr += "  editorTemplate -addControl \"valueX\";\n"
  82.                 AEStr += "  editorTemplate -addControl \"valueY\";\n"
  83.                 AEStr += "  editorTemplate -addControl \"valueZ\";\n"
  84.                 AEStr += "      editorTemplate -callCustom \"AE%sFormulaNew\" \"AE%sFormulaRepl\" \"expression\";\n" % (nodeName,nodeName)
  85.                 AEStr += "      editorTemplate -beginLayout \"Quick Help\" -collapse 1;\n"
  86.                 AEStr += "         editorTemplate -callCustom \"AE%sHelpNew\" \"AE%sHelpRepl\" \"quickHelp\";\n" % (nodeName,nodeName)
  87.                 AEStr += "      editorTemplate -endLayout;\n"
  88.                 AEStr += "  editorTemplate -addExtraControls;\n"
  89.                 AEStr += "editorTemplate -endScrollLayout;\n"
  90.                 AEStr += "}\n"
  91.                 AEStr += "global proc AE%sHelpNew( string $attrName )\n" % nodeName
  92.                 AEStr += "{\n"
  93.                 AEStr += "      textField AE%s_HelpTxt;\n" % nodeName
  94.                 AEStr += "      connectControl AE%s_HelpTxt $attrName;\n" % nodeName
  95.                 AEStr += "      scrollField     -tx `textField -q -tx AE%s_HelpTxt` -ed 0 helpScroll;\n" % nodeName
  96.                 AEStr += "      textField -e -vis 0 AE%s_HelpTxt;\n" % nodeName
  97.                 AEStr += "}\n"
  98.                 AEStr += "global proc AE%sHelpRepl( string $attrName )\n" % nodeName
  99.                 AEStr += "{\n"
  100.                 AEStr += "}\n"
  101.                 AEStr += "global proc AE%sFormulaNew( string $attrName )\n" % nodeName
  102.                 AEStr += "{\n"
  103.                 AEStr += "  text -l \"Point Coordinates Expression:\";\n"
  104.                 AEStr += "      scrollField     -tx `getAttr $attrName` AE%s_expressionField;\n" % nodeName
  105.                 AEStr += "      $AE%sCmd = \"setAttr -type \\\"string\\\" \"+$attrName+\" `scrollField -q -tx AE%s_expressionField`\";\n" % (nodeName, nodeName)
  106.                 AEStr += "      scrollField     -e -cc $AE%sCmd -ec $AE%sCmd AE%s_expressionField;\n" % (nodeName, nodeName, nodeName)
  107.                 AEStr += "}\n"
  108.                 AEStr += "global proc AE%sFormulaRepl( string $attrName )\n" % nodeName
  109.                 AEStr += "{\n"
  110.                 AEStr += "      scrollField     -e -tx `getAttr $attrName` AE%s_expressionField;\n" % nodeName
  111.                 AEStr += "      $AE%sCmd = \"setAttr -type \\\"string\\\" \"+$attrName+\" `scrollField -q -tx AE%s_expressionField`\";\n" % (nodeName, nodeName)
  112.                 AEStr += "      scrollField     -e -cc $AE%sCmd -ec $AE%sCmd AE%s_expressionField;\n" % (nodeName, nodeName, nodeName)
  113.                 AEStr += "}\n"
  114.                 return AEStr
  115.        
  116.         def __init__(self):
  117.                 super(Formula_Curve, self).__init__()
  118.  
  119.         def compute(self, plug, db):
  120.                 # Gather
  121.                 spans = db.inputValue(self.aNumSpans).asInt()
  122.                 time = db.inputValue(self.aTime).asTime().value()
  123.                 plugRange = om.MPlug( self.thisMObject(), self.aParamRange)
  124.                 prange = [ plugRange.child(0).asFloat(), plugRange.child(1).asFloat() ]
  125.                 expression = db.inputValue(self.aExpression).asString()
  126.                 degreeEnum = db.inputValue(self.aDegree).asShort()
  127.                 formEnum = db.inputValue(self.aForm).asShort()
  128.                
  129.                 # Degree And Form
  130.                 degree = 1
  131.                 if degreeEnum != self.kLinear: degree = 3
  132.                 spans = max(spans, degree)
  133.                 form = om.MFnNurbsCurve.kOpen
  134.                 if formEnum == self.kClosed: form = om.MFnNurbsCurve.kClosed
  135.                 elif formEnum == self.kPeriodic: form = om.MFnNurbsCurve.kPeriodic
  136.                
  137.                 # Process
  138.                 knotPositions = om.MPointArray()
  139.                 msu = om.MScriptUtil()
  140.                 ptr = msu.asDoublePtr()
  141.                 pmin = prange[0]
  142.                 pstep = (prange[1] - prange[0])/(spans-1)
  143.                 cmdResult = om.MCommandResult()
  144.                 vectorResult = om.MVector()
  145.                 try:
  146.                         for k in range(spans):
  147.                                 p = pmin + k*pstep
  148.                                 cmd  = "float $K = %f;\n" % k
  149.                                 cmd += "float $P = %f;\n" % p
  150.                                 cmd += "float $T = %f;\n" % time
  151.                                 cmd += "float $N = %f;\n" % spans
  152.                                 cmd += "float $X = 0;\n"
  153.                                 cmd += "float $Y = 0;\n"
  154.                                 cmd += "float $Z = 0;\n"
  155.                                 cmd += "%s;\n" % expression
  156.                                 cmd += "vector $fnCurvePt = <<$X, $Y, $Z>>;\n"
  157.                                 om.MGlobal.executeCommand(cmd, cmdResult, 0, 0)
  158.                                 cmdResult.getResult(vectorResult)
  159.                                 knotPositions.append( om.MPoint(vectorResult) )
  160.                 except:
  161.                         #om.MGlobal.displayError("Error evaluating expression")
  162.                         return 5
  163.                 cvDataFn = om.MFnNurbsCurveData()
  164.                 cvDataObj = cvDataFn.create()
  165.                 cvFn = om.MFnNurbsCurve()
  166.                 cvFn.createWithEditPoints(knotPositions, degree, form, 0, 1, 1, cvDataObj)
  167.                 dhOutCurve = db.outputValue(self.aOutCurve)
  168.                 dhOutCurve.setMObject(cvDataObj)
  169.                 dhOutCurve.setClean()
  170.                 #return om.MStatus.kSuccess
  171.                 return 0
  172.        
  173.         @staticmethod
  174.         def createNode():
  175.                 return ompx.asMPxPtr( Formula_Curve() )
  176.  
  177.         @staticmethod
  178.         def initializeNode():
  179.                 atime = om.MFnUnitAttribute()
  180.                 atype = om.MFnTypedAttribute()
  181.                 anum = om.MFnNumericAttribute()
  182.                 aenum = om.MFnEnumAttribute()
  183.                 defStrData = om.MFnStringData()
  184.        
  185.                 Formula_Curve.aTime = atime.create("time", "t", om.MFnUnitAttribute.kTime)
  186.                 makeinput(atime)
  187.                 Formula_Curve.aNumSpans = anum.create("spans", "sp", om.MFnNumericData.kInt, 32)
  188.                 makeinput(anum)
  189.        
  190.                 Formula_Curve.aDegree = aenum.create("degree", "d")
  191.                 aenum.addField("Linear", Formula_Curve.kLinear)
  192.                 aenum.addField("Cubic", Formula_Curve.kCubic)
  193.                 makeinput(aenum)
  194.        
  195.                 Formula_Curve.aForm = aenum.create("form", "f")
  196.                 aenum.addField("Open", Formula_Curve.kOpen)
  197.                 aenum.addField("Closed", Formula_Curve.kClosed)
  198.                 aenum.addField("Periodic", Formula_Curve.kPeriodic)
  199.                 makeinput(aenum)
  200.        
  201.                 aPMin = anum.create("parameterMin", "pn", om.MFnNumericData.kFloat, -1.0)
  202.                 makeinput(anum)
  203.                 aPMax = anum.create("parameterMax", "px", om.MFnNumericData.kFloat, 1.0)
  204.                 makeinput(anum)
  205.                
  206.                 Formula_Curve.aParamRange = anum.create("parameterRange", "pr", aPMin, aPMax)
  207.                 makeinput(anum)
  208.                
  209.                 expression  = "$X = $P;\n"
  210.                 expression += "$Y = sind($P*360);\n"
  211.                 expression += "$Z = cosd($P*360);\n"
  212.                 defStr = defStrData.create(expression)
  213.                 Formula_Curve.aExpression = atype.create("expression", "ex", om.MFnData.kString, defStr)
  214.                 makeinput(atype)
  215.                
  216.                 helpStr = "Local Variables:\n"
  217.                 helpStr += "\tfloat $K: current knot id, [0:spans]\n"
  218.                 helpStr += "\tfloat $P: parameter per knot, [parameterMin:parameterMax]\n"
  219.                 helpStr += "\tfloat $T: time attribute value\n"
  220.                 helpStr += "\tfloat $N: number of spans\n"
  221.                 helpStr += "\tfloat $X\\$Y\\$Z: current point coordinates\n"
  222.        
  223.                 defStr = defStrData.create(helpStr)
  224.                 Formula_Curve.aHelpStr = atype.create("quickHelp", "qh", om.MFnData.kString, defStr)
  225.                 atype.setStorable(0)
  226.                 atype.setWritable(0)
  227.                 atype.setKeyable(0)
  228.                
  229.                 Formula_Curve.aOutCurve = atype.create("outCurve", "ocv", om.MFnData.kNurbsCurve)
  230.                 makeoutput(atype)
  231.                
  232.                 Formula_Curve.addAttribute(Formula_Curve.aTime)
  233.                 Formula_Curve.addAttribute(Formula_Curve.aNumSpans)
  234.                 Formula_Curve.addAttribute(Formula_Curve.aDegree)
  235.                 Formula_Curve.addAttribute(Formula_Curve.aForm)
  236.                 Formula_Curve.addAttribute(Formula_Curve.aParamRange)
  237.                 Formula_Curve.addAttribute(Formula_Curve.aExpression)
  238.                 Formula_Curve.addAttribute(Formula_Curve.aHelpStr)
  239.                 Formula_Curve.addAttribute(Formula_Curve.aOutCurve)
  240.                
  241.                 Formula_Curve.attributeAffects(Formula_Curve.aTime, Formula_Curve.aOutCurve)
  242.                 Formula_Curve.attributeAffects(Formula_Curve.aNumSpans, Formula_Curve.aOutCurve)
  243.                 Formula_Curve.attributeAffects(Formula_Curve.aDegree, Formula_Curve.aOutCurve)
  244.                 Formula_Curve.attributeAffects(Formula_Curve.aForm, Formula_Curve.aOutCurve)
  245.                 Formula_Curve.attributeAffects(Formula_Curve.aParamRange, Formula_Curve.aOutCurve)
  246.                 Formula_Curve.attributeAffects(Formula_Curve.aExpression, Formula_Curve.aOutCurve)
  247.                
  248.                 #return om.MStatus.kSuccess
  249.                 return 0
  250.  
  251. def initializePlugin(obj):
  252.         plugin = ompx.MFnPlugin(obj, nodeAuthor, nodeVersion, nodeApi)
  253.         try:
  254.                 om.MGlobal.executeCommand(Formula_Curve.AETemplate(nodeName))
  255.                 plugin.registerNode(nodeName, nodeID, Formula_Curve.createNode, Formula_Curve.initializeNode)
  256.         except Exception as msg:
  257.                 sys.stdout.write("%s Failed\n" % nodeName)
  258.                 print '\n%s' % msg
  259.  
  260. def uninitializePlugin(obj):
  261.         plugin = ompx.MFnPlugin(obj)
  262.         try:
  263.                 plugin.deregisterNode(nodeID)
  264.         except:
  265.                 sys.stdout.write("%s Failed on exit\n" % nodeName)

관련글 더보기