Looking for a way to get screenspace bounding box coordinates straight from Maya

Looking for a way to get screenspace bounding box coordinates straight from Maya

Anonymous
Not applicable
1,596 Views
3 Replies
Message 1 of 4

Looking for a way to get screenspace bounding box coordinates straight from Maya

Anonymous
Not applicable

 Hello, I was hoping someone might be able to help me with this problem.  I'm working with a client that is requesting renders that have bounding boxes drawn around specific rendered objects in a scene, but in 2D screenspace.  This is with an animated camera with thousands of frames, so it's not so simple as doing it by hand unfortunately.  The objects are all different sizes and oblong as well so the bounding box will change dimensions every frame.  They also want the coordinates of said bounding boxes exported into either a JSON or CSV format (so basically a series of 4-corner coordinates per frame).

 

I have limited scripting knowledge but I was hoping someone might be able to point me in the right direction.  The best I have been able to do is using the autocrop option in Arnold's exr output to at least give me the right format/bbox, but that is a very painstaking process since I have to separate out each element into a separate render layer (and there's a quite a lot of objects/scenes).  

 

Thanks!

0 Likes
1,597 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

You could probably do something like this with the maya API.

 

A not very efficient way to do it would be something like:

 

 

import maya.api.OpenMayaUI as omui
import maya.api.OpenMaya as om

vertPositionsX = []
vertPositionsY = []

for vert in allVerticesPositions:
#convert the vertex position to a maya MPoint object, 'cause that's what worldToView expects as input pos = om.MPoint(vert)
#gives you the on-screen x position, y position, and whether of not the vertex is within the clipping plane xPos, yPos, visible = omui.M3dView().active3dView().worldToView(pos)
#if the vert isn't being clipped by the camera, we'll want those positions. if visible: vertPositionsX.append(xPos) vertPositionsY.append(yPos)
#get the positions we'll need to build our square (i.e. which verts are highest, lowest, farthest right and farthest left?) top = max(vertPositionsX) bottom = min(vertPositionsX) right = max(vertPositionsY) left = min(vertPositionsY)
#viewToWorld expects a dir so we'll make one, but we won't use it later. dir = om.MVector()
#make MPoint objects for each curve point we'll need topR = om.MPoint() topL = om.MPoint() botL = om.MPoint() botR = om.MPoint()
#Feed in those top, bottom, left, and right positions we got earlier and use them to get an in-scene coordinate for each curve point omui.M3dView().active3dView().viewToWorld(right, top, topR, dir) omui.M3dView().active3dView().viewToWorld(left, top, topL, dir) omui.M3dView().active3dView().viewToWorld(left, bottom, botL, dir) omui.M3dView().active3dView().viewToWorld(right, bottom, botR, dir)
#make the curve! mc.curve(degree=1, point=[topR, topL, botL, botR, topR])

If you do go with something like this, a good way to get all vert positions for an object can be found at this link.

 

 

You'd have to do that for every single frame for every single object you want a bounding box, so it's not ideal, but hopefully it points you in the right direction.

 

//edited to add code comments

Message 3 of 4

Anonymous
Not applicable

Just thinking about it, you could test how long that takes to do on its own, and if it's just a few seconds or so you could have the renderer run it before it starts each frame, then delete the curve when it finishes the frame.

0 Likes
Message 4 of 4

Anonymous
Not applicable

Hi, Did you find any solution to this? I'm working on same sort of a project and unable to get any success.

0 Likes