Skip to content

Instantly share code, notes, and snippets.

@leixingyu
Created December 5, 2021 23:22
Show Gist options
  • Save leixingyu/6789c67a3aea4258589c78878eeacc99 to your computer and use it in GitHub Desktop.
Save leixingyu/6789c67a3aea4258589c78878eeacc99 to your computer and use it in GitHub Desktop.
import maya.api.OpenMaya as om
import sys
nodeName = 'myFirstNode'
nodeID = om.MTypeId(0x55555)
nodeClassify = 'utility/general'
def maya_useNewAPI():
pass
class MyNode(om.MPxNode):
inRadiusAttr = om.MObject()
inTranslateAttr = om.MObject()
outRotateAttr = om.MObject()
def __init__(self):
om.MPxNode.__init__(self)
# plug is MPlug type object, dataBlock is MDataBlock type object, refers to the entire datablock of the node
def compute(self, plug, dataBlock):
if plug == MyNode.outRotateAttr:
# step one: get datablock handle (inputValue returns MDataHandle type object)
inRadiusHandle = dataBlock.inputValue(MyNode.inRadiusAttr)
inTranslateHandle = dataBlock.inputValue(MyNode.inTranslateAttr)
outRotateHandle = dataBlock.outputValue(MyNode.outRotateAttr)
# step two: extract input value from the handle
inRadius = inRadiusHandle.asFloat()
inTranslate = inTranslateHandle.asFloat()
# step three: create logic and set output value
outRotate = inTranslate/(6.18*inRadius) * -360
outRotateHandle.setFloat(outRotate)
# step four: mark output plug as clean
outRotateHandle.setClean()
# unknown plug, unknown error
else:
return om.kUnknownParameter
def nodeCreator():
return MyNode()
def nodeInitializer():
defaultRadius = 2.0
defaultTranslate = 0.0
# step one: create reference to attribute function set such as numericAttribute
numericAttrFn = om.MFnNumericAttribute()
# step two: create attribute using the function set
MyNode.inRadiusAttr = numericAttrFn.create('radius', 'r', om.MFnNumericData.kFloat, defaultRadius)
numericAttrFn.readable = True # API1.0: numericAttrFn.setReadable(True)
numericAttrFn.writable = True
numericAttrFn.storable = True
numericAttrFn.hidden = False
numericAttrFn.keyable = True
MyNode.inTranslateAttr = numericAttrFn.create('translate', 't', om.MFnNumericData.kFloat, defaultTranslate)
numericAttrFn.readable = True
numericAttrFn.writable = True
numericAttrFn.storable = True
numericAttrFn.hidden = False
numericAttrFn.keyable = True
MyNode.outRotateAttr = numericAttrFn.create('rotation', 'rot', om.MFnNumericData.kFloat)
numericAttrFn.readable = True
numericAttrFn.writable = False
numericAttrFn.storable = False
numericAttrFn.hidden = False
# step three: attach attribute
MyNode.addAttribute(MyNode.inRadiusAttr)
MyNode.addAttribute(MyNode.inTranslateAttr)
MyNode.addAttribute(MyNode.outRotateAttr)
# step four: add circuit (relationship in->out)
MyNode.attributeAffects(MyNode.inRadiusAttr, MyNode.outRotateAttr)
MyNode.attributeAffects(MyNode.inTranslateAttr, MyNode.outRotateAttr)
def initializePlugin(mobject):
mplugin = om.MFnPlugin(mobject)
try:
mplugin.registerNode(nodeName, nodeID, nodeCreator, nodeInitializer, om.MPxNode.kDependNode, nodeClassify)
except:
sys.stderr.write('fail to register node: ' + nodeName)
raise
def uninitializePlugin(mobject):
mplugin = om.MFnPlugin(mobject)
try:
mplugin.deregisterNode(nodeID)
except:
sys.stderr.write('fail to de-register node: ' + nodeName)
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment