Python API 2.0 Reference
python/api1/py1AnimCubeNode.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 "spAnimCube".
43 #
44 # This plug-in demonstrates how to take time as an input, and create polygonal
45 # geometry for output. The compute method of the node constructs a polygonal
46 # cube whose size depends on the current frame number. The resulting mesh is
47 # passed to an internal Maya node that displays it and allows it to be positioned.
48 #
49 # To use this node, execute the python script animCubeNode.py that contains
50 # the following commands:
51 #
52 # import maya.cmds as cmds
53 # cmds.createNode("transform", name="animCube1")
54 # cmds.createNode("mesh", name="animCubeShape1", parent="animCube1")
55 # cmds.sets("animCubeShape1", add="initialShadingGroup")
56 # cmds.createNode("spAnimCube", name="animCubeNode1")
57 # cmds.connectAttr("time1.outTime", "animCubeNode1.time")
58 # cmds.connectAttr("animCubeNode1.outputMesh", "animCubeShape1.inMesh")
59 #
60 # This creates a mesh node under a transform node which is hooked into the world for display.
61 # It then creates an spAnimCube node, and connects its input to the time node, and its
62 # output to the mesh node.
63 #
64 # A cube will now appear on the screen. If the play button on the time slider is pressed,
65 # the displayed cube will grow and shrink as the frame number changes.
66 #
67 ########################################################################
68 
69 # import maya.cmds as cmds
70 # cmds.createNode("transform", name="animCube1")
71 # cmds.createNode("mesh", name="animCubeShape1", parent="animCube1")
72 # cmds.sets("animCubeShape1", add="initialShadingGroup")
73 # cmds.createNode("spAnimCube", name="animCubeNode1")
74 # cmds.connectAttr("time1.outTime", "animCubeNode1.time")
75 # cmds.connectAttr("animCubeNode1.outputMesh", "animCubeShape1.inMesh")
76 
77 import sys
78 import maya.OpenMaya as OpenMaya
79 import maya.OpenMayaMPx as OpenMayaMPx
80 
81 kPluginNodeName = "spAnimCube"
82 kPluginNodeId = OpenMaya.MTypeId(0x00080052)
83 
84 class animCube(OpenMayaMPx.MPxNode):
85  time = OpenMaya.MObject()
86  outputMesh = OpenMaya.MObject()
87 
88  def __init__(self):
89  OpenMayaMPx.MPxNode.__init__(self)
90 
91  def createMesh(self, tempTime, outData):
92  frame = int(tempTime.asUnits(OpenMaya.MTime.kFilm))
93  if frame is 0:
94  frame = 1
95 
96  cubeSize = 0.5 * float(frame % 10)
97 
98  numFaces = 6
99  numVertices = 8
100  numFaceConnects = 24
101 
102  vtx_1 = OpenMaya.MFloatPoint(-cubeSize, -cubeSize, -cubeSize)
103  vtx_2 = OpenMaya.MFloatPoint( cubeSize, -cubeSize, -cubeSize)
104  vtx_3 = OpenMaya.MFloatPoint( cubeSize, -cubeSize, cubeSize)
105  vtx_4 = OpenMaya.MFloatPoint(-cubeSize, -cubeSize, cubeSize)
106  vtx_5 = OpenMaya.MFloatPoint(-cubeSize, cubeSize, -cubeSize)
107  vtx_6 = OpenMaya.MFloatPoint(-cubeSize, cubeSize, cubeSize)
108  vtx_7 = OpenMaya.MFloatPoint( cubeSize, cubeSize, cubeSize)
109  vtx_8 = OpenMaya.MFloatPoint( cubeSize, cubeSize, -cubeSize)
110 
111  points = OpenMaya.MFloatPointArray()
112  points.setLength(8)
113  points.set(vtx_1, 0)
114  points.set(vtx_2, 1)
115  points.set(vtx_3, 2)
116  points.set(vtx_4, 3)
117  points.set(vtx_5, 4)
118  points.set(vtx_6, 5)
119  points.set(vtx_7, 6)
120  points.set(vtx_8, 7)
121 
122  faceConnects = OpenMaya.MIntArray()
123  faceConnects.setLength(numFaceConnects)
124  faceConnects.set(0, 0)
125  faceConnects.set(1, 1)
126  faceConnects.set(2, 2)
127  faceConnects.set(3, 3)
128  faceConnects.set(4, 4)
129  faceConnects.set(5, 5)
130  faceConnects.set(6, 6)
131  faceConnects.set(7, 7)
132  faceConnects.set(3, 8)
133  faceConnects.set(2, 9)
134  faceConnects.set(6, 10)
135  faceConnects.set(5, 11)
136  faceConnects.set(0, 12)
137  faceConnects.set(3, 13)
138  faceConnects.set(5, 14)
139  faceConnects.set(4, 15)
140  faceConnects.set(0, 16)
141  faceConnects.set(4, 17)
142  faceConnects.set(7, 18)
143  faceConnects.set(1, 19)
144  faceConnects.set(1, 20)
145  faceConnects.set(7, 21)
146  faceConnects.set(6, 22)
147  faceConnects.set(2, 23)
148 
149  faceCounts = OpenMaya.MIntArray()
150  faceCounts.setLength(6)
151  faceCounts.set(4, 0)
152  faceCounts.set(4, 1)
153  faceCounts.set(4, 2)
154  faceCounts.set(4, 3)
155  faceCounts.set(4, 4)
156  faceCounts.set(4, 5)
157 
158  meshFS = OpenMaya.MFnMesh()
159  newMesh = meshFS.create(numVertices, numFaces, points, faceCounts, faceConnects, outData)
160 
161  return newMesh
162 
163  def compute(self, plug, data):
164  if plug == animCube.outputMesh:
165  timeData = data.inputValue(animCube.time)
166  tempTime = timeData.asTime()
167 
168  outputHandle = data.outputValue(animCube.outputMesh)
169 
170  dataCreator = OpenMaya.MFnMeshData()
171  newOutputData = dataCreator.create()
172 
173  self.createMesh(tempTime, newOutputData)
174 
175  outputHandle.setMObject(newOutputData)
176  data.setClean(plug)
177  else:
178  return OpenMaya.kUnknownParameter
179 
180 def nodeCreator():
181  return OpenMayaMPx.asMPxPtr( animCube() )
182 
183 def nodeInitializer():
184  unitAttr = OpenMaya.MFnUnitAttribute()
185  typedAttr = OpenMaya.MFnTypedAttribute()
186 
187  animCube.time = unitAttr.create("time", "tm", OpenMaya.MFnUnitAttribute.kTime, 0.0)
188  animCube.outputMesh = typedAttr.create("outputMesh", "out", OpenMaya.MFnData.kMesh)
189 
190  animCube.addAttribute(animCube.time)
191  animCube.addAttribute(animCube.outputMesh)
192 
193  animCube.attributeAffects(animCube.time, animCube.outputMesh)
194 
195 
196 # initialize the script plug-in
197 def initializePlugin(mobject):
198  mplugin = OpenMayaMPx.MFnPlugin(mobject)
199  try:
200  mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator, nodeInitializer)
201  except:
202  sys.stderr.write( "Failed to register node: %s" % kPluginNodeName )
203  raise
204 
205 # uninitialize the script plug-in
206 def uninitializePlugin(mobject):
207  mplugin = OpenMayaMPx.MFnPlugin(mobject)
208  try:
209  mplugin.deregisterNode( kPluginNodeId )
210  except:
211  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeName )
212  raise
213