scripted/footPrintManip.py

scripted/footPrintManip.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 "spFootPrintLocator"
43 # and "spFootPrintLocatorManip".
44 #
45 # This example demonstrates how to use the Show Manip Tool with a user-defined
46 # manipulator. The user-defined manipulator corresponds to the foot print locator.
47 #
48 # To use this plug-in:
49 # (1) Execute the footPrintManip.py script to create a foot print locator.
50 # (2) Select the foot print and then click the Show Manip Tool.
51 #
52 ########################################################################
53 
54 import maya.OpenMaya as OpenMaya
55 import maya.OpenMayaMPx as OpenMayaMPx
56 import maya.OpenMayaRender as OpenMayaRender
57 import maya.OpenMayaUI as OpenMayaUI
58 
59 import math
60 import sys
61 
62 kPluginLocatorTypeName = "spFootPrintLocator"
63 kPluginLocatorManipTypeName = "spFootPrintLocatorManip"
64 
65 footPrintLocatorId = OpenMaya.MTypeId(0x8700C)
66 footPrintLocatorManipId = OpenMaya.MTypeId(0x8700D)
67 
68 glRenderer = OpenMayaRender.MHardwareRenderer.theRenderer()
69 glFT = glRenderer.glFunctionTable()
70 
71 sole = ( [ 0.00, 0.0, -0.70 ],
72 [ 0.04, 0.0, -0.69 ],
73 [ 0.09, 0.0, -0.65 ],
74 [ 0.13, 0.0, -0.61 ],
75 [ 0.16, 0.0, -0.54 ],
76 [ 0.17, 0.0, -0.46 ],
77 [ 0.17, 0.0, -0.35 ],
78 [ 0.16, 0.0, -0.25 ],
79 [ 0.15, 0.0, -0.14 ],
80 [ 0.13, 0.0, 0.00 ],
81 [ 0.00, 0.0, 0.00 ],
82 [ -0.13, 0.0, 0.00 ],
83 [ -0.15, 0.0, -0.14 ],
84 [ -0.16, 0.0, -0.25 ],
85 [ -0.17, 0.0, -0.35 ],
86 [ -0.17, 0.0, -0.46 ],
87 [ -0.16, 0.0, -0.54 ],
88 [ -0.13, 0.0, -0.61 ],
89 [ -0.09, 0.0, -0.65 ],
90 [ -0.04, 0.0, -0.69 ],
91 [ -0.00, 0.0, -0.70 ] )
92 
93 heel = ( [ 0.00, 0.0, 0.06 ],
94 [ 0.13, 0.0, 0.06 ],
95 [ 0.14, 0.0, 0.15 ],
96 [ 0.14, 0.0, 0.21 ],
97 [ 0.13, 0.0, 0.25 ],
98 [ 0.11, 0.0, 0.28 ],
99 [ 0.09, 0.0, 0.29 ],
100 [ 0.04, 0.0, 0.30 ],
101 [ 0.00, 0.0, 0.30 ],
102 [ -0.04, 0.0, 0.30 ],
103 [ -0.09, 0.0, 0.29 ],
104 [ -0.11, 0.0, 0.28 ],
105 [ -0.13, 0.0, 0.25 ],
106 [ -0.14, 0.0, 0.21 ],
107 [ -0.14, 0.0, 0.15 ],
108 [ -0.13, 0.0, 0.06 ],
109 [ -0.00, 0.0, 0.06 ] )
110 
111 
112 class footPrintLocatorManip(OpenMayaMPx.MPxManipContainer):
113  def __init__(self):
114  OpenMayaMPx.MPxManipContainer.__init__(self)
115  self.fDistanceManip = OpenMaya.MDagPath()
116  self.fNodePath = OpenMaya.MDagPath()
117 
118  def createChildren(self):
119  try:
120  startPoint = OpenMaya.MPoint(0.0, 0.0, 0.0)
121  direction = OpenMaya.MVector(0.0, 1.0, 0.0)
122 
123  self.fDistanceManip = self.addDistanceManip("distanceManip", "distance")
124 
125  distanceManipFn = OpenMayaUI.MFnDistanceManip(self.fDistanceManip)
126  distanceManipFn.setStartPoint(startPoint)
127  distanceManipFn.setDirection(direction)
128  except:
129  sys.stderr.write("ERROR: footPrintLocatorManip.createChildren\n")
130  raise
131 
132  def plugToManipConversion( manipIndex ):
133  try:
134  numData = OpenMaya.MFnNumericData()
135  numDataObj = numData.create(OpenMaya.MFnNumericData.k3Double)
136 
137  vec = self.nodeTranslation()
138  numData.setData3Double(vec.x, vec.y, vec.z)
139 
140  returnData = OpenMaya.MManipData(numDataObj)
141  except:
142  sys.stderr.write("ERROR: footPrintLocatorManip.plugToManipConversion\n")
143  raise
144  return returnData
145 
146  def connectToDependNode(self, node):
147  try:
148  dagNodeFn = OpenMaya.MFnDagNode(node)
149  dagNodeFn.getPath(self.fNodePath)
150 
151  distanceManipFn = OpenMayaUI.MFnDistanceManip(self.fDistanceManip)
152  nodeFn = OpenMaya.MFnDependencyNode(node)
153 
154  sizePlug = nodeFn.findPlug('size')
155  distanceManipFn.connectToDistancePlug(sizePlug)
156 
157  self.finishAddingManips()
158  OpenMayaMPx.MPxManipContainer.connectToDependNode(self, node)
159  except:
160  sys.stderr.write("ERROR: footPrintLocatorManip.connectToDependNode\n")
161  raise
162 
163  def draw(self, view, path, style, status):
164  OpenMayaMPx.MPxManipContainer.draw(self, view, path, style, status)
165 
166  view.beginGL()
167 
168  textPosVector = self.nodeTranslation()
169  textPosPoint = OpenMaya.MPoint(textPosVector.x, textPosVector.y, textPosVector.z)
170 
171  view.drawText('Stretch Me!', textPosPoint, OpenMayaUI.M3dView.kLeft)
172  view.endGL()
173 
174  def startPointCallback(self, index):
175  numData = OpenMaya.MFnNumericData()
176  numDataObj = numData.create(OpenMaya.MFnNumericData.k3Double)
177 
178  vec = self.nodeTranslation()
179  numData.setData3Double(vec.x, vec.y, vec.z)
180 
181  return OpenMayaUI.MManipData(numDataObj)
182 
183  def nodeTranslation(self):
184  dagFn = OpenMaya.MFnDagNode(self.fNodePath)
185  path = OpenMaya.MDagPath()
186 
187  dagFn.getPath(path)
188 
189  # pop from the shape to the transform
190  path.pop()
191 
192  transformFn = OpenMaya.MFnTransform(path)
193  return transformFn.translation(OpenMaya.MSpace.kWorld)
194 
195 
196 class footPrintLocator(OpenMayaMPx.MPxLocatorNode):
197  size = OpenMaya.MObject()
198 
199  def __init__(self):
200  OpenMayaMPx.MPxLocatorNode.__init__(self)
201 
202  def compute(self, plug, dataBlock):
203  return OpenMaya.kUnknownParameter
204 
205  def draw(self, view, path, style, status):
206  thisNode = self.thisMObject()
207 
208  plug = OpenMaya.MPlug(thisNode, self.size)
209 
210  sizeVal = plug.asMDistance()
211 
212  multiplier = sizeVal.asCentimeters()
213 
214  view.beginGL()
215 
216  if style == OpenMayaUI.M3dView.kFlatShaded or style == OpenMayaUI.M3dView.kGouraudShaded:
217  glFT.glPushAttrib(OpenMayaRender.MGL_CURRENT_BIT)
218 
219  if status == OpenMayaUI.M3dView.kActive:
220  view.setDrawColor( 13, OpenMayaUI.M3dView.kActiveColors )
221  else:
222  view.setDrawColor( 13, OpenMayaUI.M3dView.kDormantColors )
223 
224  last = len(sole) - 1
225  glFT.glBegin( OpenMayaRender.MGL_TRIANGLE_FAN )
226  for i in range(last):
227  glFT.glVertex3f(sole[i][0]*multiplier, sole[i][1]*multiplier, sole[i][2]*multiplier)
228  glFT.glEnd()
229 
230  last = len(heel) - 1
231  glFT.glBegin( OpenMayaRender.MGL_TRIANGLE_FAN )
232  for i in range(last):
233  glFT.glVertex3f(heel[i][0]*multiplier, heel[i][1]*multiplier, heel[i][2]*multiplier)
234  glFT.glEnd()
235 
236  glFT.glPopAttrib()
237 
238  glFT.glBegin(OpenMayaRender.MGL_LINES)
239 
240  last = len(sole) - 1
241  for i in range(last):
242  glFT.glVertex3f( sole[i][0]*multiplier, sole[i][1]*multiplier, sole[i][2]*multiplier )
243  glFT.glVertex3f( sole[i+1][0]*multiplier, sole[i+1][1]*multiplier, sole[i+1][2]*multiplier )
244 
245  last = len(heel) - 1
246  for i in range(last):
247  glFT.glVertex3f( heel[i][0]*multiplier, heel[i][1]*multiplier, heel[i][2]*multiplier )
248  glFT.glVertex3f( heel[i+1][0]*multiplier, heel[i+1][1]*multiplier, heel[i+1][2]*multiplier )
249 
250  glFT.glEnd()
251 
252  view.endGL()
253 
254  def isBounded(self):
255  return True
256 
257  def boundingBox(self):
258  thisNode = self.thisMObject()
259  plug = OpenMaya.MPlug(thisNode, self.size)
260 
261  sizeVal = plug.asMDistance()
262 
263  multiplier = sizeVal.asCentimeters()
264 
265  corner1 = OpenMaya.MPoint(-0.17, 0.0, -0.7)
266  corner2 = OpenMaya.MPoint(0.17, 0.0, 0.3)
267 
268  corner1 = corner1 * multiplier
269  corner2 = corner2 * multiplier
270 
271  bbox = OpenMaya.MBoundingBox( corner1, corner2 )
272  return bbox
273 
274 
275 def locatorCreator():
276  return OpenMayaMPx.asMPxPtr( footPrintLocator() )
277 
278 def locatorInitializer():
279  unitFn = OpenMaya.MFnUnitAttribute()
280  footPrintLocator.size = unitFn.create("size", "in", OpenMaya.MFnUnitAttribute.kDistance)
281  unitFn.setDefault(10.0)
282  unitFn.setStorable(True)
283  unitFn.setWritable(True)
284 
285  footPrintLocator.addAttribute( footPrintLocator.size )
286  OpenMayaMPx.MPxManipContainer.addToManipConnectTable(footPrintLocatorId)
287 
288 def locatorManipCreator():
289  return OpenMayaMPx.asMPxPtr( footPrintLocatorManip() )
290 
291 def locatorManipInitializer():
292  OpenMayaMPx.MPxManipContainer.initialize()
293 
294 
295 # initialize the script plug-in
296 def initializePlugin(mobject):
297  mplugin = OpenMayaMPx.MFnPlugin(mobject)
298 
299  try:
300  mplugin.registerNode( kPluginLocatorTypeName, footPrintLocatorId, locatorCreator, locatorInitializer, OpenMayaMPx.MPxNode.kLocatorNode )
301  except:
302  sys.stderr.write( "Failed to register node: %s" % kPluginLocatorTypeName )
303  raise
304 
305  try:
306  mplugin.registerNode( kPluginLocatorManipTypeName, footPrintLocatorManipId, locatorManipCreator, locatorManipInitializer, OpenMayaMPx.MPxNode.kManipContainer )
307  except:
308  sys.stderr.write( "Failed to register node: %s" % kPluginLocatorManipTypeName )
309  raise
310 
311 
312 # uninitialize the script plug-in
313 def uninitializePlugin(mobject):
314  mplugin = OpenMayaMPx.MFnPlugin(mobject)
315  try:
316  mplugin.deregisterNode( footPrintLocatorId )
317  except:
318  sys.stderr.write( "Failed to deregister node: %s" % kPluginLocatorTypeName )
319  raise
320 
321  try:
322  mplugin.deregisterNode( footPrintLocatorManipId )
323  except:
324  sys.stderr.write( "Failed to deregister node: %s" % kPluginLocatorManipTypeName )
325  raise