scripted/pyUiDrawManager.py

scripted/pyUiDrawManager.py
1 #-
2 # ===========================================================================
3 # Copyright 2015 Autodesk, Inc. All rights reserved.
4 #
5 # Use of this software is subject to the terms of the Autodesk license
6 # agreement provided at the time of installation or download, or which
7 # otherwise accompanies this software in either electronic or hard copy form.
8 # ===========================================================================
9 #+
10 
11 import sys
12 import maya.api.OpenMaya as om
13 import maya.api.OpenMayaUI as omui
14 import maya.api.OpenMayaRender as omr
15 
16 def maya_useNewAPI():
17  """
18  The presence of this function tells Maya that the plugin produces, and
19  expects to be passed, objects created using the Maya Python API 2.0.
20  """
21  pass
22 
23 kText = 0
24 kLine = 1
25 kPoint = 2
26 kRect = 3
27 kQuad = 4
28 kSphere = 5
29 kCircle = 6
30 kArc = 7
31 
32 class uiDrawManager(omui.MPxLocatorNode):
33  id = om.MTypeId(0x0008002A)
34  drawDbClassification = "drawdb/geometry/uiDrawManager"
35  drawRegistrantId = "uiDrawManagerPlugin"
36 
37  ## ui type attribute
38  aUIType = None
39 
40  ## color
41  aPrimitiveColor = None
42  aPrimitiveTransparency = None
43 
44  ## line width and line style attributes
45  aLineWidth = None
46  aLineStyle = None
47 
48  ## fill attribute
49  aIsFilled = None
50 
51  ## radius attribute
52  aRadius = None
53 
54  ## 2D attributes
55  aDraw2D = None
56  aPosition = None
57 
58  ## Text attributes
59  aTextAlignment = None
60  eTextIncline = None
61  aTextWeight = None
62  aTextStretch = None
63  aTextLine = None
64  aTextBoxSize = None
65  aText = None
66  aTextBoxColor = None
67  aTextBoxTransparency = None
68  aTextFontSize = None
69  aFontFaceName = None
70 
71  ## point attributes
72  aPointSize = None
73 
74  ## line attributes
75  aLineStartPoint = None
76  aLineEndPoint = None
77 
78  ## rect attributes
79  aRectUp = None
80  aRectNormal = None
81  aRectScale = None
82 
83  ## quad attributes
84  aQuadVertex = [None, None, None, None]
85 
86  ## circle attributes
87  aCircleNormal = None
88 
89  ## arc attributes
90  aArcStart = None
91  aArcEnd = None
92  aArcNormal = None
93 
94  fFontList = None
95 
96  def __init__(self):
97  omui.MPxLocatorNode.__init__(self)
98 
99  @staticmethod
100  def creator():
101  return uiDrawManager()
102 
103  @staticmethod
104  def initialize():
105  nAttr = om.MFnNumericAttribute()
106  eAttr = om.MFnEnumAttribute()
107  typedAttr = om.MFnTypedAttribute()
108 
109  ## Add ui type attribute
110  uiDrawManager.aUIType = eAttr.create("uiType", "ut", kText)
111  eAttr.addField("text", kText)
112  eAttr.addField("line", kLine)
113  eAttr.addField("point", kPoint)
114  eAttr.addField("rect", kRect)
115  eAttr.addField("quad", kQuad)
116  eAttr.addField("sphere", kSphere)
117  eAttr.addField("circle", kCircle)
118  eAttr.addField("arc", kArc)
119  om.MPxNode.addAttribute(uiDrawManager.aUIType)
120 
121  ## Add color attribute
122  uiDrawManager.aPrimitiveColor = nAttr.create("primitiveColor", "pc", om.MFnNumericData.k3Float)
123  nAttr.default = (1.0, 0.0, 0.0)
124  nAttr.usedAsColor = True
125  om.MPxNode.addAttribute(uiDrawManager.aPrimitiveColor)
126 
127  ## Add transparency attribute
128  uiDrawManager.aPrimitiveTransparency = nAttr.create("primitiveTransparency", "pt", om.MFnNumericData.kFloat, 0.0)
129  nAttr.setSoftMin(0.0)
130  nAttr.setSoftMax(1.0)
131  om.MPxNode.addAttribute(uiDrawManager.aPrimitiveTransparency)
132 
133  ## add line width and line style attributes
134  uiDrawManager.aLineWidth = nAttr.create("lineWidth", "lw", om.MFnNumericData.kFloat, 2.0)
135  om.MPxNode.addAttribute(uiDrawManager.aLineWidth)
136 
137  uiDrawManager.aLineStyle = eAttr.create("lineStyle", "ls", omr.MUIDrawManager.kSolid)
138  eAttr.addField("solid", omr.MUIDrawManager.kSolid)
139  eAttr.addField("shortdotted", omr.MUIDrawManager.kShortDotted)
140  eAttr.addField("shortdashed", omr.MUIDrawManager.kShortDashed)
141  eAttr.addField("dashed", omr.MUIDrawManager.kDashed)
142  eAttr.addField("dotted", omr.MUIDrawManager.kDotted)
143  om.MPxNode.addAttribute(uiDrawManager.aLineStyle)
144 
145  ## Add filled attribute
146  uiDrawManager.aIsFilled = nAttr.create("isFilled", "if", om.MFnNumericData.kBoolean, 0)
147  om.MPxNode.addAttribute(uiDrawManager.aIsFilled)
148 
149  ## Add radius attribute
150  uiDrawManager.aRadius = nAttr.create("radius", "ra", om.MFnNumericData.kDouble, 1.0)
151  om.MPxNode.addAttribute(uiDrawManager.aRadius)
152 
153  ## add 2D attributes
154  uiDrawManager.aDraw2D = nAttr.create("draw2D", "d2", om.MFnNumericData.kBoolean, 0)
155  om.MPxNode.addAttribute(uiDrawManager.aDraw2D)
156 
157  uiDrawManager.aPosition = nAttr.create("position", "pos", om.MFnNumericData.k3Double)
158  nAttr.default = (0.0, 0.0, 0.001)
159  om.MPxNode.addAttribute(uiDrawManager.aPosition)
160 
161  ## Add text attributes.
162  stringFn = om.MFnStringData()
163  defaultText = stringFn.create("uiDrawManager-Text")
164  uiDrawManager.aText = typedAttr.create("text", "t", om.MFnData.kString, defaultText)
165  om.MPxNode.addAttribute(uiDrawManager.aText)
166 
167  uiDrawManager.aTextFontSize = nAttr.create("textFontSize", "tfs", om.MFnNumericData.kInt, omr.MUIDrawManager.kDefaultFontSize)
168  nAttr.setMin(-1)
169  nAttr.setMax(99)
170  om.MPxNode.addAttribute(uiDrawManager.aTextFontSize)
171 
172  uiDrawManager.fFontList = omr.MUIDrawManager.getFontList()
173  if len(uiDrawManager.fFontList) == 0:
174  sys.stderr.write("No font available!\n")
175 
176  uiDrawManager.aFontFaceName = eAttr.create("fontFaceName", "ffn", 0)
177  for i in range(len(uiDrawManager.fFontList)):
178  faceName = uiDrawManager.fFontList[i]
179  eAttr.addField(faceName, i)
180  om.MPxNode.addAttribute(uiDrawManager.aFontFaceName)
181 
182  uiDrawManager.aTextAlignment = eAttr.create("textAlignment", "ta", omr.MUIDrawManager.kLeft)
183  eAttr.addField("left", omr.MUIDrawManager.kLeft)
184  eAttr.addField("center", omr.MUIDrawManager.kCenter)
185  eAttr.addField("right", omr.MUIDrawManager.kRight)
186  om.MPxNode.addAttribute(uiDrawManager.aTextAlignment)
187 
188  uiDrawManager.eTextIncline = eAttr.create("textIncline", "tic", omr.MUIDrawManager.kInclineNormal)
189  eAttr.addField("normal", omr.MUIDrawManager.kInclineNormal)
190  eAttr.addField("italic", omr.MUIDrawManager.kInclineItalic)
191  om.MPxNode.addAttribute(uiDrawManager.eTextIncline)
192 
193  uiDrawManager.aTextWeight = eAttr.create("textWeight", "tw", omr.MUIDrawManager.kWeightBold)
194  eAttr.addField("light", omr.MUIDrawManager.kWeightLight)
195  eAttr.addField("bold", omr.MUIDrawManager.kWeightBold)
196  om.MPxNode.addAttribute(uiDrawManager.aTextWeight)
197 
198  uiDrawManager.aTextStretch = nAttr.create("textStretch", "ts", om.MFnNumericData.kInt, omr.MUIDrawManager.kStretchUnstretched)
199  nAttr.setMin(50)
200  nAttr.setMax(200)
201  om.MPxNode.addAttribute(uiDrawManager.aTextStretch)
202 
203  uiDrawManager.aTextLine = eAttr.create("textLine", "tl", 0)
204  eAttr.addField("none", 0)
205  eAttr.addField("overline", omr.MUIDrawManager.kLineOverline)
206  eAttr.addField("underline", omr.MUIDrawManager.kLineUnderline)
207  eAttr.addField("strikeout", omr.MUIDrawManager.kLineStrikeoutLine)
208  om.MPxNode.addAttribute(uiDrawManager.aTextLine)
209 
210  uiDrawManager.aTextBoxSize = nAttr.create("textBoxSize", "tbs", om.MFnNumericData.k2Int)
211  nAttr.default = (0, 0)
212  om.MPxNode.addAttribute(uiDrawManager.aTextBoxSize)
213 
214  uiDrawManager.aTextBoxColor = nAttr.create("textBoxColor", "tbc", om.MFnNumericData.k3Float)
215  nAttr.default = (0.0, 1.0, 1.0)
216  nAttr.usedAsColor = True
217  om.MPxNode.addAttribute(uiDrawManager.aTextBoxColor)
218 
219  uiDrawManager.aTextBoxTransparency = nAttr.create("textBoxTransparency", "tbt", om.MFnNumericData.kFloat, 0.0)
220  nAttr.setSoftMin(0.0)
221  nAttr.setSoftMax(1.0)
222  om.MPxNode.addAttribute(uiDrawManager.aTextBoxTransparency)
223 
224  ## add point attributes
225  uiDrawManager.aPointSize = nAttr.create("pointSize", "ps", om.MFnNumericData.kFloat, 2.0)
226  om.MPxNode.addAttribute(uiDrawManager.aPointSize)
227 
228  ## add line attributes
229  uiDrawManager.aLineStartPoint = nAttr.create("lineStartPoint", "lsp", om.MFnNumericData.k3Double)
230  nAttr.default = (0.0, 0.0, 0.0)
231  om.MPxNode.addAttribute(uiDrawManager.aLineStartPoint)
232 
233  uiDrawManager.aLineEndPoint = nAttr.create("lineEndPoint", "lep", om.MFnNumericData.k3Double)
234  nAttr.default = (1.0, 1.0, 1.0)
235  om.MPxNode.addAttribute(uiDrawManager.aLineEndPoint)
236 
237  ## add rect attributes
238  uiDrawManager.aRectUp = nAttr.create("rectUp", "ru", om.MFnNumericData.k3Double)
239  nAttr.default = (0.0, 1.0, 0.0)
240  om.MPxNode.addAttribute(uiDrawManager.aRectUp)
241 
242  uiDrawManager.aRectNormal = nAttr.create("rectNormal", "rn", om.MFnNumericData.k3Double)
243  nAttr.default = (0.0, 0.0, 1.0)
244  om.MPxNode.addAttribute(uiDrawManager.aRectNormal)
245 
246  uiDrawManager.aRectScale = nAttr.create("rectScale", "rs", om.MFnNumericData.k2Double)
247  nAttr.default = (1.0, 1.0)
248  om.MPxNode.addAttribute(uiDrawManager.aRectScale)
249 
250  ## add quad attributes
251  defaultPosition = [ (0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0) ]
252  for i in range(4):
253  fullName = "quadVertex" + str(i)
254  shortName = "qv" + str(i)
255  uiDrawManager.aQuadVertex[i] = nAttr.create(fullName, shortName, om.MFnNumericData.k3Double)
256  nAttr.default = defaultPosition[i]
257  om.MPxNode.addAttribute(uiDrawManager.aQuadVertex[i])
258 
259  ## add circle attributes
260  uiDrawManager.aCircleNormal = nAttr.create("circleNormal", "cn", om.MFnNumericData.k3Double)
261  nAttr.default = (0.0, 0.0, 1.0)
262  om.MPxNode.addAttribute(uiDrawManager.aCircleNormal)
263 
264  ## add arc attributes
265  uiDrawManager.aArcStart = nAttr.create("arcStartVector", "asv", om.MFnNumericData.k3Double)
266  nAttr.default = (1.0, 0.0, 0.0)
267  om.MPxNode.addAttribute(uiDrawManager.aArcStart)
268 
269  uiDrawManager.aArcEnd = nAttr.create("arcEndVector", "aev", om.MFnNumericData.k3Double)
270  nAttr.default = (0.0, 1.0, 0.0)
271  om.MPxNode.addAttribute(uiDrawManager.aArcEnd)
272 
273  uiDrawManager.aArcNormal = nAttr.create("arcNormal", "an", om.MFnNumericData.k3Double)
274  nAttr.default = (0.0, 0.0, 1.0)
275  om.MPxNode.addAttribute(uiDrawManager.aArcNormal)
276 
277 
278 ##---------------------------------------------------------------------------
279 ##---------------------------------------------------------------------------
280 ## Viewport 2.0 override implementation
281 ##---------------------------------------------------------------------------
282 ##---------------------------------------------------------------------------
283 class uiDrawManagerData(om.MUserData):
284  def __init__(self):
285  om.MUserData.__init__(self, False)
286  self.fUIType = kText
287  self.fColor = om.MColor((1., 0., 0., 1.))
288  self.fLineWidth = 2
289  self.fLineStyle = omr.MUIDrawManager.kSolid
290  self.fIsFilled = False
291  self.fRadius = 1.
292  self.fDraw2D = False
293  self.fPosition = om.MPoint(0.0, 0.0, 0.001)
294  # text
295  self.fText = "uiDrawManager-Text"
296  self.fTextFontSize = omr.MUIDrawManager.kDefaultFontSize
297  self.fFontFaceIndex = 0
298  self.fTextAlignment = omr.MUIDrawManager.kLeft
299  self.fTextIncline = omr.MUIDrawManager.kInclineNormal
300  self.fTextWeight = omr.MUIDrawManager.kWeightBold
301  self.fTextStretch = omr.MUIDrawManager.kStretchUnstretched
302  self.fTextLine = 0
303  self.fTextBoxSize = [0, 0]
304  self.fTextBoxColor = om.MColor((0., 1., 1., 1.))
305  # point
306  self.fPointSize = 2.
307  # line
308  self.fLineStartPoint = om.MPoint(0.0, 0.0, 0.0)
309  self.fLineEndPoint = om.MPoint(1.0, 1.0, 1.0)
310  # rect
311  self.fRectUp = om.MVector(0.0, 1.0, 0.0)
312  self.fRectNormal = om.MVector(0.0, 0.0, 1.0)
313  self.fRectScale = [1., 1.]
314  # quad
315  self.fQuadVertex = [ om.MPoint(0.0, 0.0, 0.0), om.MPoint(1.0, 0.0, 0.0), om.MPoint(1.0, 1.0, 0.0), om.MPoint(0.0, 1.0, 0.0) ]
316  # circle
317  self.fCircleNormal = om.MVector(0.0, 0.0, 1.0)
318  # arc
319  self.fArcStart = om.MVector(1.0, 0.0, 0.0)
320  self.fArcEnd = om.MVector(0.0, 1.0, 0.0)
321  self.fArcNormal = om.MVector(0.0, 0.0, 1.0)
322 
323 
324 ################################################################################################
325 class uiDrawManagerDrawOverride(omr.MPxDrawOverride):
326  def __init__(self, obj):
327  omr.MPxDrawOverride.__init__(self, obj, uiDrawManagerDrawOverride.draw)
328 
329  @staticmethod
330  def creator(obj):
331  return uiDrawManagerDrawOverride(obj)
332 
333  @staticmethod
334  def draw(context, data):
335  pass
336 
337  def supportedDrawAPIs(self):
338  return (omr.MRenderer.kOpenGL | omr.MRenderer.kDirectX11)
339 
340  def isBounded(self, objPath, cameraPath):
341  return False
342 
343  def boundingBox(self, objPath, cameraPath):
344  return om.MBoundingBox()
345 
346  def prepareForDraw(self, objPath, cameraPath, frameContext, oldData):
347  data = oldData
348  if not isinstance(data, uiDrawManagerData):
349  data = uiDrawManagerData()
350 
351  uiDrawManagerNode = objPath.node()
352  if uiDrawManagerNode.isNull():
353  return
354 
355  ## retrieve uiType
356  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aUIType)
357  data.fUIType = plug.asShort()
358 
359  ## common attributes
360  ## retrieve color
361  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aPrimitiveColor)
362  o = plug.asMObject()
363  nData = om.MFnNumericData(o)
364  data.fColor = om.MColor(nData.getData())
365 
366  ## retrieve transparency
367  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aPrimitiveTransparency)
368  data.fColor.a = 1.0 - plug.asFloat()
369 
370  ## retrieve line width
371  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aLineWidth)
372  data.fLineWidth = plug.asFloat()
373 
374  ## retrieve line style
375  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aLineStyle)
376  data.fLineStyle = plug.asShort()
377 
378  ## retrieve filled flag
379  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aIsFilled)
380  data.fIsFilled = plug.asBool()
381 
382  ## retrieve radius
383  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aRadius)
384  data.fRadius = plug.asDouble()
385 
386  ## retrieve 2D flag
387  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aDraw2D)
388  data.fDraw2D = plug.asBool()
389 
390  ## retrieve screen position
391  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aPosition)
392  o = plug.asMObject()
393  nData = om.MFnNumericData(o)
394  data.fPosition = om.MPoint(nData.getData())
395  data.fPosition.w = 1.0
396 
397  if data.fUIType == kText:
398  ## retrieve text
399  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aText)
400  data.fText = plug.asString()
401 
402  ## retrieve text font size
403  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextFontSize)
404  data.fTextFontSize = max(plug.asInt(), 0)
405 
406  ## retrieve font face index
407  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aFontFaceName)
408  data.fFontFaceIndex = plug.asInt()
409 
410  ## retrieve text alignment
411  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextAlignment)
412  data.fTextAlignment = plug.asShort()
413 
414  ## retrieve text incline
415  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.eTextIncline)
416  data.fTextIncline = plug.asInt()
417 
418  ## retrieve text weight
419  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextWeight)
420  data.fTextWeight = plug.asInt()
421 
422  ## retrieve text stretch
423  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextStretch)
424  data.fTextStretch = plug.asInt()
425 
426  ## retrieve text line
427  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextLine)
428  data.fTextLine = plug.asInt()
429 
430  ## retrieve text box size
431  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextBoxSize)
432  o = plug.asMObject()
433  nData = om.MFnNumericData(o)
434  data.fTextBoxSize = nData.getData()
435 
436  ## retrieve text box color
437  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextBoxColor)
438  o = plug.asMObject()
439  nData = om.MFnNumericData(o)
440  data.fTextBoxColor = om.MColor(nData.getData())
441 
442  ## retrieve text box transparency
443  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextBoxTransparency)
444  data.fTextBoxColor.a = 1.0 - plug.asFloat()
445 
446  elif data.fUIType == kPoint:
447  ## retrieve point size
448  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aPointSize)
449  data.fPointSize = plug.asFloat()
450 
451  elif data.fUIType == kLine:
452  ## retrieve line start point
453  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aLineStartPoint)
454  o = plug.asMObject()
455  nData = om.MFnNumericData(o)
456  data.fLineStartPoint = om.MPoint(nData.getData())
457  data.fLineStartPoint.w = 1.0
458 
459  ## retrieve line end point
460  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aLineEndPoint)
461  o = plug.asMObject()
462  nData = om.MFnNumericData(o)
463  data.fLineEndPoint = om.MPoint(nData.getData())
464  data.fLineEndPoint.w = 1.0
465 
466  elif data.fUIType == kRect:
467  ## retrieve rect up vector
468  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aRectUp)
469  o = plug.asMObject()
470  nData = om.MFnNumericData(o)
471  data.fRectUp = om.MVector(nData.getData())
472 
473  ## retrieve rect normal vector
474  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aRectNormal)
475  o = plug.asMObject()
476  nData = om.MFnNumericData(o)
477  data.fRectNormal = om.MVector(nData.getData())
478 
479  ## retrieve rect scale
480  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aRectScale)
481  o = plug.asMObject()
482  nData = om.MFnNumericData(o)
483  data.fRectScale = nData.getData()
484 
485  elif data.fUIType == kQuad:
486  ## retrieve quad vertices
487  for i in range(4):
488  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aQuadVertex[i])
489  o = plug.asMObject()
490  nData = om.MFnNumericData(o)
491  data.fQuadVertex[i] = om.MPoint(nData.getData())
492 
493  elif data.fUIType == kCircle:
494  ## retrieve circle normal
495  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aCircleNormal)
496  o = plug.asMObject()
497  nData = om.MFnNumericData(o)
498  data.fCircleNormal = om.MVector(nData.getData())
499 
500  elif data.fUIType == kArc:
501  ## retrieve arc start vector
502  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aArcStart)
503  o = plug.asMObject()
504  nData = om.MFnNumericData(o)
505  data.fArcStart = om.MVector(nData.getData())
506 
507  ## retrieve arc end vector
508  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aArcEnd)
509  o = plug.asMObject()
510  nData = om.MFnNumericData(o)
511  data.fArcEnd = om.MVector(nData.getData())
512 
513  ## retrieve arc normal
514  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aArcNormal)
515  o = plug.asMObject()
516  nData = om.MFnNumericData(o)
517  data.fArcNormal = om.MVector(nData.getData())
518 
519  elif data.fUIType == kSphere:
520  pass
521 
522  else:
523  sys.stderr.write("unhandled ui type.\n")
524 
525  return data
526 
527  def hasUIDrawables(self):
528  return True
529 
530  def addUIDrawables(self, objPath, drawManager, frameContext, data):
531  if not isinstance(data, uiDrawManagerData):
532  return
533 
534  if data.fUIType == kText:
535  ## Draw a text "uiDrawManager"
536  ## All drawing operations must take place between calls to beginDrawable()
537  ## and endDrawable().
538  drawManager.beginDrawable()
539 
540  drawManager.setColor(data.fColor)
541  drawManager.setFontSize(data.fTextFontSize)
542  drawManager.setFontIncline(data.fTextIncline)
543  drawManager.setFontWeight(data.fTextWeight)
544  drawManager.setFontStretch(data.fTextStretch)
545  drawManager.setFontLine(data.fTextLine)
546 
547  faceName = uiDrawManager.fFontList[data.fFontFaceIndex]
548  drawManager.setFontName(faceName)
549 
550  boxSize = data.fTextBoxSize
551  if boxSize[0] + boxSize[1] == 0:
552  boxSize = None
553 
554  if data.fDraw2D:
555  ## uiDrawManagerData::fPosition gives a screen space position
556  ## where 2D UI item is located.
557  drawManager.text2d(data.fPosition, data.fText, data.fTextAlignment, boxSize, data.fTextBoxColor, False)
558 
559  else:
560  ## for 3D items, place it at the origin of the world space.
561  drawManager.text(data.fPosition, data.fText, data.fTextAlignment, boxSize, data.fTextBoxColor, False)
562 
563  drawManager.endDrawable()
564 
565  elif data.fUIType == kLine:
566  drawManager.beginDrawable()
567 
568  drawManager.setColor(data.fColor)
569  drawManager.setLineWidth(data.fLineWidth)
570  drawManager.setLineStyle(data.fLineStyle)
571 
572  if data.fDraw2D:
573  drawManager.line2d(data.fLineStartPoint, data.fLineEndPoint)
574 
575  else:
576  drawManager.line(data.fLineStartPoint, data.fLineEndPoint)
577 
578  drawManager.endDrawable()
579 
580  elif data.fUIType == kPoint:
581  drawManager.beginDrawable()
582 
583  drawManager.setColor(data.fColor)
584  drawManager.setPointSize(data.fPointSize)
585 
586  if data.fDraw2D:
587  drawManager.point2d(data.fPosition)
588 
589  else:
590  drawManager.point(data.fPosition)
591 
592  drawManager.endDrawable()
593 
594  elif data.fUIType == kRect:
595  drawManager.beginDrawable()
596 
597  drawManager.setColor(data.fColor)
598  drawManager.setLineWidth(data.fLineWidth)
599  drawManager.setLineStyle(data.fLineStyle)
600 
601  if data.fDraw2D:
602  ## For 2d rectangle, an up vector in screen space is used to determine its X
603  ## and Y directions. In addition, "fRectScale" (in pixels)
604  ## specify the half-lengths of the 2d rectangle.
605  drawManager.rect2d(data.fPosition, data.fRectUp, data.fRectScale[0], data.fRectScale[1], data.fIsFilled)
606 
607  else:
608  ## For 3d rectangle, the up vector should not be parallel with the normal vector.
609  drawManager.rect(data.fPosition, data.fRectUp, data.fRectNormal, data.fRectScale[0], data.fRectScale[1], data.fIsFilled)
610 
611  drawManager.endDrawable()
612 
613  elif data.fUIType == kQuad:
614  pass
615  drawManager.beginDrawable()
616 
617  drawManager.setColor(data.fColor)
618  drawManager.setLineWidth(data.fLineWidth)
619  drawManager.setLineStyle(data.fLineStyle)
620 
621  ## prepare primitive type
622  mode = omr.MUIDrawManager.kClosedLine
623  if data.fIsFilled:
624  mode = omr.MUIDrawManager.kTriStrip
625 
626  ## prepare position list and index
627  position = om.MPointArray()
628  index = om.MUintArray()
629  for i in range(4):
630  position.append(data.fQuadVertex[i])
631  index.append(i)
632 
633  if not data.fIsFilled:
634  index = None
635 
636  ## draw mesh
637  if data.fDraw2D:
638  drawManager.mesh2d(mode, position, None, index)
639 
640  else:
641  drawManager.mesh(mode, position, None, None, index)
642 
643  drawManager.endDrawable()
644 
645  elif data.fUIType == kSphere:
646  drawManager.beginDrawable()
647 
648  drawManager.setColor(data.fColor)
649  drawManager.setLineWidth(data.fLineWidth)
650  drawManager.setLineStyle(data.fLineStyle)
651 
652  drawManager.sphere(data.fPosition, data.fRadius, data.fIsFilled)
653 
654  drawManager.endDrawable()
655 
656  elif data.fUIType == kCircle:
657  drawManager.beginDrawable()
658 
659  drawManager.setColor(data.fColor)
660  drawManager.setLineWidth(data.fLineWidth)
661  drawManager.setLineStyle(data.fLineStyle)
662 
663  if data.fDraw2D:
664  ## The radius in specified as pixel unit for 2d items.
665  drawManager.circle2d(data.fPosition, data.fRadius, data.fIsFilled)
666 
667  else:
668  drawManager.circle(data.fPosition, data.fCircleNormal, data.fRadius, data.fIsFilled)
669 
670  drawManager.endDrawable()
671 
672  elif data.fUIType == kArc:
673  drawManager.beginDrawable()
674 
675  drawManager.setColor(data.fColor)
676  drawManager.setLineWidth(data.fLineWidth)
677  drawManager.setLineStyle(data.fLineStyle)
678 
679  if data.fDraw2D:
680  ## If 2d, the range of the arc is defined by the start and end vectors
681  ## specified in screen space.
682  drawManager.arc2d(data.fPosition, data.fArcStart, data.fArcEnd, data.fRadius, data.fIsFilled)
683 
684  else:
685  ## For 3d arc, the projections of the start and end vectors onto the arc plane(
686  ## determined by the normal vector) determine the range of the arc.
687  drawManager.arc(data.fPosition, data.fArcStart, data.fArcEnd, data.fArcNormal, data.fRadius, data.fIsFilled)
688 
689  drawManager.endDrawable()
690 
691  else:
692  sys.stderr.write("unhandled ui type.\n")
693 
694 
695 def initializePlugin(obj):
696  plugin = om.MFnPlugin(obj, "Autodesk", "3.0", "Any")
697  try:
698  plugin.registerNode("uiDrawManager", uiDrawManager.id, uiDrawManager.creator, uiDrawManager.initialize, om.MPxNode.kLocatorNode, uiDrawManager.drawDbClassification)
699  except:
700  sys.stderr.write("Failed to register node\n")
701  raise
702 
703  try:
704  omr.MDrawRegistry.registerDrawOverrideCreator(uiDrawManager.drawDbClassification, uiDrawManager.drawRegistrantId, uiDrawManagerDrawOverride.creator)
705  except:
706  sys.stderr.write("Failed to register override\n")
707  raise
708 
709 def uninitializePlugin(obj):
710  plugin = om.MFnPlugin(obj)
711  try:
712  plugin.deregisterNode(uiDrawManager.id)
713  except:
714  sys.stderr.write("Failed to deregister node\n")
715  raise
716 
717  try:
718  omr.MDrawRegistry.deregisterGeometryOverrideCreator(uiDrawManager.drawDbClassification, uiDrawManager.drawRegistrantId)
719  except:
720  sys.stderr.write("Failed to deregister override\n")
721  raise
722