Advertisement
ntashev

ntFormulaCurve

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