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