Skip to content

Instantly share code, notes, and snippets.

@Liametc
Last active April 20, 2024 05:50
Show Gist options
  • Save Liametc/febd947c4a89396a933ea94846ac3008 to your computer and use it in GitHub Desktop.
Save Liametc/febd947c4a89396a933ea94846ac3008 to your computer and use it in GitHub Desktop.
Center maya camera 2D pan/zoom on object using python api 2.0
from maya import OpenMaya
# create selection
sel = OpenMaya.MSelectionList()
sel.add("locator1")
sel.add("camera1")
# get dag paths
loc_dag = OpenMaya.MDagPath()
sel.getDagPath(0, loc_dag)
cam_dag = OpenMaya.MDagPath()
sel.getDagPath(1, cam_dag)
# camera object
cam = OpenMaya.MFnCamera(cam_dag)
# camera transform
cam_trans = OpenMaya.MFnTransform(cam_dag)
# camera world position
cam_pos = cam_trans.translation(OpenMaya.MSpace.kWorld)
# camera rotation matrix
rot_mat = cam_dag.inclusiveMatrix()
# locator bounding box
loc_dag_node = OpenMaya.MFnDagNode(loc_dag)
loc_bb = loc_dag_node.boundingBox()
# locator position in world space
loc_pos = loc_bb.center()
# get locator position in camera space
# multiply the locator to camera vector by the camera rotation matrix
loc_vec = OpenMaya.MVector(loc_pos - cam_pos)
# api 1.0 doesn't allow matrix * vector, but does allow vector * matrix
# transpose the matrix to account for this
loc_cam_pos = loc_vec * rot_mat.transpose()
focal_len = cam.focalLength()
# get the x and y position on the imaginary plane at the focal length
# distance away from the camera
# f -> distance away from camera
# X,Y,Z -> object's camera space coordinates
# x' = f(X/Z) y' = f(Y/Z)
# refer to http://www.cse.psu.edu/~rtc12/CSE486/lecture12.pdf
# negate because maya camera looks in -z direction
# convert to inches because 2d pan operates in inches
x = -(focal_len*(loc_cam_pos.x/loc_cam_pos.z))/25.4
y = -(focal_len*(loc_cam_pos.y/loc_cam_pos.z))/25.4
# set the camera's x and y pan. Account for camera scale and offsets
cam.setHorizontalPan((x/cam.cameraScale()) - cam.horizontalFilmOffset())
cam.setVerticalPan((y/cam.cameraScale()) - cam.verticalFilmOffset())
from maya.api import OpenMaya
# create selection
sel = OpenMaya.MSelectionList()
sel.add("locator1")
sel.add("camera1")
# get dag paths
loc_dag = sel.getDagPath(0)
cam_dag = sel.getDagPath(1)
# camera object
cam = OpenMaya.MFnCamera(cam_dag)
# camera transform
cam_trans = OpenMaya.MFnTransform(cam_dag)
# camera world position
cam_pos = cam_trans.translation(OpenMaya.MSpace.kWorld)
# camera rotation matrix
rot_mat = cam_dag.inclusiveMatrix()
# locator bounding box
loc_dag_node = OpenMaya.MFnDagNode(loc_dag)
loc_bb = loc_dag_node.boundingBox
# locator position in world space
loc_pos = loc_bb.center
# get locator position in camera space
# multiply the locator to camera vector by the camera rotation matrix
loc_cam_pos = rot_mat * (loc_pos - cam_pos)
focal_len = cam.focalLength
# get the x and y position on the imaginary plane at the focal length
# distance away from the camera
# f -> distance away from camera
# X,Y,Z -> object's camera space coordinates
# x' = f(X/Z) y' = f(Y/Z)
# refer to http://www.cse.psu.edu/~rtc12/CSE486/lecture12.pdf
# negate because maya camera looks in -z direction
# convert to inches because 2d pan operates in inches
x = -(focal_len*(loc_cam_pos.x/loc_cam_pos.z))/25.4
y = -(focal_len*(loc_cam_pos.y/loc_cam_pos.z))/25.4
# set the camera's x and y pan. Account for camera scale and offsets
cam.horizontalPan = (x/cam.cameraScale) - cam.horizontalFilmOffset
cam.verticalPan = (y/cam.cameraScale) - cam.verticalFilmOffset
@Liametc
Copy link
Author

Liametc commented May 17, 2023

Greetings.

for api2 line 19: rot_mat = cam_dag.inclusiveMatrix() instead of rot_mat = cam_trans.inclusiveMatrix()

Thanks @orgazmaat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment