scripted/animCubeNode.py

scripted/animCubeNode.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.cmds as cmds
40 # cmds.createNode("transform", name="animCube1")
41 # cmds.createNode("mesh", name="animCubeShape1", parent="animCube1")
42 # cmds.sets("animCubeShape1", add="initialShadingGroup")
43 # cmds.createNode("spAnimCube", name="animCubeNode1")
44 # cmds.connectAttr("time1.outTime", "animCubeNode1.time")
45 # cmds.connectAttr("animCubeNode1.outputMesh", "animCubeShape1.inMesh")
46 
47 import sys
48 import maya.OpenMaya as OpenMaya
49 import maya.OpenMayaMPx as OpenMayaMPx
50 
51 kPluginNodeName = "spAnimCube"
52 kPluginNodeId = OpenMaya.MTypeId(0x8700B)
53 
54 class animCube(OpenMayaMPx.MPxNode):
55  time = OpenMaya.MObject()
56  outputMesh = OpenMaya.MObject()
57 
58  def __init__(self):
59  OpenMayaMPx.MPxNode.__init__(self)
60 
61  def createMesh(self, tempTime, outData):
62  frame = int(tempTime.asUnits(OpenMaya.MTime.kFilm))
63  if frame is 0:
64  frame = 1
65 
66  cubeSize = 0.5 * float(frame % 10)
67 
68  numFaces = 6
69  numVertices = 8
70  numFaceConnects = 24
71 
72  vtx_1 = OpenMaya.MFloatPoint(-cubeSize, -cubeSize, -cubeSize)
73  vtx_2 = OpenMaya.MFloatPoint( cubeSize, -cubeSize, -cubeSize)
74  vtx_3 = OpenMaya.MFloatPoint( cubeSize, -cubeSize, cubeSize)
75  vtx_4 = OpenMaya.MFloatPoint(-cubeSize, -cubeSize, cubeSize)
76  vtx_5 = OpenMaya.MFloatPoint(-cubeSize, cubeSize, -cubeSize)
77  vtx_6 = OpenMaya.MFloatPoint(-cubeSize, cubeSize, cubeSize)
78  vtx_7 = OpenMaya.MFloatPoint( cubeSize, cubeSize, cubeSize)
79  vtx_8 = OpenMaya.MFloatPoint( cubeSize, cubeSize, -cubeSize)
80 
81  points = OpenMaya.MFloatPointArray()
82  points.setLength(8)
83  points.set(vtx_1, 0)
84  points.set(vtx_2, 1)
85  points.set(vtx_3, 2)
86  points.set(vtx_4, 3)
87  points.set(vtx_5, 4)
88  points.set(vtx_6, 5)
89  points.set(vtx_7, 6)
90  points.set(vtx_8, 7)
91 
92  faceConnects = OpenMaya.MIntArray()
93  faceConnects.setLength(numFaceConnects)
94  faceConnects.set(0, 0)
95  faceConnects.set(1, 1)
96  faceConnects.set(2, 2)
97  faceConnects.set(3, 3)
98  faceConnects.set(4, 4)
99  faceConnects.set(5, 5)
100  faceConnects.set(6, 6)
101  faceConnects.set(7, 7)
102  faceConnects.set(3, 8)
103  faceConnects.set(2, 9)
104  faceConnects.set(6, 10)
105  faceConnects.set(5, 11)
106  faceConnects.set(0, 12)
107  faceConnects.set(3, 13)
108  faceConnects.set(5, 14)
109  faceConnects.set(4, 15)
110  faceConnects.set(0, 16)
111  faceConnects.set(4, 17)
112  faceConnects.set(7, 18)
113  faceConnects.set(1, 19)
114  faceConnects.set(1, 20)
115  faceConnects.set(7, 21)
116  faceConnects.set(6, 22)
117  faceConnects.set(2, 23)
118 
119  faceCounts = OpenMaya.MIntArray()
120  faceCounts.setLength(6)
121  faceCounts.set(4, 0)
122  faceCounts.set(4, 1)
123  faceCounts.set(4, 2)
124  faceCounts.set(4, 3)
125  faceCounts.set(4, 4)
126  faceCounts.set(4, 5)
127 
128  meshFS = OpenMaya.MFnMesh()
129  newMesh = meshFS.create(numVertices, numFaces, points, faceCounts, faceConnects, outData)
130 
131  return newMesh
132 
133  def compute(self, plug, data):
134  if plug == animCube.outputMesh:
135  timeData = data.inputValue(animCube.time)
136  tempTime = timeData.asTime()
137 
138  outputHandle = data.outputValue(animCube.outputMesh)
139 
140  dataCreator = OpenMaya.MFnMeshData()
141  newOutputData = dataCreator.create()
142 
143  self.createMesh(tempTime, newOutputData)
144 
145  outputHandle.setMObject(newOutputData)
146  data.setClean(plug)
147  else:
148  return OpenMaya.kUnknownParameter
149 
150 def nodeCreator():
151  return OpenMayaMPx.asMPxPtr( animCube() )
152 
153 def nodeInitializer():
154  unitAttr = OpenMaya.MFnUnitAttribute()
155  typedAttr = OpenMaya.MFnTypedAttribute()
156 
157  animCube.time = unitAttr.create("time", "tm", OpenMaya.MFnUnitAttribute.kTime, 0.0)
158  animCube.outputMesh = typedAttr.create("outputMesh", "out", OpenMaya.MFnData.kMesh)
159 
160  animCube.addAttribute(animCube.time)
161  animCube.addAttribute(animCube.outputMesh)
162 
163  animCube.attributeAffects(animCube.time, animCube.outputMesh)
164 
165 
166 # initialize the script plug-in
167 def initializePlugin(mobject):
168  mplugin = OpenMayaMPx.MFnPlugin(mobject)
169  try:
170  mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator, nodeInitializer)
171  except:
172  sys.stderr.write( "Failed to register node: %s" % kPluginNodeName )
173  raise
174 
175 # uninitialize the script plug-in
176 def uninitializePlugin(mobject):
177  mplugin = OpenMayaMPx.MFnPlugin(mobject)
178  try:
179  mplugin.deregisterNode( kPluginNodeId )
180  except:
181  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeName )
182  raise
183