SHOW:
|
|
- or go back to the newest paste.
1 | import math, sys | |
2 | import maya.OpenMaya as OpenMaya | |
3 | import maya.OpenMayaMPx as OpenMayaMPx | |
4 | ||
5 | kPluginNodeTypeName = "MathNode" | |
6 | ||
7 | mathNodeId = OpenMaya.MTypeId(0x87107) | |
8 | ||
9 | # Node definition | |
10 | class mathNode(OpenMayaMPx.MPxNode): | |
11 | # class variables | |
12 | input = OpenMaya.MObject() | |
13 | oper = OpenMaya.MObject() | |
14 | scale = OpenMaya.MObject() | |
15 | output = OpenMaya.MObject() | |
16 | radgrad=OpenMaya.MObject() | |
17 | def __init__(self): | |
18 | OpenMayaMPx.MPxNode.__init__(self) | |
19 | def compute(self,plug,dataBlock): | |
20 | if ( plug == mathNode.output ): | |
21 | dataHandle = dataBlock.inputValue( mathNode.input ) | |
22 | dataOpHandle=dataBlock.inputValue(mathNode.oper) | |
23 | dataRGHandle=dataBlock.inputValue(mathNode.radgrad) | |
24 | dataSclHandle=dataBlock.inputValue(mathNode.scale) | |
25 | operShort=dataOpHandle.asShort() | |
26 | rgShort=dataRGHandle.asShort() | |
27 | inputFloat = dataHandle.asFloat() | |
28 | scaleFloat = dataSclHandle.asFloat() | |
29 | result=0.0 | |
30 | outputHandle = dataBlock.outputValue( mathNode.output ) | |
31 | if (operShort==0): | |
32 | result = math.sin( inputFloat ) * scaleFloat | |
33 | outputHandle.setFloat( result ) | |
34 | elif(operShort==1): | |
35 | result = math.cos( inputFloat ) * scaleFloat | |
36 | outputHandle.setFloat( result ) | |
37 | elif(operShort==2): | |
38 | result = math.tan( inputFloat ) * scaleFloat | |
39 | outputHandle.setFloat( result ) | |
40 | elif(operShort==3): | |
41 | if(inputFloat > 1): | |
42 | inputFloat=1 | |
43 | if(inputFloat < -1): | |
44 | inputFloat=-1 | |
45 | result = math.asin( inputFloat ) * scaleFloat | |
46 | if(rgShort==0): | |
47 | outputHandle.setFloat( result ) | |
48 | else: | |
49 | outputHandle.setFloat( math.degrees(result)) | |
50 | elif(operShort==4): | |
51 | if(inputFloat>1): | |
52 | inputFloat=1 | |
53 | if(inputFloat<-1): | |
54 | inputFloat=-1 | |
55 | result = math.acos( inputFloat ) * scaleFloat | |
56 | if(rgShort==0): | |
57 | outputHandle.setFloat( result ) | |
58 | else: | |
59 | outputHandle.setFloat( math.degrees(result)) | |
60 | elif(operShort==5): | |
61 | result = math.atan( inputFloat ) * scaleFloat | |
62 | if(rgShort==0): | |
63 | outputHandle.setFloat( result ) | |
64 | else: | |
65 | outputHandle.setFloat( math.degrees(result)) | |
66 | dataBlock.setClean( plug ) | |
67 | return OpenMaya.kUnknownParameter | |
68 | ||
69 | # creator | |
70 | def nodeCreator(): | |
71 | return OpenMayaMPx.asMPxPtr( mathNode() ) | |
72 | ||
73 | # initializer | |
74 | def nodeInitializer(): | |
75 | # input | |
76 | nAttr = OpenMaya.MFnNumericAttribute() | |
77 | mathNode.input = nAttr.create( "input", "in", OpenMaya.MFnNumericData.kFloat, 0.0 ) | |
78 | nAttr.setStorable(1) | |
79 | nAttr.setKeyable(1) | |
80 | nAttr.setWritable(1) | |
81 | # enum attribute operator | |
82 | enumAttr=OpenMaya.MFnEnumAttribute() | |
83 | mathNode.oper=enumAttr.create("operator","op", 0) | |
84 | attrF=enumAttr.addField("sin",0) | |
85 | attrF=enumAttr.addField("cos",1) | |
86 | attrF=enumAttr.addField("tan",2) | |
87 | attrF=enumAttr.addField("arcsin",3) | |
88 | attrF=enumAttr.addField("arccos",4) | |
89 | attrF=enumAttr.addField("arctan",5) | |
90 | enumAttr.setStorable(1) | |
91 | enumAttr.setKeyable(0) | |
92 | enumAttr.setWritable(1) | |
93 | #radiant to deg and vice versa attribute | |
94 | radgradAttr=OpenMaya.MFnEnumAttribute() | |
95 | mathNode.radgrad=radgradAttr.create("angleType","at",0) | |
96 | rgF=radgradAttr.addField("rad",0) | |
97 | rgF=radgradAttr.addField("grad",1) | |
98 | radgradAttr.setStorable(1) | |
99 | radgradAttr.setKeyable(0) | |
100 | radgradAttr.setWritable(1) | |
101 | #scale attribute | |
102 | sAttr=OpenMaya.MFnNumericAttribute() | |
103 | mathNode.scale= sAttr.create( "scale", "scl", OpenMaya.MFnNumericData.kFloat, 1.0 ) | |
104 | sAttr.setStorable(1) | |
105 | sAttr.setKeyable(1) | |
106 | sAttr.setWritable(1) | |
107 | # output | |
108 | nAttr = OpenMaya.MFnNumericAttribute() | |
109 | mathNode.output = nAttr.create( "output", "out", OpenMaya.MFnNumericData.kFloat, 0.0 ) | |
110 | nAttr.setStorable(0) | |
111 | nAttr.setWritable(0) | |
112 | # add attributes | |
113 | mathNode.addAttribute( mathNode.input ) | |
114 | mathNode.addAttribute( mathNode.oper ) | |
115 | mathNode.addAttribute(mathNode.radgrad) | |
116 | mathNode.addAttribute(mathNode.scale) | |
117 | mathNode.addAttribute( mathNode.output ) | |
118 | # dependency graph | |
119 | mathNode.attributeAffects( mathNode.input, mathNode.output ) | |
120 | mathNode.attributeAffects( mathNode.oper, mathNode.output ) | |
121 | mathNode.attributeAffects(mathNode.radgrad,mathNode.output) | |
122 | mathNode.attributeAffects( mathNode.scale, mathNode.output ) | |
123 | # initialize the script plug-in | |
124 | def initializePlugin(mobject): | |
125 | mplugin = OpenMayaMPx.MFnPlugin(mobject,"Antonio Sacco","0.5") | |
126 | try: | |
127 | mplugin.registerNode( kPluginNodeTypeName, mathNodeId, nodeCreator, nodeInitializer ) | |
128 | except: | |
129 | sys.stderr.write( "Failed to register node: %s" % kPluginNodeTypeName ) | |
130 | raise | |
131 | ||
132 | # uninitialize the script plug-in | |
133 | def uninitializePlugin(mobject): | |
134 | mplugin = OpenMayaMPx.MFnPlugin(mobject) | |
135 | try: | |
136 | mplugin.deregisterNode( mathNodeId ) | |
137 | except: | |
138 | sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeTypeName ) | |
139 | raise |