Python API 2.0 Reference
python/api1/py1SineNode.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 ########################################################################
40 # DESCRIPTION:
41 #
42 # Produces the dependency graph node "spSineNode".
43 #
44 # This plug-in is an example of a user-defined dependency graph node. It takes
45 # a single input value and outputs the sine of this multiplied by 10. It is a
46 # simple example of creating a procedural animation. The number describes a sine
47 # curve as the input varies. If this number is hooked up to the x translation
48 # attributes of an object, the object will describe the movement along the x-axis
49 # as time changes.
50 #
51 ########################################################################
52 
53 # import maya
54 # maya.cmds.loadPlugin("sineNode.py")
55 # maya.cmds.createNode("spSineNode")
56 # maya.cmds.getAttr("spSineNode1.output")
57 
58 import math, sys
59 
60 import maya.OpenMaya as OpenMaya
61 import maya.OpenMayaMPx as OpenMayaMPx
62 
63 kPluginNodeTypeName = "spSineNode"
64 
65 sineNodeId = OpenMaya.MTypeId(0x80045)
66 
67 # Node definition
68 class sineNode(OpenMayaMPx.MPxNode):
69  # class variables
70  input = OpenMaya.MObject()
71  output = OpenMaya.MObject()
72  def __init__(self):
73  OpenMayaMPx.MPxNode.__init__(self)
74  def compute(self,plug,dataBlock):
75  if ( plug == sineNode.output ):
76  dataHandle = dataBlock.inputValue( sineNode.input )
77 
78  inputFloat = dataHandle.asFloat()
79  result = math.sin( inputFloat ) * 10.0
80  outputHandle = dataBlock.outputValue( sineNode.output )
81  outputHandle.setFloat( result )
82  dataBlock.setClean( plug )
83 
84  return OpenMaya.kUnknownParameter
85 
86 # creator
87 def nodeCreator():
88  return OpenMayaMPx.asMPxPtr( sineNode() )
89 
90 # initializer
91 def nodeInitializer():
92  # input
94  sineNode.input = nAttr.create( "input", "in", OpenMaya.MFnNumericData.kFloat, 0.0 )
95  nAttr.setStorable(1)
96  # output
98  sineNode.output = nAttr.create( "output", "out", OpenMaya.MFnNumericData.kFloat, 0.0 )
99  nAttr.setStorable(1)
100  nAttr.setWritable(0)
101  # add attributes
102  sineNode.addAttribute( sineNode.input )
103  sineNode.addAttribute( sineNode.output )
104  sineNode.attributeAffects( sineNode.input, sineNode.output )
105 
106 # initialize the script plug-in
107 def initializePlugin(mobject):
108  mplugin = OpenMayaMPx.MFnPlugin(mobject)
109  try:
110  mplugin.registerNode( kPluginNodeTypeName, sineNodeId, nodeCreator, nodeInitializer )
111  except:
112  sys.stderr.write( "Failed to register node: %s" % kPluginNodeTypeName )
113  raise
114 
115 # uninitialize the script plug-in
116 def uninitializePlugin(mobject):
117  mplugin = OpenMayaMPx.MFnPlugin(mobject)
118  try:
119  mplugin.deregisterNode( sineNodeId )
120  except:
121  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeTypeName )
122  raise
123