Created
September 10, 2018 15:39
-
-
Save blockinhead/b3cf6a229306a0706fceeab16d02ebb5 to your computer and use it in GitHub Desktop.
maya python api 2.0 node example which evaluates with respect to node's state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # encoding: utf8 | |
| import maya.api.OpenMaya as om | |
| import math | |
| def maya_useNewAPI(): | |
| pass | |
| class SimpleNode(om.MPxNode): | |
| node_name = 'simpleNode' | |
| node_id = om.MTypeId(0x83002) | |
| a_arg = None | |
| a_amp = None | |
| a_val = None | |
| def __init__(self): | |
| om.MPxNode.__init__(self) | |
| @staticmethod | |
| def creator(): | |
| return SimpleNode() | |
| @staticmethod | |
| def initialize(): | |
| num_attr = om.MFnNumericAttribute() | |
| SimpleNode.a_arg = num_attr.create('arg', 'arg', om.MFnNumericData.kFloat, 0.0) | |
| num_attr.storable = True | |
| om.MPxNode.addAttribute(SimpleNode.a_arg) | |
| SimpleNode.a_amp = num_attr.create('amp', 'amp', om.MFnNumericData.kFloat, 1.0) | |
| num_attr.storable = True | |
| om.MPxNode.addAttribute(SimpleNode.a_amp) | |
| SimpleNode.a_val = num_attr.create('val', 'val', om.MFnNumericData.kFloat, 0.0) | |
| num_attr.storable = True | |
| num_attr.writable = True | |
| om.MPxNode.addAttribute(SimpleNode.a_val) | |
| om.MPxNode.attributeAffects(SimpleNode.a_arg, SimpleNode.a_val) | |
| om.MPxNode.attributeAffects(SimpleNode.a_amp, SimpleNode.a_val) | |
| def compute(self, plug, data): | |
| # type: (om.MPlug, om.MDataBlock) -> None | |
| state = om.MFnDependencyNode(self.thisMObject()).findPlug('nodeState', False).asInt() | |
| if state == 1: | |
| data.outputValue(SimpleNode.a_val).setFloat(data.inputValue(SimpleNode.a_arg).asFloat()) | |
| return | |
| if plug == SimpleNode.a_val: | |
| arg = data.inputValue(SimpleNode.a_arg).asFloat() | |
| amp = data.inputValue(SimpleNode.a_amp).asFloat() | |
| val_handle = data.outputValue(SimpleNode.a_val) # type: om.MDataHandle | |
| val_handle.setFloat(amp * math.sin(arg)) | |
| data.setClean(plug) | |
| def initializePlugin(obj): | |
| fn_plugin = om.MFnPlugin(obj, 'ilya radovilsky', '1.0') | |
| fn_plugin.registerNode(SimpleNode.node_name, SimpleNode.node_id, SimpleNode.creator, SimpleNode.initialize) | |
| def uninitializePlugin(obj): | |
| fn_plugin = om.MFnPlugin(obj) | |
| fn_plugin.deregisterNode(SimpleNode.node_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment