Python API 2.0 Reference
python/api1/py1CircleNode.py
1 from __future__ import division
2 #-
3 # ==========================================================================
4 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
5 # rights reserved.
6 #
7 # The coded instructions, statements, computer programs, and/or related
8 # material (collectively the "Data") in these files contain unpublished
9 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
10 # licensors, which is protected by U.S. and Canadian federal copyright
11 # law and by international treaties.
12 #
13 # The Data is provided for use exclusively by You. You have the right
14 # to use, modify, and incorporate this Data into other products for
15 # purposes authorized by the Autodesk software license agreement,
16 # without fee.
17 #
18 # The copyright notices in the Software and this entire statement,
19 # including the above license grant, this restriction and the
20 # following disclaimer, must be included in all copies of the
21 # Software, in whole or in part, and all derivative works of
22 # the Software, unless such copies or derivative works are solely
23 # in the form of machine-executable object code generated by a
24 # source language processor.
25 #
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
27 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
28 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
29 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
30 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
31 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
32 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
33 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
34 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
35 # OR PROBABILITY OF SUCH DAMAGES.
36 #
37 # ==========================================================================
38 #+
39 
40 #
41 # Autodesk Script File
42 # MODIFY THIS AT YOUR OWN RISK
43 #
44 # Creation Date: 27 September 2006
45 #
46 
47 ########################################################################
48 # DESCRIPTION:
49 #
50 # Produces the dependency graph node "spCircle".
51 #
52 # This plug-in is an example of a user-defined dependency graph node.
53 # It takes a number as input (such as time) and generates two output
54 # numbers. One number describes a sine curve as the input varies and
55 # the other number generates a cosine curve. If these two are hooked up to
56 # the x and z translation attributes of an object, the object will describe
57 # movement through a circle in the xz plane as time is changed.
58 #
59 # See circleNodeTest.py for an example of how to use the node. The script will
60 # create a new "Circle" menu with a single item. Selecting this will build
61 # a simple model (a sphere which follows a circular path), which can be played back
62 # by clicking on the "play" icon on the time slider.
63 # Note: The circleNode plug-in needs to be loaded before the "Circle" menu item
64 # can be executed properly.
65 #
66 # The node has two additional attributes, which can be changed to affect
67 # the animation: "scale" that defines the size of the circular path and
68 # "frames" that defines the number of frames required for a complete circuit
69 # of the path. Either of these can be hooked up to other nodes, or can
70 # be simply set using the command "maya.cmds.setAttr" operating on the circle node
71 # "circleNode1" created by the Python script. For example:
72 #
73 # import maya.cmds as cmds
74 # cmds.createNode("spCircle", name="circleNode1")
75 # cmds.setAttr("circleNode1.scale", 3)
76 # # will change the size of the circle
77 # cmds.setAttr("circleNode1.frames", 5)
78 # # will cause objects to complete a circle in indicated number of frames.
79 #
80 ########################################################################
81 
82 import math, sys
83 
84 import maya.OpenMaya as OpenMaya
85 import maya.OpenMayaMPx as OpenMayaMPx
86 
87 kPluginNodeTypeName = "spCircle"
88 kPluginNodeId = OpenMaya.MTypeId(0x0008004c)
89 
90 
91 # Node definition
92 class circle(OpenMayaMPx.MPxNode):
93  # class variables
94  aInput = OpenMaya.MObject()
95  aScale = OpenMaya.MObject()
96  aFrames = OpenMaya.MObject()
97  aSOutput = OpenMaya.MObject()
98  aCOutput = OpenMaya.MObject()
99 
100 
101  def __init__(self):
102  OpenMayaMPx.MPxNode.__init__(self)
103 
104 
105  def compute(self, plug, data):
106  # Check that the requested recompute is one of the output values
107  if (plug == circle.aSOutput or plug == circle.aCOutput):
108  # Read the input values
109  inputData = data.inputValue(circle.aInput)
110  scaleData = data.inputValue(circle.aScale)
111  framesData = data.inputValue(circle.aFrames)
112 
113  # Compute the output values
114  currentFrame = inputData.asFloat()
115  scaleFactor = scaleData.asFloat()
116  framesPerCircle = framesData.asFloat()
117  angle = 6.2831853 * (currentFrame/framesPerCircle)
118  sinResult = math.sin(angle) * scaleFactor
119  cosResult = math.cos(angle) * scaleFactor
120 
121  # Store them on the output plugs
122  sinHandle = data.outputValue(circle.aSOutput)
123  cosHandle = data.outputValue(circle.aCOutput)
124  sinHandle.setFloat(sinResult)
125  cosHandle.setFloat(cosResult)
126  data.setClean(plug)
127  else:
128  return OpenMaya.kUnknownParameter
129 
130  return None
131 
132 
133 # creator
134 def nodeCreator():
135  return OpenMayaMPx.asMPxPtr( circle() )
136 
137 
138 # initializer
139 def nodeInitializer():
141 
142  # Setup the input attributes
143  circle.aInput = nAttr.create("input", "in", OpenMaya.MFnNumericData.kFloat, 0.0)
144  nAttr.setStorable(True)
145 
146  circle.aScale = nAttr.create("scale", "sc", OpenMaya.MFnNumericData.kFloat, 10.0)
147  nAttr.setStorable(True)
148 
149  circle.aFrames = nAttr.create("frames", "fr", OpenMaya.MFnNumericData.kFloat, 48.0)
150  nAttr.setStorable(True)
151 
152  # Setup the output attributes
153  circle.aSOutput = nAttr.create("sineOutput", "so", OpenMaya.MFnNumericData.kFloat, 0.0)
154  nAttr.setWritable(False)
155  nAttr.setStorable(False)
156 
157  circle.aCOutput = nAttr.create("cosineOutput", "co", OpenMaya.MFnNumericData.kFloat, 0.0,)
158  nAttr.setWritable(False)
159  nAttr.setStorable(False)
160 
161  # Add the attributes to the node
162  circle.addAttribute(circle.aInput)
163  circle.addAttribute(circle.aScale)
164  circle.addAttribute(circle.aFrames)
165  circle.addAttribute(circle.aSOutput)
166  circle.addAttribute(circle.aCOutput)
167 
168  # Set the attribute dependencies
169  circle.attributeAffects(circle.aInput, circle.aSOutput)
170  circle.attributeAffects(circle.aInput, circle.aCOutput)
171  circle.attributeAffects(circle.aScale, circle.aSOutput)
172  circle.attributeAffects(circle.aScale, circle.aCOutput)
173  circle.attributeAffects(circle.aFrames, circle.aSOutput)
174  circle.attributeAffects(circle.aFrames, circle.aCOutput)
175 
176 
177 # initialize the script plug-in
178 def initializePlugin(mobject):
179  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
180  try:
181  mplugin.registerNode( kPluginNodeTypeName, kPluginNodeId, nodeCreator, nodeInitializer )
182  except:
183  sys.stderr.write( "Failed to register node: %s" % kPluginNodeTypeName )
184  raise
185 
186 
187 # uninitialize the script plug-in
188 def uninitializePlugin(mobject):
189  mplugin = OpenMayaMPx.MFnPlugin(mobject)
190  try:
191  mplugin.deregisterNode( kPluginNodeId )
192  except:
193  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeTypeName )
194  raise
195