Skip to content

Instantly share code, notes, and snippets.

@boredstiff
Created July 28, 2018 01:39
Show Gist options
  • Save boredstiff/816005ba4a6e28a0d7f8cf7fcb69bee7 to your computer and use it in GitHub Desktop.
Save boredstiff/816005ba4a6e28a0d7f8cf7fcb69bee7 to your computer and use it in GitHub Desktop.
"""
no Get the midpoint of the locators.
no Get a vector from that point to each locator, and to the camera.
no The mid-to-front vector dotted with mid to camera vector will be greater than 0
1.) Get the world position (MPoint) of the front and back locators
2.) Get the vector from back to front. This will be your Z vector for the matrix
3.) Cross this vector with the world up vector (world Y) to get your X vector
4.) Cross your X and Z vectors to get an orthogonal Y vector
5.) Build a matrix with these component vectors and the position of your camera
6.) Set the camera to that matrix
There are at least three ways to get the position component of a matrix.
You can multiply an identity point by the matrix (MPoint() * matrix)
You can explicitly construct the point with the values (MPoint(m(3, 0), m(3, 1), m(3, 2))
You can use MTransformationMatrix(matrix).translation() to get the position as an MVector
"""
from maya.api import OpenMaya
camera_name = 'shot_cam'
face_front_locator_name = 'cam_base'
back_locator_name = 'cam_target'
front_locator = None
back_locator = None
shot_cam = None
transform_nodes = OpenMaya.MItDag(filterType=OpenMaya.MFn.kTransform)
dependency_node = OpenMaya.MFnDependencyNode()
# Get the locators
while not transform_nodes.isDone():
current_object = transform_nodes.currentItem()
dag_node = OpenMaya.MFnDagNode()
dag_node.setObject(current_object)
if dag_node.name() == face_front_locator_name:
front_locator = current_object
elif dag_node.name() == back_locator_name:
back_locator = current_object
elif dag_node.name() == camera_name:
shot_cam = current_object
transform_nodes.next()
def get_world_space_matrix(obj):
dependency_node = OpenMaya.MFnDependencyNode(obj)
world_matrix_attr = dependency_node.attribute('worldMatrix')
plug = OpenMaya.MPlug(obj, world_matrix_attr)
plug = plug.elementByLogicalIndex(0)
_matrix = plug.asMObject()
data = OpenMaya.MFnMatrixData(_matrix)
matrix = data.matrix()
return matrix
front_locator_matrix = get_world_space_matrix(front_locator)
back_locator_matrix = get_world_space_matrix(back_locator)
front_point = OpenMaya.MPoint() * front_locator_matrix
back_point = OpenMaya.MPoint() * back_locator_matrix
front_vector = OpenMaya.MVector(front_point)
back_vector = OpenMaya.MVector(back_point)
z_vector = front_vector - back_vector
# Don't know how to query scene up in API
y_up_vector = OpenMaya.MVector.kYaxisVector
x_vector = z_vector ^ y_up_vector
orthogonal_y_vector = x_vector ^ z_vector
# Don't get
shot_cam_transform = OpenMaya.MFnTransform(shot_cam)
translation = shot_cam_transform.translation(OpenMaya.MSpace.kTransform)
def build_matrix(translate=(0,0,0), x=(1,0,0), y=(0,1,0), z=(0,0,1)):
# Create transformation matrix from input vectors
matrix = OpenMaya.MMatrix([
x.x, x.y, x.z, 0.0,
y.x, y.y, y.z, 0.0,
z.x, z.y, z.z, 0.0,
translate.x, translate.y, translate.z, 1.0])
return matrix
camera_matrix = build_matrix(translation, x_vector.normalize(), y_up_vector.normalize(), z_vector.normalize())
camera_transformation_matrix = OpenMaya.MTransformationMatrix(camera_matrix)
shot_cam_transform.setTransformation(camera_transformation_matrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment