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