Python API 2.0 Reference
python/api2/py2SwissArmyManip.py
1 ##-
2 ## ==========================================================================
3 ## Copyright 2019 Autodesk, Inc. All rights reserved.
4 ##
5 ## Use of this software is subject to the terms of the Autodesk
6 ## license agreement provided at the time of installation or download,
7 ## or which otherwise accompanies this software in either electronic
8 ## or hard copy form.
9 ## ==========================================================================
10 ##+
11 
12 #
13 # Autodesk Script File
14 # MODIFY THIS AT YOUR OWN RISK
15 #
16 # Creation Date: 11 October 2019
17 #
18 
19 #########################################################################
20 # DESCRIPTION:
21 #
22 # A Python 2.0 version of swissArmyManip.py
23 #
24 # This plug-in is an example of a user-defined manipulator,
25 # which is comprised of a variety of the base manipulators:
26 # - MFnCircleSweepManip
27 # - MFnDirectionManip
28 # - MFnDiscManip
29 # - MFnDistanceManip
30 # - MFnFreePointTriadManip
31 # - MFnStateManip
32 # - MFnToggleManip
33 # - MFnRotateManip
34 # - MFnScaleManip
35 #
36 # It attaches one of every kind of user-defined manipulator to a node.
37 # It demonstrates the source code required to create each user-defined manipulator.
38 #
39 # To use this plug-in:
40 #
41 # (1) Execute these commands:
42 #
43 # import maya.cmds as cmds
44 # cmds.createNode("spSwissArmyLocator2")
45 #
46 # (2) Click the Show Manipulator Tool icon on the side toolbar.
47 # The locator on the screen will be overlaid with one of every kind of user-defined manipulator.
48 #
49 #########################################################################
50 
51 import maya.api.OpenMaya as OpenMaya
52 import maya.api.OpenMayaUI as OpenMayaUI
53 import maya.OpenMayaRender as OpenMayaRender
54 
55 import math,sys
56 import maya.cmds as cmds
57 
58 glRenderer = OpenMayaRender.MHardwareRenderer.theRenderer()
59 glFT = glRenderer.glFunctionTable()
60 
61 def maya_useNewAPI():
62  pass
63 
64 kSwissArmyLocator2Name = "spSwissArmyLocator2"
65 kSwissArmyLocator2Id = OpenMaya.MTypeId(0x87019)
66 kSwissArmyLocator2ManipName = "spSwissArmyLocator2Manip"
67 kSwissArmyLocator2ManipId = OpenMaya.MTypeId(0x8701A)
68 
69 delta1 = 0.01
70 delta2 = 0.02
71 delta3 = 0.03
72 delta4 = 0.04
73 
74 # Locator Data
75 centre = [ [ 0.10, 0.0, 0.10 ],
76  [ 0.10, 0.0, -0.10 ],
77  [ -0.10, 0.0, -0.10 ],
78  [ -0.10, 0.0, 0.10 ],
79  [ 0.10, 0.0, 0.10 ] ]
80 state1 = [ [ 1.00, 0.0, 1.00 ],
81  [ 1.00, 0.0, 0.50 ],
82  [ 0.50, 0.0, 0.50 ],
83  [ 0.50, 0.0, 1.00 ],
84  [ 1.00, 0.0, 1.00 ] ]
85 state2 = [ [ 1.00, 0.0, -1.00 ],
86  [ 1.00, 0.0, -0.50 ],
87  [ 0.50, 0.0, -0.50 ],
88  [ 0.50, 0.0, -1.00 ],
89  [ 1.00, 0.0, -1.00 ] ]
90 state3 = [ [ -1.00, 0.0, -1.00 ],
91  [ -1.00, 0.0, -0.50 ],
92  [ -0.50, 0.0, -0.50 ],
93  [ -0.50, 0.0, -1.00 ],
94  [ -1.00, 0.0, -1.00 ] ]
95 state4 = [ [ -1.00, 0.0, 1.00 ],
96  [ -1.00, 0.0, 0.50 ],
97  [ -0.50, 0.0, 0.50 ],
98  [ -0.50, 0.0, 1.00 ],
99  [ -1.00, 0.0, 1.00 ] ]
100 arrow1 = [ [ 0.00, 0.0, 1.00 ],
101  [ 0.10, 0.0, 0.20 ],
102  [ -0.10, 0.0, 0.20 ],
103  [ 0.00, 0.0, 1.00 ] ]
104 arrow2 = [ [ 1.00, 0.0, 0.00 ],
105  [ 0.20, 0.0, 0.10 ],
106  [ 0.20, 0.0, -0.10 ],
107  [ 1.00, 0.0, 0.00 ] ]
108 arrow3 = [ [ 0.00, 0.0, -1.00 ],
109  [ 0.10, 0.0, -0.20 ],
110  [ -0.10, 0.0, -0.20 ],
111  [ 0.00, 0.0, -1.00 ] ]
112 arrow4 = [ [ -1.00, 0.0, 0.00 ],
113  [ -0.20, 0.0, 0.10 ],
114  [ -0.20, 0.0, -0.10 ],
115  [ -1.00, 0.0, 0.00 ] ]
116 perimeter=[ [ 1.10, 0.0, 1.10 ],
117  [ 1.10, 0.0, -1.10 ],
118  [ -1.10, 0.0, -1.10 ],
119  [ -1.10, 0.0, 1.10 ],
120  [ 1.10, 0.0, 1.10 ] ]
121 
122 kCentreCount = 5
123 kState1Count = 5
124 kState2Count = 5
125 kState3Count = 5
126 kState4Count = 5
127 kArrow1Count = 4
128 kArrow2Count = 4
129 kArrow3Count = 4
130 kArrow4Count = 4
131 kPerimeterCount = 5
132 
133 
134 ########################################################################
135 ########################################################################
136 
137 
138 class swissArmyLocator2Manip(OpenMayaUI.MPxManipContainer):
139  def __init__(self):
141 
142 
143  self.fCircleSweepManip = OpenMaya.MDagPath()
144  self.fDirectionManip = OpenMaya.MDagPath()
145  self.fDiscManip = OpenMaya.MDagPath()
146  self.fDistanceManip = OpenMaya.MDagPath()
147  self.fFreePointTriadManip = OpenMaya.MDagPath()
148  self.fStateManip = OpenMaya.MDagPath()
149  self.fToggleManip = OpenMaya.MDagPath()
150  self.fRotateManip = OpenMaya.MDagPath()
151  self.fScaleManip = OpenMaya.MDagPath()
152  self.fNodePath = OpenMaya.MDagPath()
153 
154 
155  def createChildren(self):
156  # FreePointTriadManip
157  self.fFreePointTriadManip = self.addFreePointTriadManip("freePointTriadManip", "point")
158  freePointTriadManipFn = OpenMayaUI.MFnFreePointTriadManip(self.fFreePointTriadManip)
159 
160  # DirectionManip
161  self.fDirectionManip = self.addDirectionManip("directionManip", "direction")
162  directionManipFn = OpenMayaUI.MFnDirectionManip(self.fDirectionManip)
163  self._DirectionDirectionIndex = directionManipFn.directionIndex()
164  self._DirectionStartPointIndex = directionManipFn.startPointIndex()
165 
166  # ToggleManip
167  self.fToggleManip = self.addToggleManip("toggleManip", "toggle")
168  toggleManipFn = OpenMayaUI.MFnToggleManip(self.fToggleManip)
169  self._ToggleLengthIndex = toggleManipFn.lengthIndex()
170  self._ToggleStartPointIndex = toggleManipFn.startPointIndex()
171 
172  # StateManip
173  self.fStateManip = self.addStateManip("stateManip", "state")
174  stateManipFn = OpenMayaUI.MFnStateManip(self.fStateManip)
175  self._StatePositionIndex = stateManipFn.positionIndex()
176 
177  # DiscManip
178  self.fDiscManip = self.addDiscManip("discManip", "angle")
179  discManipFn = OpenMayaUI.MFnDiscManip(self.fDiscManip)
180  self._DiscCenterIndex = discManipFn.centerIndex()
181 
182  # CircleSweepManip
183  self.fCircleSweepManip = self.addCircleSweepManip("circleSweepManip", "angle")
184  circleSweepManipFn = OpenMayaUI.MFnCircleSweepManip(self.fCircleSweepManip)
185  circleSweepManipFn.setCenterPoint(OpenMaya.MPoint(2, 7, 2))
186  circleSweepManipFn.setNormal(OpenMaya.MVector(0, 1, 0))
187  circleSweepManipFn.setRadius(2.0)
188  circleSweepManipFn.setDrawAsArc(True)
189  self._CircleSweepCenterIndex = circleSweepManipFn.centerIndex()
190 
191  # DistanceManip
192  self.fDistanceManip = self.addDistanceManip("distanceManip", "distance")
193  distanceManipFn = OpenMayaUI.MFnDistanceManip(self.fDistanceManip)
194  distanceManipFn.setStartPoint(OpenMaya.MPoint(0, 0, 0))
195  distanceManipFn.setDirection(OpenMaya.MVector(0, 1, 0))
196  self._DistanceStartPointIndex = distanceManipFn.startPointIndex()
197 
198  # RotateManip
199  self.fRotateManip = self.addRotateManip("RotateManip", "rotation")
200  rotateManipFn = OpenMayaUI.MFnRotateManip(self.fRotateManip)
201 
202  # ScaleManip
203  self.fScaleManip = self.addScaleManip("scaleManip", "scale")
204  scaleManipFn = OpenMayaUI.MFnScaleManip(self.fScaleManip)
205 
206  def connectToDependNode(self, node):
207  # Get the DAG path
208  dagNodeFn = OpenMaya.MFnDagNode(node)
209  self.fNodePath = dagNodeFn.getPath()
210  parentNode = dagNodeFn.parent(0)
211  parentNodeFn = OpenMaya.MFnDagNode(parentNode)
212  parentPath = parentNodeFn.getPath()
213 
214  # Connect the plugs
215  nodeFn = OpenMaya.MFnDependencyNode()
216  nodeFn.setObject(node)
217  self._plugDoubleIndex = -1
218  self._plugMPointIndex = -1
219  self._plugMVectorIndex = -1
220 
221  # FreePointTriadManip
222  freePointTriadManipFn = OpenMayaUI.MFnFreePointTriadManip(self.fFreePointTriadManip)
223  try:
224  translationPlug = parentNodeFn.findPlug("t",True)
225  freePointTriadManipFn.connectToPointPlug(translationPlug)
226  except:
227  pass
228 
229  # DirectionManip
230  directionManipFn = OpenMayaUI.MFnDirectionManip()
231  directionManipFn.setObject(self.fDirectionManip)
232  directionManipFn.setDirection(OpenMaya.MVector(3, 1, 4))
233  try:
234  directionPlug = nodeFn.findPlug("arrow2Direction",True)
235  directionManipFn.connectToDirectionPlug(directionPlug)
236  self.addPlugToManipConversion(self._DirectionStartPointIndex)
237  except:
238  pass
239 
240  try:
241  self.addPlugToManipConversion(self._DirectionDirectionIndex)
242  except:
243  pass
244 
245  # DistanceManip
246  distanceManipFn = OpenMayaUI.MFnDistanceManip()
247  distanceManipFn.setObject(self.fDistanceManip)
248  try:
249  sizePlug = nodeFn.findPlug("size",True)
250  distanceManipFn.connectToDistancePlug(sizePlug)
251  self.addPlugToManipConversion(self._DistanceStartPointIndex)
252  except:
253  pass
254 
255  # CircleSweepManip
256  circleSweepManipFn = OpenMayaUI.MFnCircleSweepManip(self.fCircleSweepManip)
257  try:
258  arrow1AnglePlug = nodeFn.findPlug("arrow1Angle",True)
259  circleSweepManipFn.connectToAnglePlug(arrow1AnglePlug)
260  self.addPlugToManipConversion(self._CircleSweepCenterIndex)
261  except:
262  pass
263 
264  # DiscManip
265  discManipFn = OpenMayaUI.MFnDiscManip(self.fDiscManip)
266  try:
267  arrow3AnglePlug = nodeFn.findPlug("arrow3Angle",True)
268  discManipFn.connectToAnglePlug(arrow3AnglePlug)
269  self.addPlugToManipConversion(self._DiscCenterIndex)
270  except:
271  pass
272 
273  # StateManip
274  stateManipFn = OpenMayaUI.MFnStateManip(self.fStateManip)
275  try:
276  statePlug = nodeFn.findPlug("state",True)
277  stateManipFn.connectToStatePlug(statePlug)
278  self.addPlugToManipConversion(self._StatePositionIndex)
279  except:
280  pass
281 
282  # ToggleManip
283  toggleManipFn = OpenMayaUI.MFnToggleManip(self.fToggleManip)
284  toggleManipFn.length = 3.14
285  try:
286  togglePlug = nodeFn.findPlug("toggle",True)
287  toggleManipFn.connectToTogglePlug(togglePlug)
288  self.addPlugToManipConversion(self._ToggleStartPointIndex)
289  except:
290  pass
291 
292  try:
293  self.addPlugToManipConversion(self._ToggleLengthIndex)
294  except:
295  pass
296 
297  # Determine the transform node for the locator
298  transformPath = OpenMaya.MDagPath(self.fNodePath)
299  transformPath.pop()
300 
301  transformNode = OpenMaya.MFnTransform(transformPath)
302 
303  # RotateManip
304  rotateManipFn = OpenMayaUI.MFnRotateManip(self.fRotateManip)
305  try:
306  rotatePlug = transformNode.findPlug("rotate",True)
307  rotateManipFn.connectToRotationPlug(rotatePlug)
308  rotateManipFn.displayWithNode(node)
309  except:
310  pass
311 
312  # ScaleManip
313  scaleManipFn = OpenMayaUI.MFnScaleManip(self.fScaleManip)
314  try:
315  scalePlug = transformNode.findPlug("scale",True)
316  scaleManipFn.connectToScalePlug(scalePlug)
317  scaleManipFn.displayWithNode(node)
318  except:
319  pass
320 
321  try:
322  mVectorPlug = nodeFn.findPlug("inPlugMVector",True)
323  tmpIndex = self.addManipToPlugConversion(mVectorPlug)
324  self._plugMVectorIndex = tmpIndex
325  except:
326  pass
327 
328  try:
329  mPointPlug = nodeFn.findPlug("inPlugMPoint",True)
330  tmpIndex = self.addManipToPlugConversion(mPointPlug)
331  self._plugMPointIndex = tmpIndex
332  except:
333  pass
334 
335  try:
336  doublePlug = nodeFn.findPlug("inPlugDouble",True)
337  tmpIndex = self.addManipToPlugConversion(doublePlug)
338  self._plugDoubleIndex = tmpIndex
339  except:
340  pass
341 
342  self.finishAddingManips()
344 
345 
346  def draw(self, view, path, style, status):
347  OpenMayaUI.MPxManipContainer.draw(self, view, path, style, status)
348  view.beginGL()
349  textPos = OpenMaya.MPoint(self.nodeTranslation())
350  view.drawText("Swiss Army Manipulator 2", textPos, OpenMayaUI.M3dView.kLeft)
351  view.endGL()
352 
353 
354  def plugToManipConversion(self, theIndex):
355 
356  manipVec = self.getConverterManipMVectorValue(theIndex)
357  numData = OpenMaya.MFnNumericData()
358  numDataObj = numData.create(OpenMaya.MFnNumericData.k3Float)
359  numData.setData(manipVec)
360  manipData = OpenMayaUI.MManipData(numDataObj)
361 
362  # Set some out attributes for testing getConvertManip by a calling
363  # script
364  if theIndex == self._CircleSweepCenterIndex:
365  # Only want to stuff the initial value into the out attribute
366  self._CircleSweepCenterIndex = -1
367  manipMPoint = self.getConverterManipMPointValue(theIndex)
368  x = manipMPoint[0]
369  y = manipMPoint[1]
370  z = manipMPoint[2]
371  a = manipMPoint[3]
372  cmds.setAttr( "%s.outManipMPoint"%(self.fNodePath), x, y, z, a )
373 
374  if theIndex == self._DirectionDirectionIndex:
375  # Only want to stuff the initial value into the out attribute
376  self._DirectionDirectionIndex = -1
377  manipMVector = self.getConverterManipMVectorValue(theIndex)
378  x = manipMVector[0]
379  y = manipMVector[1]
380  z = manipMVector[2]
381  cmds.setAttr( "%s.outManipMVector"%(self.fNodePath), x, y, z )
382 
383  if theIndex == self._ToggleLengthIndex:
384  # Only want to stuff the initial value into the out attribute
385  self._ToggleLengthIndex = -1
386  manipDouble = self.getConverterManipDoubleValue(theIndex)
387  cmds.setAttr( "%s.outManipDouble"%(self.fNodePath), manipDouble )
388  result = OpenMayaUI.MManipData(manipDouble)
389  return result
390 
391  return manipData
392 
393  def manipToPlugConversion(self, plugIndex):
394 
395  plugVec = self.getConverterPlugMVectorValue(plugIndex)
396  numData = OpenMaya.MFnNumericData()
397  numDataObj = numData.create(OpenMaya.MFnNumericData.k3Double)
398  numData.setData(plugVec)
399 
400  manipData = OpenMayaUI.MManipData(numDataObj)
401 
402  # Set some out attributes for testing getConvertPlug by a calling script
403  if plugIndex == self._plugMPointIndex:
404  plugMPoint = self.getConverterPlugMPointValue(self._plugMPointIndex)
405  x = plugMPoint[0]
406  y = plugMPoint[1]
407  z = plugMPoint[2]
408  a = plugMPoint[3]
409  cmds.setAttr( "%s.outPlugMPoint"%(self.fNodePath), x, y, z, a )
410 
411  if plugIndex == self._plugMVectorIndex:
412  plugMVector = self.getConverterPlugMVectorValue(self._plugMVectorIndex)
413  x = plugMVector[0]
414  y = plugMVector[1]
415  z = plugMVector[2]
416  cmds.setAttr( "%s.outPlugMVector"%(self.fNodePath), x, y, z )
417 
418  if plugIndex == self._plugDoubleIndex:
419  plugDouble = self.getConverterPlugDoubleValue(self._plugDoubleIndex)
420  cmds.setAttr( "%s.outPlugDouble"%(self.fNodePath), plugDouble )
421  result = OpenMayaUI.MManipData(plugDouble)
422  return result
423 
424  return manipData
425 
426 
427  def nodeTranslation(self):
428  dagFn = OpenMaya.MFnDagNode(self.fNodePath)
429  path = OpenMaya.MDagPath()
430  path = dagFn.getPath()
431  path.pop() # pop from the shape to the transform
432  transformFn = OpenMaya.MFnTransform(path)
433  return transformFn.translation(OpenMaya.MSpace.kWorld)
434 
435 
436 ########################################################################
437 ########################################################################
438 
439 class swissArmyLocator2(OpenMayaUI.MPxLocatorNode):
440  aSize = OpenMaya.MObject() # The size of the locator
441  aPoint = OpenMaya.MObject()
442  aPointX = OpenMaya.MObject()
443  aPointY = OpenMaya.MObject()
444  aPointZ = OpenMaya.MObject()
445  aArrow1Angle = OpenMaya.MObject()
446  aArrow2Direction = OpenMaya.MObject()
447  aArrow2DirectionX = OpenMaya.MObject()
448  aArrow2DirectionY = OpenMaya.MObject()
449  aArrow2DirectionZ = OpenMaya.MObject()
450 
451  aArrow3Angle = OpenMaya.MObject()
452  aArrow4Distance = OpenMaya.MObject()
453  aState = OpenMaya.MObject()
454  aToggle = OpenMaya.MObject()
455 
456  # The in/out Manip/Plug attributes allow testing of the getConverter
457  # methods from a calling script
458  # In Manips
459  aInManipDouble = OpenMaya.MObject()
460  aInManipMPoint = OpenMaya.MObject()
461  aInManipMPointX = OpenMaya.MObject()
462  aInManipMPointY = OpenMaya.MObject()
463  aInManipMPointZ = OpenMaya.MObject()
464  aInManipMVector = OpenMaya.MObject()
465  aInManipMVectorX = OpenMaya.MObject()
466  aInManipMVectorY = OpenMaya.MObject()
467  aInManipMVectorZ = OpenMaya.MObject()
468 
469  # In Plugs
470  aInPlugDouble = OpenMaya.MObject()
471  aInPlugMPoint = OpenMaya.MObject()
472  aInPlugMPointX = OpenMaya.MObject()
473  aInPlugMPointY = OpenMaya.MObject()
474  aInPlugMPointZ = OpenMaya.MObject()
475  aInPlugMVector = OpenMaya.MObject()
476  aInPlugMVectorX = OpenMaya.MObject()
477  aInPlugMVectorY = OpenMaya.MObject()
478  aInPlugMVectorZ = OpenMaya.MObject()
479 
480  # Out Manips
481  aOutManipDouble = OpenMaya.MObject()
482  aOutManipMPoint = OpenMaya.MObject()
483  aOutManipMPointX = OpenMaya.MObject()
484  aOutManipMPointY = OpenMaya.MObject()
485  aOutManipMPointZ = OpenMaya.MObject()
486  aOutManipMVector = OpenMaya.MObject()
487  aOutManipMVectorX = OpenMaya.MObject()
488  aOutManipMVectorY = OpenMaya.MObject()
489  aOutManipMVectorZ = OpenMaya.MObject()
490 
491  # Out Plugs
492  aOutPlugDouble = OpenMaya.MObject()
493  aOutPlugMPoint = OpenMaya.MObject()
494  aOutPlugMPointX = OpenMaya.MObject()
495  aOutPlugMPointY = OpenMaya.MObject()
496  aOutPlugMPointZ = OpenMaya.MObject()
497  aOutPlugMVector = OpenMaya.MObject()
498  aOutPlugMVectorX = OpenMaya.MObject()
499  aOutPlugMVectorY = OpenMaya.MObject()
500  aOutPlugMVectorZ = OpenMaya.MObject()
501 
502  def __init__(self):
504 
505 
506  def compute(self, plug, data):
507  return None
508 
509 
510  def draw(self, view, path, style, status):
511 
512  # Get the size
513  thisNode = self.thisMObject()
514 
515  plug = OpenMaya.MPlug(thisNode, swissArmyLocator2.aSize)
516  sizeVal = plug.asMDistance()
517 
518  arrow1AnglePlug = OpenMaya.MPlug(thisNode, swissArmyLocator2.aArrow1Angle)
519  arrow1Angle = arrow1AnglePlug.asMAngle()
520  angle1 = -arrow1Angle.asRadians() - 3.1415927/2.0
521 
522  arrow3AnglePlug = OpenMaya.MPlug(thisNode, swissArmyLocator2.aArrow3Angle)
523  arrow3Angle = arrow3AnglePlug.asMAngle()
524  angle3 = arrow3Angle.asRadians()
525 
526  statePlug = OpenMaya.MPlug(thisNode, swissArmyLocator2.aState)
527  state = statePlug.asInt()
528 
529  togglePlug = OpenMaya.MPlug(thisNode, swissArmyLocator2.aToggle)
530  toggle = togglePlug.asBool()
531 
532  directionXPlug = OpenMaya.MPlug(thisNode, swissArmyLocator2.aArrow2DirectionX)
533  directionYPlug = OpenMaya.MPlug(thisNode, swissArmyLocator2.aArrow2DirectionY)
534  directionZPlug = OpenMaya.MPlug(thisNode, swissArmyLocator2.aArrow2DirectionZ)
535  dirX = directionXPlug.asDouble()
536  dirY = directionYPlug.asDouble()
537  dirZ = directionZPlug.asDouble()
538 
539  angle2 = math.atan2(dirZ, dirX)
540  angle2 += 3.1415927
541 
542  multiplier = sizeVal.asCentimeters()
543 
544  view.beginGL()
545 
546  if ((style == OpenMayaUI.M3dView.kFlatShaded) or
547  (style == OpenMayaUI.M3dView.kGouraudShaded)):
548  # Push the color settings
549  glFT.glPushAttrib(OpenMayaRender.MGL_CURRENT_BIT)
550 
551  if (status == OpenMayaUI.M3dView.kActive):
552  view.setDrawColor(13, OpenMayaUI.M3dView.kActiveColors)
553  else:
554  view.setDrawColor(13, OpenMayaUI.M3dView.kDormantColors)
555 
556  if (toggle):
557  if (status == OpenMayaUI.M3dView.kActive):
558  view.setDrawColor(15, OpenMayaUI.M3dView.kActiveColors)
559  else:
560  view.setDrawColor(15, OpenMayaUI.M3dView.kDormantColors)
561  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
562  last = kCentreCount - 1
563  for i in range(last):
564  glFT.glVertex3f(centre[i][0] * multiplier,
565  centre[i][1] * multiplier,
566  centre[i][2] * multiplier)
567  glFT.glEnd()
568 
569  if (state == 0):
570  if (status == OpenMayaUI.M3dView.kActive):
571  view.setDrawColor(19, OpenMayaUI.M3dView.kActiveColors)
572  else:
573  view.setDrawColor(19, OpenMayaUI.M3dView.kDormantColors)
574  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
575  last = kState1Count - 1
576  for i in range(last):
577  glFT.glVertex3f(state1[i][0] * multiplier,
578  state1[i][1] * multiplier,
579  state1[i][2] * multiplier)
580  glFT.glEnd()
581 
582  if (state == 1):
583  if (status == OpenMayaUI.M3dView.kActive):
584  view.setDrawColor(21, OpenMayaUI.M3dView.kActiveColors)
585  else:
586  view.setDrawColor(21, OpenMayaUI.M3dView.kDormantColors)
587  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
588  last = kState2Count - 1
589  for i in range(last):
590  glFT.glVertex3f(state2[i][0] * multiplier,
591  state2[i][1] * multiplier,
592  state2[i][2] * multiplier)
593  glFT.glEnd()
594 
595  if (state == 2):
596  if (status == OpenMayaUI.M3dView.kActive):
597  view.setDrawColor(18, OpenMayaUI.M3dView.kActiveColors)
598  else:
599  view.setDrawColor(18, OpenMayaUI.M3dView.kDormantColors)
600  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
601  last = kState3Count - 1
602  for i in range(last):
603  glFT.glVertex3f(state3[i][0] * multiplier,
604  state3[i][1] * multiplier,
605  state3[i][2] * multiplier)
606  glFT.glEnd()
607 
608  if (state == 3):
609  if (status == OpenMayaUI.M3dView.kActive):
610  view.setDrawColor(17, OpenMayaUI.M3dView.kActiveColors)
611  else:
612  view.setDrawColor(17, OpenMayaUI.M3dView.kDormantColors)
613  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
614  last = kState4Count - 1
615  for i in range(last):
616  glFT.glVertex3f(state4[i][0] * multiplier,
617  state4[i][1] * multiplier,
618  state4[i][2] * multiplier)
619  glFT.glEnd()
620 
621  if (status == OpenMayaUI.M3dView.kActive):
622  view.setDrawColor(12, OpenMayaUI.M3dView.kActiveColors)
623  else:
624  view.setDrawColor(12, OpenMayaUI.M3dView.kDormantColors)
625  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
626  last = kArrow1Count - 1
627  for i in range(last):
628  glFT.glVertex3f((-arrow1[i][0] * multiplier * math.cos(angle1) - arrow1[i][2] * multiplier * math.sin(angle1)),
629  (arrow1[i][1] * multiplier + delta1),
630  (arrow1[i][2] * multiplier * math.cos(angle1) - arrow1[i][0] * multiplier * math.sin(angle1)))
631  glFT.glEnd()
632 
633  if (status == OpenMayaUI.M3dView.kActive):
634  view.setDrawColor(16, OpenMayaUI.M3dView.kActiveColors)
635  else:
636  view.setDrawColor(16, OpenMayaUI.M3dView.kDormantColors)
637  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
638  last = kArrow2Count - 1
639  for i in range(last):
640  glFT.glVertex3f((-arrow2[i][0] * multiplier * math.cos(angle2) - arrow2[i][2] * multiplier * math.sin(angle2)),
641  (arrow2[i][1] * multiplier + delta2),
642  (arrow2[i][2] * multiplier * math.cos(angle2) - arrow2[i][0] * multiplier * math.sin(angle2)))
643  glFT.glEnd()
644 
645  if (status == OpenMayaUI.M3dView.kActive):
646  view.setDrawColor(13, OpenMayaUI.M3dView.kActiveColors)
647  else:
648  view.setDrawColor(13, OpenMayaUI.M3dView.kDormantColors)
649  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
650  last = kArrow3Count - 1
651  for i in range(last):
652  glFT.glVertex3f((-arrow3[i][0] * multiplier * math.cos(angle3) - arrow3[i][2] * multiplier * math.sin(angle3)),
653  (arrow3[i][1] * multiplier + delta3),
654  (arrow3[i][2] * multiplier * math.cos(angle3) - arrow3[i][0] * multiplier * math.sin(angle3)))
655  glFT.glEnd()
656 
657  if (status == OpenMayaUI.M3dView.kActive):
658  view.setDrawColor(5, OpenMayaUI.M3dView.kActiveColors)
659  else:
660  view.setDrawColor(5, OpenMayaUI.M3dView.kDormantColors)
661  glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
662  last = kArrow4Count - 1
663  for i in range(last):
664  glFT.glVertex3f((arrow4[i][0] * multiplier),
665  (arrow4[i][1] * multiplier + delta4),
666  (arrow4[i][2] * multiplier))
667  glFT.glEnd()
668 
669  glFT.glPopAttrib()
670 
671  # Draw the outline of the locator
672  glFT.glBegin(OpenMayaRender.MGL_LINES)
673 
674  if toggle:
675  last = kCentreCount - 1
676  for i in range(last):
677  glFT.glVertex3f(centre[i][0] * multiplier,
678  centre[i][1] * multiplier,
679  centre[i][2] * multiplier)
680  glFT.glVertex3f(centre[i+1][0] * multiplier,
681  centre[i+1][1] * multiplier,
682  centre[i+1][2] * multiplier)
683 
684  if (state == 0):
685  last = kState1Count - 1
686  for i in range(last):
687  glFT.glVertex3f(state1[i][0] * multiplier,
688  state1[i][1] * multiplier,
689  state1[i][2] * multiplier)
690  glFT.glVertex3f(state1[i+1][0] * multiplier,
691  state1[i+1][1] * multiplier,
692  state1[i+1][2] * multiplier)
693 
694  if (state == 1):
695  last = kState2Count - 1
696  for i in range(last):
697  glFT.glVertex3f(state2[i][0] * multiplier,
698  state2[i][1] * multiplier,
699  state2[i][2] * multiplier)
700  glFT.glVertex3f(state2[i+1][0] * multiplier,
701  state2[i+1][1] * multiplier,
702  state2[i+1][2] * multiplier)
703 
704  if (state == 2):
705  last = kState3Count - 1
706  for i in range(last):
707  glFT.glVertex3f(state3[i][0] * multiplier,
708  state3[i][1] * multiplier,
709  state3[i][2] * multiplier)
710  glFT.glVertex3f(state3[i+1][0] * multiplier,
711  state3[i+1][1] * multiplier,
712  state3[i+1][2] * multiplier)
713 
714  if (state == 3):
715  last = kState4Count - 1
716  for i in range(last):
717  glFT.glVertex3f(state4[i][0] * multiplier,
718  state4[i][1] * multiplier,
719  state4[i][2] * multiplier)
720  glFT.glVertex3f(state4[i+1][0] * multiplier,
721  state4[i+1][1] * multiplier,
722  state4[i+1][2] * multiplier)
723 
724  last = kArrow1Count - 1
725  for i in range(last):
726  glFT.glVertex3f((-arrow1[i][0] * multiplier * math.cos(angle1) - arrow1[i][2] * multiplier * math.sin(angle1)),
727  (arrow1[i][1] * multiplier + delta1),
728  (arrow1[i][2] * multiplier * math.cos(angle1) - arrow1[i][0] * multiplier * math.sin(angle1)))
729  glFT.glVertex3f((-arrow1[i+1][0] * multiplier * math.cos(angle1) - arrow1[i+1][2] * multiplier * math.sin(angle1)),
730  (arrow1[i+1][1] * multiplier + delta1),
731  (arrow1[i+1][2] * multiplier * math.cos(angle1) - arrow1[i+1][0] * multiplier * math.sin(angle1)))
732 
733  last = kArrow2Count - 1
734  for i in range(last):
735  glFT.glVertex3f((-arrow2[i][0] * multiplier * math.cos(angle2) - arrow2[i][2] * multiplier * math.sin(angle2)),
736  (arrow2[i][1] * multiplier + delta2),
737  (arrow2[i][2] * multiplier * math.cos(angle2) - arrow2[i][0] * multiplier * math.sin(angle2)))
738  glFT.glVertex3f((-arrow2[i+1][0] * multiplier * math.cos(angle2) - arrow2[i+1][2] * multiplier * math.sin(angle2)),
739  (arrow2[i+1][1] * multiplier + delta2),
740  (arrow2[i+1][2] * multiplier * math.cos(angle2) - arrow2[i+1][0] * multiplier * math.sin(angle2)))
741 
742  last = kArrow3Count - 1
743  for i in range(last):
744  glFT.glVertex3f((-arrow3[i][0] * multiplier * math.cos(angle3) - arrow3[i][2] * multiplier * math.sin(angle3)),
745  (arrow3[i][1] * multiplier + delta3),
746  (arrow3[i][2] * multiplier * math.cos(angle3) - arrow3[i][0] * multiplier * math.sin(angle3)))
747  glFT.glVertex3f((-arrow3[i+1][0] * multiplier * math.cos(angle3) - arrow3[i+1][2] * multiplier * math.sin(angle3)),
748  (arrow3[i+1][1] * multiplier + delta3),
749  (arrow3[i+1][2] * multiplier * math.cos(angle3) - arrow3[i+1][0] * multiplier * math.sin(angle3)))
750 
751  last = kArrow4Count - 1
752  for i in range(last):
753  glFT.glVertex3f((arrow4[i][0] * multiplier),
754  (arrow4[i][1] * multiplier + delta4),
755  (arrow4[i][2] * multiplier))
756  glFT.glVertex3f((arrow4[i+1][0] * multiplier),
757  (arrow4[i+1][1] * multiplier + delta4),
758  (arrow4[i+1][2] * multiplier))
759 
760  last = kPerimeterCount - 1
761  for i in range(last):
762  glFT.glVertex3f(perimeter[i][0] * multiplier,
763  perimeter[i][1] * multiplier,
764  perimeter[i][2] * multiplier)
765  glFT.glVertex3f(perimeter[i+1][0] * multiplier,
766  perimeter[i+1][1] * multiplier,
767  perimeter[i+1][2] * multiplier)
768 
769  glFT.glEnd()
770 
771  view.endGL()
772 
773 
774  def isBounded(self):
775  return True
776 
777 
778  def boundingBox(self):
779  thisNode = self.thisMObject()
780  plug = OpenMaya.MPlug(thisNode, swissArmyLocator2.aSize)
781  sizeVal = plug.asMDistance()
782 
783  multiplier = sizeVal.asCentimeters()
784 
785  corner1 = OpenMaya.MPoint(-1.1, 0.0, -1.1)
786  corner2 = OpenMaya.MPoint(1.1, 0.0, 1.1)
787 
788  corner1 = corner1 * multiplier
789  corner2 = corner2 * multiplier
790 
791  return OpenMaya.MBoundingBox(corner1, corner2)
792 
793 
794 
795 ########################################################################
796 ########################################################################
797 
798 
799 def locatorCreator():
800  return swissArmyLocator2()
801 
802 
803 def locatorInit():
804  unitFn = OpenMaya.MFnUnitAttribute()
805  numericFn = OpenMaya.MFnNumericAttribute()
806 
807  # aSize
808  swissArmyLocator2.aSize = unitFn.create("size", "sz", OpenMaya.MFnUnitAttribute.kDistance, 10.0)
809 
810  # aPoint
811  swissArmyLocator2.aPointX = numericFn.create("pointX", "ptx", OpenMaya.MFnNumericData.kDouble, 0.0)
812  swissArmyLocator2.aPointY = numericFn.create("pointY", "pty", OpenMaya.MFnNumericData.kDouble, 0.0)
813  swissArmyLocator2.aPointZ = numericFn.create("pointZ", "ptz", OpenMaya.MFnNumericData.kDouble, 0.0)
814  swissArmyLocator2.aPoint = numericFn.create("point", "pt", swissArmyLocator2.aPointX, swissArmyLocator2.aPointY, swissArmyLocator2.aPointZ)
815 
816  # aArrow1Angle
817  swissArmyLocator2.aArrow1Angle = unitFn.create("arrow1Angle", "a1a", OpenMaya.MFnUnitAttribute.kAngle, 0.0)
818 
819  # aArrow2Direction
820  swissArmyLocator2.aArrow2DirectionX = numericFn.create("arrow2DirectionX", "a2x", OpenMaya.MFnNumericData.kDouble, 3.0)
821  swissArmyLocator2.aArrow2DirectionY = numericFn.create("arrow2DirectionY", "a2y", OpenMaya.MFnNumericData.kDouble, 1.0)
822  swissArmyLocator2.aArrow2DirectionZ = numericFn.create("arrow2DirectionZ", "a2z", OpenMaya.MFnNumericData.kDouble, 4.0)
823  swissArmyLocator2.aArrow2Direction = numericFn.create("arrow2Direction", "dir", swissArmyLocator2.aArrow2DirectionX, swissArmyLocator2.aArrow2DirectionY, swissArmyLocator2.aArrow2DirectionZ)
824 
825  # aArrow3Angle
826  # aArrow3Angle
827  swissArmyLocator2.aArrow3Angle = unitFn.create("arrow3Angle", "a3a", OpenMaya.MFnUnitAttribute.kAngle, 0.0)
828  # aArrow4Distance
829  swissArmyLocator2.aArrow4Distance = unitFn.create("arrow2Distance", "dis", OpenMaya.MFnUnitAttribute.kDistance, 0.0)
830 
831  # aState
832  swissArmyLocator2.aState = numericFn.create("state", "s", OpenMaya.MFnNumericData.kLong, 0)
833 
834  # aToggle
835  swissArmyLocator2.aToggle = numericFn.create("toggle", "t", OpenMaya.MFnNumericData.kBoolean, False)
836 
837  # The in/out Manip/Plug attributes allow testing of the getConverter
838  # methods from a calling script
839 
840  # aInManipDouble
841  swissArmyLocator2.aInManipDouble = numericFn.create("inManipDouble", "imd", OpenMaya.MFnNumericData.kDouble, 3.14)
842 
843  # aInManipMPoint
844  swissArmyLocator2.aInManipMPointX = numericFn.create("inManipMPointX", "ima", OpenMaya.MFnNumericData.kDouble, 2.0)
845  swissArmyLocator2.aInManipMPointY = numericFn.create("inManipMPointY", "imb", OpenMaya.MFnNumericData.kDouble, 7.0)
846  swissArmyLocator2.aInManipMPointZ = numericFn.create("inManipMPointZ", "imc", OpenMaya.MFnNumericData.kDouble, 2.0)
847  swissArmyLocator2.aInManipMPoint = numericFn.create("inManipMPoint", "imp", swissArmyLocator2.aInManipMPointX, swissArmyLocator2.aInManipMPointY, swissArmyLocator2.aInManipMPointZ)
848 
849  # aInManipMVector
850  swissArmyLocator2.aInManipMVectorX = numericFn.create("inManipMVectorX", "imx", OpenMaya.MFnNumericData.kDouble, 3.0)
851  swissArmyLocator2.aInManipMVectorY = numericFn.create("inManipMVectorY", "imy", OpenMaya.MFnNumericData.kDouble, 1.0)
852  swissArmyLocator2.aInManipMVectorZ = numericFn.create("inManipMVectorZ", "imz", OpenMaya.MFnNumericData.kDouble, 4.0)
853  swissArmyLocator2.aInManipMVector = numericFn.create("inManipMVector", "imv", swissArmyLocator2.aInManipMVectorX, swissArmyLocator2.aInManipMVectorY, swissArmyLocator2.aInManipMVectorZ)
854 
855  # aInPlugDouble
856  swissArmyLocator2.aInPlugDouble = numericFn.create("inPlugDouble", "ipd", OpenMaya.MFnNumericData.kDouble, 2.718)
857 
858  # aInPlugMPoint
859  swissArmyLocator2.aInPlugMPointX = numericFn.create("inPlugMPointX", "ipa", OpenMaya.MFnNumericData.kDouble, 1.0)
860  swissArmyLocator2.aInPlugMPointY = numericFn.create("inPlugMPointY", "ipb", OpenMaya.MFnNumericData.kDouble, 4.0)
861  swissArmyLocator2.aInPlugMPointZ = numericFn.create("inPlugMPointZ", "ipc", OpenMaya.MFnNumericData.kDouble, 1.0)
862  swissArmyLocator2.aInPlugMPoint = numericFn.create("inPlugMPoint", "ipp", swissArmyLocator2.aInPlugMPointX, swissArmyLocator2.aInPlugMPointY, swissArmyLocator2.aInPlugMPointZ)
863 
864  # aInPlugMVector
865  swissArmyLocator2.aInPlugMVectorX = numericFn.create("inPlugMVectorX", "ipx", OpenMaya.MFnNumericData.kDouble, 1.0)
866  swissArmyLocator2.aInPlugMVectorY = numericFn.create("inPlugMVectorY", "ipy", OpenMaya.MFnNumericData.kDouble, 5.0)
867  swissArmyLocator2.aInPlugMVectorZ = numericFn.create("inPlugMVectorZ", "ipz", OpenMaya.MFnNumericData.kDouble, 9.0)
868  swissArmyLocator2.aInPlugMVector = numericFn.create("inPlugMVector", "ipv", swissArmyLocator2.aInPlugMVectorX, swissArmyLocator2.aInPlugMVectorY, swissArmyLocator2.aInPlugMVectorZ)
869 
870  # aOutManipDouble
871  swissArmyLocator2.aOutManipDouble = numericFn.create("outManipDouble", "omd", OpenMaya.MFnNumericData.kDouble, 0.0)
872 
873  # aOutManipMPoint
874  swissArmyLocator2.aOutManipMPointX = numericFn.create("outManipMPointX", "oma", OpenMaya.MFnNumericData.kDouble, 0.0)
875  swissArmyLocator2.aOutManipMPointY = numericFn.create("outManipMPointY", "omb", OpenMaya.MFnNumericData.kDouble, 0.0)
876  swissArmyLocator2.aOutManipMPointZ = numericFn.create("outManipMPointZ", "omc", OpenMaya.MFnNumericData.kDouble, 0.0)
877  swissArmyLocator2.aOutManipMPoint = numericFn.create("outManipMPoint", "omp", swissArmyLocator2.aOutManipMPointX, swissArmyLocator2.aOutManipMPointY, swissArmyLocator2.aOutManipMPointZ)
878 
879  # aOutManipMVector
880  swissArmyLocator2.aOutManipMVectorX = numericFn.create("outManipMVectorX", "omx", OpenMaya.MFnNumericData.kDouble, 0.0)
881  swissArmyLocator2.aOutManipMVectorY = numericFn.create("outManipMVectorY", "omy", OpenMaya.MFnNumericData.kDouble, 0.0)
882  swissArmyLocator2.aOutManipMVectorZ = numericFn.create("outManipMVectorZ", "omz", OpenMaya.MFnNumericData.kDouble, 0.0)
883  swissArmyLocator2.aOutManipMVector = numericFn.create("outManipMVector", "omv", swissArmyLocator2.aOutManipMVectorX, swissArmyLocator2.aOutManipMVectorY, swissArmyLocator2.aOutManipMVectorZ)
884 
885  # aOutPlugDouble
886  swissArmyLocator2.aOutPlugDouble = numericFn.create("outPlugDouble", "opd", OpenMaya.MFnNumericData.kDouble, 0.0)
887 
888  # aOutPlugMPoint
889  swissArmyLocator2.aOutPlugMPointX = numericFn.create("outPlugMPointX", "opa", OpenMaya.MFnNumericData.kDouble, 0.0)
890  swissArmyLocator2.aOutPlugMPointY = numericFn.create("outPlugMPointY", "opb", OpenMaya.MFnNumericData.kDouble, 0.0)
891  swissArmyLocator2.aOutPlugMPointZ = numericFn.create("outPlugMPointZ", "opc", OpenMaya.MFnNumericData.kDouble, 0.0)
892  swissArmyLocator2.aOutPlugMPoint = numericFn.create("outPlugMPoint", "opp", swissArmyLocator2.aOutPlugMPointX, swissArmyLocator2.aOutPlugMPointY, swissArmyLocator2.aOutPlugMPointZ)
893 
894  # aOutPlugMVector
895  swissArmyLocator2.aOutPlugMVectorX = numericFn.create("outPlugMVectorX", "opx", OpenMaya.MFnNumericData.kDouble, 0.0)
896  swissArmyLocator2.aOutPlugMVectorY = numericFn.create("outPlugMVectorY", "opy", OpenMaya.MFnNumericData.kDouble, 0.0)
897  swissArmyLocator2.aOutPlugMVectorZ = numericFn.create("outPlugMVectorZ", "opz", OpenMaya.MFnNumericData.kDouble, 0.0)
898  swissArmyLocator2.aOutPlugMVector = numericFn.create("outPlugMVector", "opv", swissArmyLocator2.aOutPlugMVectorX, swissArmyLocator2.aOutPlugMVectorY, swissArmyLocator2.aOutPlugMVectorZ)
899 
900  swissArmyLocator2.addAttribute(swissArmyLocator2.aPoint)
901  swissArmyLocator2.addAttribute(swissArmyLocator2.aArrow1Angle)
902  swissArmyLocator2.addAttribute(swissArmyLocator2.aArrow2Direction)
903  swissArmyLocator2.addAttribute(swissArmyLocator2.aArrow3Angle)
904  swissArmyLocator2.addAttribute(swissArmyLocator2.aArrow4Distance)
905  swissArmyLocator2.addAttribute(swissArmyLocator2.aState)
906  swissArmyLocator2.addAttribute(swissArmyLocator2.aToggle)
907  swissArmyLocator2.addAttribute(swissArmyLocator2.aSize)
908  swissArmyLocator2.addAttribute(swissArmyLocator2.aInManipDouble)
909  swissArmyLocator2.addAttribute(swissArmyLocator2.aInManipMPoint)
910  swissArmyLocator2.addAttribute(swissArmyLocator2.aInManipMVector)
911  swissArmyLocator2.addAttribute(swissArmyLocator2.aInPlugDouble)
912  swissArmyLocator2.addAttribute(swissArmyLocator2.aInPlugMPoint)
913  swissArmyLocator2.addAttribute(swissArmyLocator2.aInPlugMVector)
914  swissArmyLocator2.addAttribute(swissArmyLocator2.aOutManipDouble)
915  swissArmyLocator2.addAttribute(swissArmyLocator2.aOutManipMPoint)
916  swissArmyLocator2.addAttribute(swissArmyLocator2.aOutManipMVector)
917  swissArmyLocator2.addAttribute(swissArmyLocator2.aOutPlugDouble)
918  swissArmyLocator2.addAttribute(swissArmyLocator2.aOutPlugMPoint)
919  swissArmyLocator2.addAttribute(swissArmyLocator2.aOutPlugMVector)
920 
922 
923 
924 def locatorManipCreator():
925  return swissArmyLocator2Manip()
926 
927 
928 def locatorManipInit():
930 
931 
932 # initialize the script plug-in
933 def initializePlugin(mobject):
934  mplugin = OpenMaya.MFnPlugin(mobject)
935 
936  try:
937  mplugin.registerNode(kSwissArmyLocator2Name,
938  kSwissArmyLocator2Id,
939  locatorCreator,
940  locatorInit,
941  OpenMaya.MPxNode.kLocatorNode)
942  except:
943  print ("Failed to register context command:", kSwissArmyLocator2Name)
944  raise
945 
946  try:
947  mplugin.registerNode(kSwissArmyLocator2ManipName,
948  kSwissArmyLocator2ManipId,
949  locatorManipCreator,
950  locatorManipInit,
951  OpenMaya.MPxNode.kManipContainer)
952  except:
953  print ("Failed to register node:", kSwissArmyLocator2ManipName)
954  raise
955 
956 
957 # uninitialize the script plug-in
958 def uninitializePlugin(mobject):
959  mplugin = OpenMaya.MFnPlugin(mobject)
961 
962  try:
963  mplugin.deregisterNode(kSwissArmyLocator2Id)
964  except:
965  print ("Failed to deregister context command:", kSwissArmyLocator2Name)
966  raise
967 
968  try:
969  mplugin.deregisterNode(kSwissArmyLocator2ManipId)
970  except:
971  print ("Failed to deregister node:", kSwissArmyLocator2ManipName)
972  raise