scripted/sineNode.py

scripted/sineNode.py
1 #-
2 # ==========================================================================
3 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
4 # rights reserved.
5 #
6 # The coded instructions, statements, computer programs, and/or related
7 # material (collectively the "Data") in these files contain unpublished
8 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
9 # licensors, which is protected by U.S. and Canadian federal copyright
10 # law and by international treaties.
11 #
12 # The Data is provided for use exclusively by You. You have the right
13 # to use, modify, and incorporate this Data into other products for
14 # purposes authorized by the Autodesk software license agreement,
15 # without fee.
16 #
17 # The copyright notices in the Software and this entire statement,
18 # including the above license grant, this restriction and the
19 # following disclaimer, must be included in all copies of the
20 # Software, in whole or in part, and all derivative works of
21 # the Software, unless such copies or derivative works are solely
22 # in the form of machine-executable object code generated by a
23 # source language processor.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
26 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
27 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
28 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
29 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
30 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
31 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
32 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
33 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
34 # OR PROBABILITY OF SUCH DAMAGES.
35 #
36 # ==========================================================================
37 #+
38 
39 # import maya
40 # maya.cmds.loadPlugin("sineNode.py")
41 # maya.cmds.createNode("spSineNode")
42 # maya.cmds.getAttr("spSineNode1.output")
43 
44 import math, sys
45 
46 import maya.OpenMaya as OpenMaya
47 import maya.OpenMayaMPx as OpenMayaMPx
48 
49 kPluginNodeTypeName = "spSineNode"
50 
51 sineNodeId = OpenMaya.MTypeId(0x87000)
52 
53 # Node definition
54 class sineNode(OpenMayaMPx.MPxNode):
55  # class variables
56  input = OpenMaya.MObject()
57  output = OpenMaya.MObject()
58  def __init__(self):
59  OpenMayaMPx.MPxNode.__init__(self)
60  def compute(self,plug,dataBlock):
61  if ( plug == sineNode.output ):
62  dataHandle = dataBlock.inputValue( sineNode.input )
63 
64  inputFloat = dataHandle.asFloat()
65  result = math.sin( inputFloat ) * 10.0
66  outputHandle = dataBlock.outputValue( sineNode.output )
67  outputHandle.setFloat( result )
68  dataBlock.setClean( plug )
69 
70  return OpenMaya.kUnknownParameter
71 
72 # creator
73 def nodeCreator():
74  return OpenMayaMPx.asMPxPtr( sineNode() )
75 
76 # initializer
77 def nodeInitializer():
78  # input
79  nAttr = OpenMaya.MFnNumericAttribute()
80  sineNode.input = nAttr.create( "input", "in", OpenMaya.MFnNumericData.kFloat, 0.0 )
81  nAttr.setStorable(1)
82  # output
83  nAttr = OpenMaya.MFnNumericAttribute()
84  sineNode.output = nAttr.create( "output", "out", OpenMaya.MFnNumericData.kFloat, 0.0 )
85  nAttr.setStorable(1)
86  nAttr.setWritable(1)
87  # add attributes
88  sineNode.addAttribute( sineNode.input )
89  sineNode.addAttribute( sineNode.output )
90  sineNode.attributeAffects( sineNode.input, sineNode.output )
91 
92 # initialize the script plug-in
93 def initializePlugin(mobject):
94  mplugin = OpenMayaMPx.MFnPlugin(mobject)
95  try:
96  mplugin.registerNode( kPluginNodeTypeName, sineNodeId, nodeCreator, nodeInitializer )
97  except:
98  sys.stderr.write( "Failed to register node: %s" % kPluginNodeTypeName )
99  raise
100 
101 # uninitialize the script plug-in
102 def uninitializePlugin(mobject):
103  mplugin = OpenMayaMPx.MFnPlugin(mobject)
104  try:
105  mplugin.deregisterNode( sineNodeId )
106  except:
107  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeTypeName )
108  raise
109