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