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