scripted/swissArmyManip.py

scripted/swissArmyManip.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 # Autodesk Script File
41 # MODIFY THIS AT YOUR OWN RISK
42 #
43 # Creation Date: 27 September 2006
44 #
45 
46 #########################################################################
47 # DESCRIPTION:
48 #
49 # Produces the dependency graph nodes "spSwissArmyLocator" and "spSwissArmyLocatorManip".
50 #
51 # This plug-in is an example of a user-defined manipulator,
52 # which is comprised of a variety of the base manipulators:
53 # - MFnCircleSweepManip
54 # - MFnDirectionManip
55 # - MFnDiscManip
56 # - MFnDistanceManip
57 # - MFnFreePointTriadManip
58 # - MFnStateManip
59 # - MFnToggleManip
60 # - MFnRotateManip
61 # - MFnScaleManip
62 #
63 # It attaches one of every kind of user-defined manipulator to a node.
64 # It demonstrates the source code required to create each user-defined manipulator.
65 #
66 # To use this plug-in:
67 #
68 # (1) Execute these commands:
69 #
70 # import maya.cmds as cmds
71 # cmds.createNode("spSwissArmyLocator")
72 #
73 # (2) Click the Show Manipulator Tool icon on the side toolbar.
74 # The locator on the screen will be overlaid with one of every kind of user-defined manipulator.
75 #
76 #########################################################################
77 
78 import maya.OpenMaya as OpenMaya
79 import maya.OpenMayaUI as OpenMayaUI
80 import maya.OpenMayaRender as OpenMayaRender
81 import maya.OpenMayaMPx as OpenMayaMPx
82 
83 import math,sys
84 
85 glRenderer = OpenMayaRender.MHardwareRenderer.theRenderer()
86 glFT = glRenderer.glFunctionTable()
87 
88 kSwissArmyLocatorName = "spSwissArmyLocator"
89 kSwissArmyLocatorId = OpenMaya.MTypeId(0x87006)
90 kSwissArmyLocatorManipName = "spSwissArmyLocatorManip"
91 kSwissArmyLocatorManipId = OpenMaya.MTypeId(0x87007)
92 
93 delta1 = 0.01
94 delta2 = 0.02
95 delta3 = 0.03
96 delta4 = 0.04
97 
98 # Locator Data
99 centre = [ [ 0.10, 0.0, 0.10 ],
100  [ 0.10, 0.0, -0.10 ],
101  [ -0.10, 0.0, -0.10 ],
102  [ -0.10, 0.0, 0.10 ],
103  [ 0.10, 0.0, 0.10 ] ]
104 state1 = [ [ 1.00, 0.0, 1.00 ],
105  [ 1.00, 0.0, 0.50 ],
106  [ 0.50, 0.0, 0.50 ],
107  [ 0.50, 0.0, 1.00 ],
108  [ 1.00, 0.0, 1.00 ] ]
109 state2 = [ [ 1.00, 0.0, -1.00 ],
110  [ 1.00, 0.0, -0.50 ],
111  [ 0.50, 0.0, -0.50 ],
112  [ 0.50, 0.0, -1.00 ],
113  [ 1.00, 0.0, -1.00 ] ]
114 state3 = [ [ -1.00, 0.0, -1.00 ],
115  [ -1.00, 0.0, -0.50 ],
116  [ -0.50, 0.0, -0.50 ],
117  [ -0.50, 0.0, -1.00 ],
118  [ -1.00, 0.0, -1.00 ] ]
119 state4 = [ [ -1.00, 0.0, 1.00 ],
120  [ -1.00, 0.0, 0.50 ],
121  [ -0.50, 0.0, 0.50 ],
122  [ -0.50, 0.0, 1.00 ],
123  [ -1.00, 0.0, 1.00 ] ]
124 arrow1 = [ [ 0.00, 0.0, 1.00 ],
125  [ 0.10, 0.0, 0.20 ],
126  [ -0.10, 0.0, 0.20 ],
127  [ 0.00, 0.0, 1.00 ] ]
128 arrow2 = [ [ 1.00, 0.0, 0.00 ],
129  [ 0.20, 0.0, 0.10 ],
130  [ 0.20, 0.0, -0.10 ],
131  [ 1.00, 0.0, 0.00 ] ]
132 arrow3 = [ [ 0.00, 0.0, -1.00 ],
133  [ 0.10, 0.0, -0.20 ],
134  [ -0.10, 0.0, -0.20 ],
135  [ 0.00, 0.0, -1.00 ] ]
136 arrow4 = [ [ -1.00, 0.0, 0.00 ],
137  [ -0.20, 0.0, 0.10 ],
138  [ -0.20, 0.0, -0.10 ],
139  [ -1.00, 0.0, 0.00 ] ]
140 perimeter=[ [ 1.10, 0.0, 1.10 ],
141  [ 1.10, 0.0, -1.10 ],
142  [ -1.10, 0.0, -1.10 ],
143  [ -1.10, 0.0, 1.10 ],
144  [ 1.10, 0.0, 1.10 ] ]
145 
146 kCentreCount = 5
147 kState1Count = 5
148 kState2Count = 5
149 kState3Count = 5
150 kState4Count = 5
151 kArrow1Count = 4
152 kArrow2Count = 4
153 kArrow3Count = 4
154 kArrow4Count = 4
155 kPerimeterCount = 5
156 
157 
158 ########################################################################
159 ########################################################################
160 
161 
162 class swissArmyLocatorManip(OpenMayaMPx.MPxManipContainer):
163  def __init__(self):
164  OpenMayaMPx.MPxManipContainer.__init__(self)
165 
166 
167  self.fCircleSweepManip = OpenMaya.MDagPath()
168  self.fDirectionManip = OpenMaya.MDagPath()
169  self.fDiscManip = OpenMaya.MDagPath()
170  self.fDistanceManip = OpenMaya.MDagPath()
171  self.fFreePointTriadManip = OpenMaya.MDagPath()
172  self.fStateManip = OpenMaya.MDagPath()
173  self.fToggleManip = OpenMaya.MDagPath()
174  self.fRotateManip = OpenMaya.MDagPath()
175  self.fScaleManip = OpenMaya.MDagPath()
176  self.fNodePath = OpenMaya.MDagPath()
177 
178 
179  def createChildren(self):
180  # FreePointTriadManip
181  self.fFreePointTriadManip = self.addFreePointTriadManip("freePointTriadManip", "point")
182  freePointTriadManipFn = OpenMayaUI.MFnFreePointTriadManip(self.fFreePointTriadManip)
183 
184  # DirectionManip
185  self.fDirectionManip = self.addDirectionManip("directionManip", "direction")
186  directionManipFn = OpenMayaUI.MFnDirectionManip(self.fDirectionManip)
187 
188  # ToggleManip
189  self.fToggleManip = self.addToggleManip("toggleManip", "toggle")
190  toggleManipFn = OpenMayaUI.MFnToggleManip(self.fToggleManip)
191 
192  # StateManip
193  self.fStateManip = self.addStateManip("stateManip", "state")
194  stateManipFn = OpenMayaUI.MFnStateManip(self.fStateManip)
195 
196  # DiscManip
197  self.fDiscManip = self.addDiscManip("discManip", "angle")
198  discManipFn = OpenMayaUI.MFnDiscManip(self.fDiscManip)
199 
200  # CircleSweepManip
201  self.fCircleSweepManip = self.addCircleSweepManip("circleSweepManip", "angle")
202  circleSweepManipFn = OpenMayaUI.MFnCircleSweepManip(self.fCircleSweepManip)
203  circleSweepManipFn.setCenterPoint(OpenMaya.MPoint(0, 0, 0))
204  circleSweepManipFn.setNormal(OpenMaya.MVector(0, 1, 0))
205  circleSweepManipFn.setRadius(2.0)
206  circleSweepManipFn.setDrawAsArc(True)
207 
208  # DistanceManip
209  self.fDistanceManip = self.addDistanceManip("distanceManip", "distance")
210  distanceManipFn = OpenMayaUI.MFnDistanceManip(self.fDistanceManip)
211  distanceManipFn.setStartPoint(OpenMaya.MPoint(0, 0, 0))
212  distanceManipFn.setDirection(OpenMaya.MVector(0, 1, 0))
213 
214  # RotateManip
215  self.fRotateManip = self.addRotateManip("RotateManip", "rotation")
216  rotateManipFn = OpenMayaUI.MFnRotateManip(self.fRotateManip)
217 
218  # ScaleManip
219  self.fScaleManip = self.addScaleManip("scaleManip", "scale")
220  scaleManipFn = OpenMayaUI.MFnScaleManip(self.fScaleManip)
221 
222 
223  def connectToDependNode(self, node):
224  # Get the DAG path
225  dagNodeFn = OpenMaya.MFnDagNode(node)
226  dagNodeFn.getPath(self.fNodePath)
227  parentNode = dagNodeFn.parent(0)
228  parentNodeFn = OpenMaya.MFnDagNode(parentNode)
229 
230  # Connect the plugs
231  nodeFn = OpenMaya.MFnDependencyNode()
232  nodeFn.setObject(node)
233 
234  # FreePointTriadManip
235  freePointTriadManipFn = OpenMayaUI.MFnFreePointTriadManip(self.fFreePointTriadManip)
236  try:
237  translationPlug = parentNodeFn.findPlug("t")
238  freePointTriadManipFn.connectToPointPlug(translationPlug)
239  except:
240  pass
241 
242  # DirectionManip
243  directionManipFn = OpenMayaUI.MFnDirectionManip()
244  directionManipFn.setObject(self.fDirectionManip)
245  try:
246  directionPlug = nodeFn.findPlug("arrow2Direction")
247  directionManipFn.connectToDirectionPlug(directionPlug)
248  startPointIndex = directionManipFn.startPointIndex()
249  self.addPlugToManipConversion(startPointIndex)
250  except:
251  pass
252 
253  # DistanceManip
254  distanceManipFn = OpenMayaUI.MFnDistanceManip()
255  distanceManipFn.setObject(self.fDistanceManip)
256  try:
257  sizePlug = nodeFn.findPlug("size")
258  distanceManipFn.connectToDistancePlug(sizePlug)
259  startPointIndex = distanceManipFn.startPointIndex()
260  self.addPlugToManipConversion(startPointIndex)
261  except:
262  pass
263 
264  # CircleSweepManip
265  circleSweepManipFn = OpenMayaUI.MFnCircleSweepManip(self.fCircleSweepManip)
266  try:
267  arrow1AnglePlug = nodeFn.findPlug("arrow1Angle")
268  circleSweepManipFn.connectToAnglePlug(arrow1AnglePlug)
269  centerIndex = circleSweepManipFn.centerIndex()
270  self.addPlugToManipConversion(centerIndex)
271  except:
272  pass
273 
274  # DiscManip
275  discManipFn = OpenMayaUI.MFnDiscManip(self.fDiscManip)
276  try:
277  arrow3AnglePlug = nodeFn.findPlug("arrow3Angle")
278  discManipFn.connectToAnglePlug(arrow3AnglePlug)
279  centerIndex = discManipFn.centerIndex()
280  self.addPlugToManipConversion(centerIndex)
281  except:
282  pass
283 
284  # StateManip
285  stateManipFn = OpenMayaUI.MFnStateManip(self.fStateManip)
286  try:
287  statePlug = nodeFn.findPlug("state")
288  stateManipFn.connectToStatePlug(statePlug)
289  positionIndex = stateManipFn.positionIndex()
290  self.addPlugToManipConversion(positionIndex)
291  except:
292  pass
293 
294  # ToggleManip
295  toggleManipFn = OpenMayaUI.MFnToggleManip(self.fToggleManip)
296  try:
297  togglePlug = nodeFn.findPlug("toggle")
298  toggleManipFn.connectToTogglePlug(togglePlug)
299  startPointIndex = toggleManipFn.startPointIndex()
300  self.addPlugToManipConversion(startPointIndex)
301  except:
302  pass
303 
304  # Determine the transform node for the locator
305  transformPath = OpenMaya.MDagPath(self.fNodePath)
306  transformPath.pop()
307 
308  transformNode = OpenMaya.MFnTransform(transformPath)
309 
310  # RotateManip
311  rotateManipFn = OpenMayaUI.MFnRotateManip(self.fRotateManip)
312  try:
313  rotatePlug = transformNode.findPlug("rotate")
314  rotateManipFn.connectToRotationPlug(rotatePlug)
315  rotateManipFn.displayWithNode(node)
316  except:
317  pass
318 
319  # ScaleManip
320  scaleManipFn = OpenMayaUI.MFnScaleManip(self.fScaleManip)
321  try:
322  scalePlug = transformNode.findPlug("scale")
323  scaleManipFn.connectToScalePlug(scalePlug)
324  scaleManipFn.displayWithNode(node)
325  except:
326  pass
327 
328  self.finishAddingManips()
329  OpenMayaMPx.MPxManipContainer.connectToDependNode(self, node)
330 
331 
332  def draw(self, view, path, style, status):
333  OpenMayaMPx.MPxManipContainer.draw(self, view, path, style, status)
334  view.beginGL()
335  textPos = OpenMaya.MPoint(self.nodeTranslation())
336  view.drawText("Swiss Army Manipulator", textPos, OpenMayaUI.M3dView.kLeft)
337  view.endGL()
338 
339 
340  def plugToManipConversion(self, theIndex):
341  numData = OpenMaya.MFnNumericData()
342  numDataObj = numData.create(OpenMaya.MFnNumericData.k3Float)
343  vec = self.nodeTranslation()
344  numData.setData3Float(vec.x, vec.y, vec.z)
345  manipData = OpenMayaUI.MManipData(numDataObj)
346  return manipData
347 
348 
349  def nodeTranslation(self):
350  dagFn = OpenMaya.MFnDagNode(self.fNodePath)
351  path = OpenMaya.MDagPath()
352  dagFn.getPath(path)
353  path.pop() # pop from the shape to the transform
354  transformFn = OpenMaya.MFnTransform(path)
355  return transformFn.getTranslation(OpenMaya.MSpace.kWorld)
356 
357 
358 ########################################################################
359 ########################################################################
360 
361 class swissArmyLocator(OpenMayaMPx.MPxLocatorNode):
362  aSize = OpenMaya.MObject() # The size of the locator
363  aPoint = OpenMaya.MObject()
364  aPointX = OpenMaya.MObject()
365  aPointY = OpenMaya.MObject()
366  aPointZ = OpenMaya.MObject()
367  aArrow1Angle = OpenMaya.MObject()
368  aArrow2Direction = OpenMaya.MObject()
369  aArrow2DirectionX = OpenMaya.MObject()
370  aArrow2DirectionY = OpenMaya.MObject()
371  aArrow2DirectionZ = OpenMaya.MObject()
372  aArrow3Angle = OpenMaya.MObject()
373  aArrow4Distance = OpenMaya.MObject()
374  aState = OpenMaya.MObject()
375  aToggle = OpenMaya.MObject()
376 
377  def __init__(self):
378  OpenMayaMPx.MPxLocatorNode.__init__(self)
379 
380 
381  def compute(self, plug, data):
382  return OpenMaya.kUnknownParameter
383 
384 
385  def draw(self, view, path, style, status):
386 
387  # Get the size
388  thisNode = self.thisMObject()
389 
390  plug = OpenMaya.MPlug(thisNode, swissArmyLocator.aSize)
391  sizeVal = plug.asMDistance()
392 
393  arrow1AnglePlug = OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow1Angle)
394  arrow1Angle = arrow1AnglePlug.asMAngle()
395  angle1 = -arrow1Angle.asRadians() - 3.1415927/2.0
396 
397  arrow3AnglePlug = OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow3Angle)
398  arrow3Angle = arrow3AnglePlug.asMAngle()
399  angle3 = arrow3Angle.asRadians()
400 
401  statePlug = OpenMaya.MPlug(thisNode, swissArmyLocator.aState)
402  state = statePlug.asInt()
403 
404  togglePlug = OpenMaya.MPlug(thisNode, swissArmyLocator.aToggle)
405  toggle = togglePlug.asBool()
406 
407  directionXPlug = OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow2DirectionX)
408  directionYPlug = OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow2DirectionY)
409  directionZPlug = OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow2DirectionZ)
410  dirX = directionXPlug.asDouble()
411  dirY = directionYPlug.asDouble()
412  dirZ = directionZPlug.asDouble()
413 
414  angle2 = math.atan2(dirZ, dirX)
415  angle2 += 3.1415927
416 
417  multiplier = sizeVal.asCentimeters()
418 
419  view.beginGL()
420 
421  if ((style == OpenMayaUI.M3dView.kFlatShaded) or
422  (style == OpenMayaUI.M3dView.kGouraudShaded)):
423  # Push the color settings
424  glFT.glPushAttrib(OpenMayaRender.MGL_CURRENT_BIT)
425 
426  if (status == OpenMayaUI.M3dView.kActive):
427  view.setDrawColor(13, OpenMayaUI.M3dView.kActiveColors)
428  else:
429  view.setDrawColor(13, OpenMayaUI.M3dView.kDormantColors)
430 
431  if (toggle):
432  if (status == OpenMayaUI.M3dView.kActive):
433  view.setDrawColor(15, OpenMayaUI.M3dView.kActiveColors)
434  else:
435  view.setDrawColor(15, OpenMayaUI.M3dView.kDormantColors)
436  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
437  last = kCentreCount - 1
438  for i in range(last):
439  glFT.glVertex3f(centre[i][0] * multiplier,
440  centre[i][1] * multiplier,
441  centre[i][2] * multiplier)
442  glFT.glEnd()
443 
444  if (state == 0):
445  if (status == OpenMayaUI.M3dView.kActive):
446  view.setDrawColor(19, OpenMayaUI.M3dView.kActiveColors)
447  else:
448  view.setDrawColor(19, OpenMayaUI.M3dView.kDormantColors)
449  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
450  last = kState1Count - 1
451  for i in range(last):
452  glFT.glVertex3f(state1[i][0] * multiplier,
453  state1[i][1] * multiplier,
454  state1[i][2] * multiplier)
455  glFT.glEnd()
456 
457  if (state == 1):
458  if (status == OpenMayaUI.M3dView.kActive):
459  view.setDrawColor(21, OpenMayaUI.M3dView.kActiveColors)
460  else:
461  view.setDrawColor(21, OpenMayaUI.M3dView.kDormantColors)
462  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
463  last = kState2Count - 1
464  for i in range(last):
465  glFT.glVertex3f(state2[i][0] * multiplier,
466  state2[i][1] * multiplier,
467  state2[i][2] * multiplier)
468  glFT.glEnd()
469 
470  if (state == 2):
471  if (status == OpenMayaUI.M3dView.kActive):
472  view.setDrawColor(18, OpenMayaUI.M3dView.kActiveColors)
473  else:
474  view.setDrawColor(18, OpenMayaUI.M3dView.kDormantColors)
475  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
476  last = kState3Count - 1
477  for i in range(last):
478  glFT.glVertex3f(state3[i][0] * multiplier,
479  state3[i][1] * multiplier,
480  state3[i][2] * multiplier)
481  glFT.glEnd()
482 
483  if (state == 3):
484  if (status == OpenMayaUI.M3dView.kActive):
485  view.setDrawColor(17, OpenMayaUI.M3dView.kActiveColors)
486  else:
487  view.setDrawColor(17, OpenMayaUI.M3dView.kDormantColors)
488  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
489  last = kState4Count - 1
490  for i in range(last):
491  glFT.glVertex3f(state4[i][0] * multiplier,
492  state4[i][1] * multiplier,
493  state4[i][2] * multiplier)
494  glFT.glEnd()
495 
496  if (status == OpenMayaUI.M3dView.kActive):
497  view.setDrawColor(12, OpenMayaUI.M3dView.kActiveColors)
498  else:
499  view.setDrawColor(12, OpenMayaUI.M3dView.kDormantColors)
500  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
501  last = kArrow1Count - 1
502  for i in range(last):
503  glFT.glVertex3f((-arrow1[i][0] * multiplier * math.cos(angle1) - arrow1[i][2] * multiplier * math.sin(angle1)),
504  (arrow1[i][1] * multiplier + delta1),
505  (arrow1[i][2] * multiplier * math.cos(angle1) - arrow1[i][0] * multiplier * math.sin(angle1)))
506  glFT.glEnd()
507 
508  if (status == OpenMayaUI.M3dView.kActive):
509  view.setDrawColor(16, OpenMayaUI.M3dView.kActiveColors)
510  else:
511  view.setDrawColor(16, OpenMayaUI.M3dView.kDormantColors)
512  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
513  last = kArrow2Count - 1
514  for i in range(last):
515  glFT.glVertex3f((-arrow2[i][0] * multiplier * math.cos(angle2) - arrow2[i][2] * multiplier * math.sin(angle2)),
516  (arrow2[i][1] * multiplier + delta2),
517  (arrow2[i][2] * multiplier * math.cos(angle2) - arrow2[i][0] * multiplier * math.sin(angle2)))
518  glFT.glEnd()
519 
520  if (status == OpenMayaUI.M3dView.kActive):
521  view.setDrawColor(13, OpenMayaUI.M3dView.kActiveColors)
522  else:
523  view.setDrawColor(13, OpenMayaUI.M3dView.kDormantColors)
524  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
525  last = kArrow3Count - 1
526  for i in range(last):
527  glFT.glVertex3f((-arrow3[i][0] * multiplier * math.cos(angle3) - arrow3[i][2] * multiplier * math.sin(angle3)),
528  (arrow3[i][1] * multiplier + delta3),
529  (arrow3[i][2] * multiplier * math.cos(angle3) - arrow3[i][0] * multiplier * math.sin(angle3)))
530  glFT.glEnd()
531 
532  if (status == OpenMayaUI.M3dView.kActive):
533  view.setDrawColor(5, OpenMayaUI.M3dView.kActiveColors)
534  else:
535  view.setDrawColor(5, OpenMayaUI.M3dView.kDormantColors)
536  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
537  last = kArrow4Count - 1
538  for i in range(last):
539  glFT.glVertex3f((arrow4[i][0] * multiplier),
540  (arrow4[i][1] * multiplier + delta4),
541  (arrow4[i][2] * multiplier))
542  glFT.glEnd()
543 
544  glFT.glPopAttrib()
545 
546  # Draw the outline of the locator
547  glFT.glBegin(OpenMayaRender.MGL_LINES)
548 
549  if toggle:
550  last = kCentreCount - 1
551  for i in range(last):
552  glFT.glVertex3f(centre[i][0] * multiplier,
553  centre[i][1] * multiplier,
554  centre[i][2] * multiplier)
555  glFT.glVertex3f(centre[i+1][0] * multiplier,
556  centre[i+1][1] * multiplier,
557  centre[i+1][2] * multiplier)
558 
559  if (state == 0):
560  last = kState1Count - 1
561  for i in range(last):
562  glFT.glVertex3f(state1[i][0] * multiplier,
563  state1[i][1] * multiplier,
564  state1[i][2] * multiplier)
565  glFT.glVertex3f(state1[i+1][0] * multiplier,
566  state1[i+1][1] * multiplier,
567  state1[i+1][2] * multiplier)
568 
569  if (state == 1):
570  last = kState2Count - 1
571  for i in range(last):
572  glFT.glVertex3f(state2[i][0] * multiplier,
573  state2[i][1] * multiplier,
574  state2[i][2] * multiplier)
575  glFT.glVertex3f(state2[i+1][0] * multiplier,
576  state2[i+1][1] * multiplier,
577  state2[i+1][2] * multiplier)
578 
579  if (state == 2):
580  last = kState3Count - 1
581  for i in range(last):
582  glFT.glVertex3f(state3[i][0] * multiplier,
583  state3[i][1] * multiplier,
584  state3[i][2] * multiplier)
585  glFT.glVertex3f(state3[i+1][0] * multiplier,
586  state3[i+1][1] * multiplier,
587  state3[i+1][2] * multiplier)
588 
589  if (state == 3):
590  last = kState4Count - 1
591  for i in range(last):
592  glFT.glVertex3f(state4[i][0] * multiplier,
593  state4[i][1] * multiplier,
594  state4[i][2] * multiplier)
595  glFT.glVertex3f(state4[i+1][0] * multiplier,
596  state4[i+1][1] * multiplier,
597  state4[i+1][2] * multiplier)
598 
599  last = kArrow1Count - 1
600  for i in range(last):
601  glFT.glVertex3f((-arrow1[i][0] * multiplier * math.cos(angle1) - arrow1[i][2] * multiplier * math.sin(angle1)),
602  (arrow1[i][1] * multiplier + delta1),
603  (arrow1[i][2] * multiplier * math.cos(angle1) - arrow1[i][0] * multiplier * math.sin(angle1)))
604  glFT.glVertex3f((-arrow1[i+1][0] * multiplier * math.cos(angle1) - arrow1[i+1][2] * multiplier * math.sin(angle1)),
605  (arrow1[i+1][1] * multiplier + delta1),
606  (arrow1[i+1][2] * multiplier * math.cos(angle1) - arrow1[i+1][0] * multiplier * math.sin(angle1)))
607 
608  last = kArrow2Count - 1
609  for i in range(last):
610  glFT.glVertex3f((-arrow2[i][0] * multiplier * math.cos(angle2) - arrow2[i][2] * multiplier * math.sin(angle2)),
611  (arrow2[i][1] * multiplier + delta2),
612  (arrow2[i][2] * multiplier * math.cos(angle2) - arrow2[i][0] * multiplier * math.sin(angle2)))
613  glFT.glVertex3f((-arrow2[i+1][0] * multiplier * math.cos(angle2) - arrow2[i+1][2] * multiplier * math.sin(angle2)),
614  (arrow2[i+1][1] * multiplier + delta2),
615  (arrow2[i+1][2] * multiplier * math.cos(angle2) - arrow2[i+1][0] * multiplier * math.sin(angle2)))
616 
617  last = kArrow3Count - 1
618  for i in range(last):
619  glFT.glVertex3f((-arrow3[i][0] * multiplier * math.cos(angle3) - arrow3[i][2] * multiplier * math.sin(angle3)),
620  (arrow3[i][1] * multiplier + delta3),
621  (arrow3[i][2] * multiplier * math.cos(angle3) - arrow3[i][0] * multiplier * math.sin(angle3)))
622  glFT.glVertex3f((-arrow3[i+1][0] * multiplier * math.cos(angle3) - arrow3[i+1][2] * multiplier * math.sin(angle3)),
623  (arrow3[i+1][1] * multiplier + delta3),
624  (arrow3[i+1][2] * multiplier * math.cos(angle3) - arrow3[i+1][0] * multiplier * math.sin(angle3)))
625 
626  last = kArrow4Count - 1
627  for i in range(last):
628  glFT.glVertex3f((arrow4[i][0] * multiplier),
629  (arrow4[i][1] * multiplier + delta4),
630  (arrow4[i][2] * multiplier))
631  glFT.glVertex3f((arrow4[i+1][0] * multiplier),
632  (arrow4[i+1][1] * multiplier + delta4),
633  (arrow4[i+1][2] * multiplier))
634 
635  last = kPerimeterCount - 1
636  for i in range(last):
637  glFT.glVertex3f(perimeter[i][0] * multiplier,
638  perimeter[i][1] * multiplier,
639  perimeter[i][2] * multiplier)
640  glFT.glVertex3f(perimeter[i+1][0] * multiplier,
641  perimeter[i+1][1] * multiplier,
642  perimeter[i+1][2] * multiplier)
643 
644  glFT.glEnd()
645 
646  view.endGL()
647 
648 
649  def isBounded(self):
650  return True
651 
652 
653  def boundingBox(self):
654  thisNode = self.thisMObject()
655  plug = OpenMaya.MPlug(thisNode, swissArmyLocator.aSize)
656  sizeVal = plug.asMDistance()
657 
658  multiplier = sizeVal.asCentimeters()
659 
660  corner1 = OpenMaya.MPoint(-1.1, 0.0, -1.1)
661  corner2 = OpenMaya.MPoint(1.1, 0.0, 1.1)
662 
663  corner1 = corner1 * multiplier
664  corner2 = corner2 * multiplier
665 
666  return OpenMaya.MBoundingBox(corner1, corner2)
667 
668 
669 
670 ########################################################################
671 ########################################################################
672 
673 
674 def locatorCreator():
675  return OpenMayaMPx.asMPxPtr(swissArmyLocator())
676 
677 
678 def locatorInit():
679  unitFn = OpenMaya.MFnUnitAttribute()
680  numericFn = OpenMaya.MFnNumericAttribute()
681 
682  # aSize
683  swissArmyLocator.aSize = unitFn.create("size", "sz", OpenMaya.MFnUnitAttribute.kDistance, 10.0)
684  unitFn.setStorable(True)
685  unitFn.setWritable(True)
686 
687  # aPoint
688  swissArmyLocator.aPointX = numericFn.create("pointX", "ptx", OpenMaya.MFnNumericData.kDouble, 0.0)
689  swissArmyLocator.aPointY = numericFn.create("pointY", "pty", OpenMaya.MFnNumericData.kDouble, 0.0)
690  swissArmyLocator.aPointZ = numericFn.create("pointZ", "ptz", OpenMaya.MFnNumericData.kDouble, 0.0)
691  swissArmyLocator.aPoint = numericFn.create("point", "pt", swissArmyLocator.aPointX, swissArmyLocator.aPointY, swissArmyLocator.aPointZ)
692 
693  # aArrow1Angle
694  swissArmyLocator.aArrow1Angle = unitFn.create("arrow1Angle", "a1a", OpenMaya.MFnUnitAttribute.kAngle, 0.0)
695 
696  # aArrow2Direction
697  swissArmyLocator.aArrow2DirectionX = numericFn.create("arrow2DirectionX", "a2x", OpenMaya.MFnNumericData.kDouble, 1.0)
698  swissArmyLocator.aArrow2DirectionY = numericFn.create("arrow2DirectionY", "a2y", OpenMaya.MFnNumericData.kDouble, 0.0)
699  swissArmyLocator.aArrow2DirectionZ = numericFn.create("arrow2DirectionZ", "a2z", OpenMaya.MFnNumericData.kDouble, 0.0)
700  swissArmyLocator.aArrow2Direction = numericFn.create("arrow2Direction", "dir", swissArmyLocator.aArrow2DirectionX, swissArmyLocator.aArrow2DirectionY, swissArmyLocator.aArrow2DirectionZ)
701 
702  # aArrow3Angle
703  swissArmyLocator.aArrow3Angle = unitFn.create("arrow3Angle", "a3a", OpenMaya.MFnUnitAttribute.kAngle, 0.0)
704  # aArrow4Distance
705  swissArmyLocator.aArrow4Distance = unitFn.create("arrow2Distance", "dis", OpenMaya.MFnUnitAttribute.kDistance, 0.0)
706 
707  # aState
708  swissArmyLocator.aState = numericFn.create("state", "s", OpenMaya.MFnNumericData.kLong, 0)
709 
710  # aToggle
711  swissArmyLocator.aToggle = numericFn.create("toggle", "t", OpenMaya.MFnNumericData.kBoolean, False)
712 
713  swissArmyLocator.addAttribute(swissArmyLocator.aPoint)
714  swissArmyLocator.addAttribute(swissArmyLocator.aArrow1Angle)
715  swissArmyLocator.addAttribute(swissArmyLocator.aArrow2Direction)
716  swissArmyLocator.addAttribute(swissArmyLocator.aArrow3Angle)
717  swissArmyLocator.addAttribute(swissArmyLocator.aArrow4Distance)
718  swissArmyLocator.addAttribute(swissArmyLocator.aState)
719  swissArmyLocator.addAttribute(swissArmyLocator.aToggle)
720  swissArmyLocator.addAttribute(swissArmyLocator.aSize)
721 
722  OpenMayaMPx.MPxManipContainer.addToManipConnectTable(kSwissArmyLocatorId)
723 
724 
725 def locatorManipCreator():
726  return OpenMayaMPx.asMPxPtr(swissArmyLocatorManip())
727 
728 
729 def locatorManipInit():
730  OpenMayaMPx.MPxManipContainer.initialize()
731 
732 
733 # initialize the script plug-in
734 def initializePlugin(mobject):
735  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
736 
737  try:
738  mplugin.registerNode(kSwissArmyLocatorName,
739  kSwissArmyLocatorId,
740  locatorCreator,
741  locatorInit,
742  OpenMayaMPx.MPxNode.kLocatorNode)
743  except:
744  print "Failed to register context command: %s" % kSwissArmyLocatorName
745  raise
746 
747  try:
748  mplugin.registerNode(kSwissArmyLocatorManipName,
749  kSwissArmyLocatorManipId,
750  locatorManipCreator,
751  locatorManipInit,
752  OpenMayaMPx.MPxNode.kManipContainer)
753  except:
754  print "Failed to register node: %s" % kSwissArmyLocatorManipName
755  raise
756 
757 
758 # uninitialize the script plug-in
759 def uninitializePlugin(mobject):
760  mplugin = OpenMayaMPx.MFnPlugin(mobject)
761 
762  try:
763  mplugin.deregisterNode(kSwissArmyLocatorId)
764  except:
765  print "Failed to deregister context command: %s" % kSwissArmyLocatorName
766  raise
767 
768  try:
769  mplugin.deregisterNode(kSwissArmyLocatorManipId)
770  except:
771  print "Failed to deregister node: %s" % kSwissArmyLocatorManipName
772  raise