When looking to find the 2D bounding rect of an object I came across this:
Sadly, it is the only tagged tech, maya or mel, so no more goodies there.
Here’s a python version. I intended to do it with more API, but the heighFieldOfView of the MFnCamera does not return the same value as the cmds.camera(hfv=True), not even when you convert it to radians, or when you put it through a tan operation. Hence I have no idea how that value is formatted and can’t continue with it.
Updated the math library for this (somewhere in python, located all the way at the bottom), to include a boundingbox class and fixed matrix to vector multiplication (I mixed up rows and columns before according to how Maya formats matrices, which is how openGL formats them as well).
It returns a BoundingBox with two 2D vectors (min and max) that represent screenspace coordinates. Multiply it with the width of the image to get actual pixels. The aspect ratio is squared so the top of the image may not be 1 if the height is not equal to the width. If you multiply the outy with the image width, you’ll still get the proper pixels though.
import math from maya.OpenMaya import * from maya import cmds from Vmath.boundingbox import BoundingBox import Vmath.matrix import Vmath.vec PI = 3.14159 DEGTORAD = (PI/180) def getScreenspaceBoundingRect(in_objstr, in_camstr): hfv = math.tan(cmds.camera(in_camstr, hfv=True, q=True)*0.5*DEGTORAD) vfv = math.tan(cmds.camera(in_camstr, vfv=True, q=True)*0.5*DEGTORAD) worldInverseMatrix = Matrix( 4, 4, cmds.getAttr('%s.worldInverseMatrix'%in_camstr) ) ar = cmds.camera(in_camstr, q=True, ar=True) ''' handle meshes ''' path = getDagPathFromName(in_objstr) iter = MItMeshVertex( path ) bb = None while not iter.isDone(): pt = iter.position(MSpace.kWorld) pt = worldInverseMatrix*Vec(pt.x,pt.y,pt.z) if pt: ptx,pty = 0,0 ptx = ((pt/-pt)/hfv)*0.5+0.5 pty = ((pt/-pt)/vfv)*0.5+ar*0.5 pty *= (1.0/ar) if not bb: bb = BoundingBox([ptx,pty],[ptx,pty]) else: bb.extend([ptx,pty]) iter.next() return bb def getDagPathFromName(in_name): selector = MSelectionList() MGlobal.getSelectionListByName(in_name,selector) path = MDagPath() selector.getDagPath(0, path) return path