Created
October 5, 2014 04:30
-
-
Save nathanielanozie/33f6242b69177898557f to your computer and use it in GitHub Desktop.
camera custom node (maya plugin)
This file contains 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
##@file camNode.py | |
# | |
#Track start and end frames and a transform in Maya(Tested Maya 2008) | |
# | |
#@author Nathaniel Anozie | |
# | |
# | |
#@note Inspired by Maya's api tutorials and examples on sineNode.py and lambertShader.cpp | |
# | |
## | |
#for some error checking | |
import sys | |
#get necessary api functions | |
import maya.OpenMaya as OpenMaya | |
#need this because were making a node | |
import maya.OpenMayaMPx as OpenMayaMPx | |
kPluginNodeTypeName = "camNode" | |
#using a temporary id | |
camNodeId = OpenMaya.MTypeId(0x0000) | |
#build our node off of Maya's our constructor needs to call parent class | |
class camNode(OpenMayaMPx.MPxNode): | |
#in c these are protected, they are the attribute names | |
transform = OpenMaya.MObject() | |
startFrame = OpenMaya.MObject() | |
endFrame = OpenMaya.MObject() | |
output = OpenMaya.MObject() | |
def __init__(self): | |
OpenMayaMPx.MPxNode.__init__(self) | |
##use inputs of node to generate output | |
#@note in c explicitly give it the type for input parameters, in python types are ommited | |
#@param plug tells us the attributes that need to be recomputed | |
#@param use dataBlock to find all nodes attributes can use | |
## | |
def compute(self,plug,dataBlock): | |
#in c would have ommited the class part | |
if( plug == camNode.startFrame ): | |
print 'Great An Update in -- startFrame' | |
#in c what is returned from block.inputValue(...) is a reference ex a MFloatVector& | |
#in python forgetting about the type that is returned | |
sframeHandle = dataBlock.inputValue(camNode.startFrame) | |
startFloat = frameHandle.asFloat() | |
print 'Start Frame is >>%d' %startFloat | |
#mark plug as clean | |
dataBlock.setClean(plug) | |
if( plug == camNode.endFrame ): | |
print 'Great An Update in -- endFrame' | |
eframeHandle = dataBlock.inputValue(camNode.endFrame) | |
endFloat = eframeHandle.asFloat() | |
print 'End Frame is >>%d' %endFloat | |
dataBlock.setClean(plug) | |
if( plug == camNode.transform ): | |
print 'Great An Update in -- transform' | |
tfHandle = dataBlock.inputValue(camNode.transform) | |
tfFloat = tfHandle.asFloat() | |
print 'Transform is >>%d' %tfFloat | |
dataBlock.setClean(plug) | |
if( plug == camNode.output ): | |
print 'Great an Update in Ouput' | |
#in c would have passed output and maybe an error checking object | |
outHandle = dataBlock.outputValue( camNode.output ) | |
#mark output value as clean | |
#in c i think would have used the handle to setClean not the dataBlock | |
dataBlock.setClean(plug) | |
##allow Maya to instantiate an instance of our custom node, its called every time a new instance | |
#of node is made ex: with Maya's createNode command | |
#@note in c this is creator() | |
## | |
def nodeCreator(): | |
return OpenMayaMPx.asMPxPtr( camNode() ) | |
##create the node attributes and set their types, keyable status ets | |
#this is called once when Maya loads the plugin ex: pressing load in plugin manager | |
#@note in c this is initialize() | |
## | |
def nodeInitializer(): | |
#""" | |
#similar in c, this is needed to create numeric attributes for our node | |
#startFrame | |
nAttr = OpenMaya.MFnNumericAttribute(); | |
#This part actually creates the attribute, in c the create would also take a status object | |
#also in c would have set default value separately using setDefault(...) | |
camNode.startFrame = nAttr.create( "startFrame","sf", OpenMaya.MFnNumericData.kFloat, 0.0) | |
#so when file is saved the attribute is written out | |
nAttr.setStorable(1) | |
#so setAttr commands can change this attribute, and attribute can be a connection destination | |
nAttr.setWritable(1) | |
#so cannot be keyframed | |
nAttr.setKeyable(0) | |
#endFrame | |
nAttr = OpenMaya.MFnNumericAttribute(); | |
camNode.endFrame = nAttr.create( "endFrame","ef", OpenMaya.MFnNumericData.kFloat, 0.0) | |
nAttr.setStorable(1) | |
nAttr.setWritable(1) | |
nAttr.setKeyable(0) | |
#transform | |
nAttr = OpenMaya.MFnNumericAttribute(); | |
camNode.transform = nAttr.create( "camTransform","ct", OpenMaya.MFnNumericData.kFloat, 0.0) | |
nAttr.setStorable(1) | |
nAttr.setWritable(1) | |
nAttr.setKeyable(0) | |
#output | |
nAttr = OpenMaya.MFnNumericAttribute(); | |
camNode.output = nAttr.create( "output","out", OpenMaya.MFnNumericData.kFloat, 0.0) | |
nAttr.setStorable(1) | |
nAttr.setWritable(1) | |
nAttr.setKeyable(0) | |
#now we add the attributes to the node | |
camNode.addAttribute(camNode.startFrame) | |
camNode.addAttribute(camNode.endFrame) | |
camNode.addAttribute(camNode.transform) | |
camNode.addAttribute(camNode.output) | |
##so when plug in loaded Maya can register the node | |
# | |
## | |
def initializePlugin(mobject): | |
mplugin = OpenMayaMPx.MFnPlugin(mobject) | |
try: | |
mplugin.registerNode( kPluginNodeTypeName, camNodeId, nodeCreator, nodeInitializer) | |
except: | |
sys.stderr.write("Failed register node: %s" %kPluginNodeTypeName) | |
raise | |
def uninitializePlugin(mobject): | |
mplugin = OpenMayaMPx.MFnPlugin(mobject) | |
try: | |
#only need the id to deregister node | |
mplugin.deregisterNode(camNodeId) | |
except: | |
sys.stderr.write("Failed deregister node: %s" %kPluginNodeTypeName) | |
raise | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment