scripted/footPrintNode.py

scripted/footPrintNode.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 "spFootPrint".
43 #
44 # This example demonstrates how to create a user-defined locator.
45 # A locator is a DAG object that is drawn in the 3D views, but that does
46 # not render. This example plug-in defines a new locator node that draws
47 # a foot print. The foot print can be selected and moved using the regular
48 # manipulators.
49 #
50 # To use this plug-in, execute the footPrintNode.py script to create a foot print locator.
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 kPluginNodeTypeName = "spFootPrintNode"
63 
64 footPrintNodeId = OpenMaya.MTypeId(0x87010)
65 glRenderer = OpenMayaRender.MHardwareRenderer.theRenderer()
66 glFT = glRenderer.glFunctionTable()
67 
68 sole = ( [ 0.00, 0.0, -0.70 ],
69 [ 0.04, 0.0, -0.69 ],
70 [ 0.09, 0.0, -0.65 ],
71 [ 0.13, 0.0, -0.61 ],
72 [ 0.16, 0.0, -0.54 ],
73 [ 0.17, 0.0, -0.46 ],
74 [ 0.17, 0.0, -0.35 ],
75 [ 0.16, 0.0, -0.25 ],
76 [ 0.15, 0.0, -0.14 ],
77 [ 0.13, 0.0, 0.00 ],
78 [ 0.00, 0.0, 0.00 ],
79 [ -0.13, 0.0, 0.00 ],
80 [ -0.15, 0.0, -0.14 ],
81 [ -0.16, 0.0, -0.25 ],
82 [ -0.17, 0.0, -0.35 ],
83 [ -0.17, 0.0, -0.46 ],
84 [ -0.16, 0.0, -0.54 ],
85 [ -0.13, 0.0, -0.61 ],
86 [ -0.09, 0.0, -0.65 ],
87 [ -0.04, 0.0, -0.69 ],
88 [ -0.00, 0.0, -0.70 ] )
89 
90 heel = ( [ 0.00, 0.0, 0.06 ],
91 [ 0.13, 0.0, 0.06 ],
92 [ 0.14, 0.0, 0.15 ],
93 [ 0.14, 0.0, 0.21 ],
94 [ 0.13, 0.0, 0.25 ],
95 [ 0.11, 0.0, 0.28 ],
96 [ 0.09, 0.0, 0.29 ],
97 [ 0.04, 0.0, 0.30 ],
98 [ 0.00, 0.0, 0.30 ],
99 [ -0.04, 0.0, 0.30 ],
100 [ -0.09, 0.0, 0.29 ],
101 [ -0.11, 0.0, 0.28 ],
102 [ -0.13, 0.0, 0.25 ],
103 [ -0.14, 0.0, 0.21 ],
104 [ -0.14, 0.0, 0.15 ],
105 [ -0.13, 0.0, 0.06 ],
106 [ -0.00, 0.0, 0.06 ] )
107 
108 class footPrintNode(OpenMayaMPx.MPxLocatorNode):
109  size = OpenMaya.MObject()
110 
111  def __init__(self):
112  OpenMayaMPx.MPxLocatorNode.__init__(self)
113 
114  def compute(self, plug, dataBlock):
115  return OpenMaya.kUnknownParameter
116 
117  def draw(self, view, path, style, status):
118  thisNode = self.thisMObject()
119 
120  plug = OpenMaya.MPlug(thisNode, self.size)
121 
122  sizeVal = plug.asMDistance()
123 
124  multiplier = sizeVal.asCentimeters()
125 
126  view.beginGL()
127 
128  if style == OpenMayaUI.M3dView.kFlatShaded or style == OpenMayaUI.M3dView.kGouraudShaded:
129  glFT.glPushAttrib(OpenMayaRender.MGL_CURRENT_BIT)
130 
131  if status == OpenMayaUI.M3dView.kActive:
132  view.setDrawColor( 13, OpenMayaUI.M3dView.kActiveColors )
133  else:
134  view.setDrawColor( 13, OpenMayaUI.M3dView.kDormantColors )
135 
136  last = len(sole) - 1
137  glFT.glBegin( OpenMayaRender.MGL_TRIANGLE_FAN )
138  for i in range(last):
139  glFT.glVertex3f(sole[i][0]*multiplier, sole[i][1]*multiplier, sole[i][2]*multiplier)
140  glFT.glEnd()
141 
142  last = len(heel) - 1
143  glFT.glBegin( OpenMayaRender.MGL_TRIANGLE_FAN )
144  for i in range(last):
145  glFT.glVertex3f(heel[i][0]*multiplier, heel[i][1]*multiplier, heel[i][2]*multiplier)
146  glFT.glEnd()
147 
148  glFT.glPopAttrib()
149 
150  glFT.glBegin(OpenMayaRender.MGL_LINES)
151 
152  last = len(sole) - 1
153  for i in range(last):
154  glFT.glVertex3f( sole[i][0]*multiplier, sole[i][1]*multiplier, sole[i][2]*multiplier )
155  glFT.glVertex3f( sole[i+1][0]*multiplier, sole[i+1][1]*multiplier, sole[i+1][2]*multiplier )
156 
157  last = len(heel) - 1
158  for i in range(last):
159  glFT.glVertex3f( heel[i][0]*multiplier, heel[i][1]*multiplier, heel[i][2]*multiplier )
160  glFT.glVertex3f( heel[i+1][0]*multiplier, heel[i+1][1]*multiplier, heel[i+1][2]*multiplier )
161 
162  glFT.glEnd()
163 
164  view.endGL()
165 
166  def isBounded(self):
167  return True
168 
169  def boundingBox(self):
170  thisNode = self.thisMObject()
171  plug = OpenMaya.MPlug(thisNode, self.size)
172 
173  sizeVal = plug.asMDistance()
174 
175  multiplier = sizeVal.asCentimeters()
176 
177  corner1 = OpenMaya.MPoint(-0.17, 0.0, -0.7)
178  corner2 = OpenMaya.MPoint(0.17, 0.0, 0.3)
179 
180  corner1 = corner1 * multiplier
181  corner2 = corner2 * multiplier
182 
183  bbox = OpenMaya.MBoundingBox( corner1, corner2 )
184  return bbox
185 
186 
187 # creator
188 def nodeCreator():
189  return OpenMayaMPx.asMPxPtr( footPrintNode() )
190 
191 # initializer
192 def nodeInitializer():
193  unitFn = OpenMaya.MFnUnitAttribute()
194  footPrintNode.size = unitFn.create("size", "in", OpenMaya.MFnUnitAttribute.kDistance)
195  unitFn.setDefault(1.0)
196  footPrintNode.addAttribute( footPrintNode.size )
197 
198 # initialize the script plug-in
199 def initializePlugin(mobject):
200  mplugin = OpenMayaMPx.MFnPlugin(mobject)
201  try:
202  mplugin.registerNode( kPluginNodeTypeName, footPrintNodeId, nodeCreator, nodeInitializer, OpenMayaMPx.MPxNode.kLocatorNode )
203  except:
204  sys.stderr.write( "Failed to register node: %s" % kPluginNodeTypeName )
205  raise
206 
207 # uninitialize the script plug-in
208 def uninitializePlugin(mobject):
209  mplugin = OpenMayaMPx.MFnPlugin(mobject)
210  try:
211  mplugin.deregisterNode( footPrintNodeId )
212  except:
213  sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeTypeName )
214  raise
215