Okay following the previous vector posts, I decided to plunge ahead and create a plugin that capitalizes on that knowledge.
Previously, in OpenMaya 1.0, the MPxLocator has been defined by using the draw method by using Open Graphics Library (OpenGL) functions. Maya’s architecture has updated a new method, and I used the OpenMaya.MUIDrawManager class method to do the drawings, making things straight forward. Here, I draw a circle, rectangle and a line; I spent way too much time figuring out the nuances of this plugin.
At first, finding out which MPxLocator examples file work right out of the box has been an issue, except finally, I found this one: uiDrawManager/uiDrawManager.cpp
In addition to finding out how the 2.0 plugins work, I also wanted to learn a bit more on reflection math, and I put that to use in this plugin:
R = 2(N * L) * N – L
Which using Maya’s Python code looks like this:
# define normal vector at origin
normal = OpenMaya.MVector(0.0, 1.0, 0.0)
# get opposing vector through double cross product
opposing_vector = normal * (2 * (normal * input_point))
opposing_vector -= input_point
# now multiply it by the scalar value
opposing_vector *= scale
if as_vector:
return opposing_vector
else:
return opposing_vector.x, opposing_vector.y, opposing_vector.z
Maya viewport handles drawing by using the DrawManager, like this:
A circle:
radius = 2.0
is_filled = True
position = OpenMaya.MPoint(0, 0, 0)
normal = OpenMaya.MVector(0, 1, 0)
drawManager.beginDrawable()
drawManager.beginDrawInXray()
drawManager.setLineWidth(line_width)
drawManager.setLineStyle(drawManager.kSolid)
drawManager.setColor(OpenMaya.MColor(plane_color))
drawManager.circle(position, normal, radius, is_filled)
drawManager.endDrawInXray()
drawManager.endDrawable()
A rectangle:
rect_scale_x = 1.0
rect_scale_y = 1.0
is_filled = False
position = OpenMaya.MPoint(0, 0, 0)
normal = OpenMaya.MVector(0, 0, 1)
up = OpenMaya.MVector(0, 1, 0)
drawManager.beginDrawable()
drawManager.setLineWidth(line_width)
drawManager.setLineStyle(drawManager.kSolid)
drawManager.setColor(OpenMaya.MColor(plane_color))
# For 3d rectangle, the up vector should not be parallel with the normal vector.
drawManager.rect(position, normal, up, rect_scale_x, rect_scale_y, is_filled)
drawManager.endDrawable()
A line:
drawManager.beginDrawable()
drawManager.setLineWidth(line_width)
drawManager.setLineStyle(drawManager.kSolid)
drawManager.setColor(OpenMaya.MColor(plane_color))
drawManager.line(OpenMaya.MPoint(0, -1, 0), OpenMaya.MPoint(0, 1, 0))
drawManager.endDrawable()
The reason why I dived into Maya’s Viewport drawing is because I was following Chad Vernon’s excellent C++ series, and his MPxLocator example no longer works in the current Maya 2020 version. The full working code can be found at my GitHub page: