[maya] framing object in camera view with python

hello,

Is there any idea how you can move a camera to a position so that a geometry would fit perfect in it s frame ?

I have tried to do calculation with bounding box and estimate a correct distance,
but there are so many factors to consider (angle of view of camera, max size, frame size etc…)

The maya frame selected works quit good.
But uses views and animates it (which i don’t know how it behaves in headless machine batch)

Is there a way to get just the calculated results of a frame.
Or any methods available that can help me on this ?

thanks !
Sven

I guess you can use this

import pymel.core as pm

# Position the active camera to view the active objects
pm.viewFit()

# Position cameraShape-1 to view all objects
pm.viewFit( 'cameraShape1', all=True )

# Fill 50 percent of the active view with active objects
pm.viewFit( f=0.5 )

pm.viewFit( all=True )

link to the docs…

http://help.autodesk.com/cloudhelp/2015/ENU/Maya-Tech-Docs/CommandsPython/viewFit.html

a yes great, this all works ! also on headless machines this is no problem.

Hello,

I have tried this part of the code and it works ok.
Only problem is that the fitting happens on the opengl view size and not on the render size.

I want to fit the object in the render frame.

Any ideas on this ?
Is it possible to change the viewsize or a work around for this ?

thanks !

Sven

1 Like

I’d do something like this:
1 - viewfit current viewport
2 - query the active camera transform
3 - Apply this transform to the render camera

Hey svennovich, not sure if this is what you wanted but I once needed to do something similar - Frame all objects in scene within the render/resolution frame. If my memory serves me right, this is how I did it:

  • Query all the height of bounding boxes and taking only the biggest/highest value
  • Using that value, create a plane as the base ( highest value times <try out a range of numbers, see which fits best>)
  • Deselect any selection and do a FrameAll() command
  • Delete the created plane

[QUOTE=svennovich;27447]Hello,

I have tried this part of the code and it works ok.
Only problem is that the fitting happens on the opengl view size and not on the render size.

I want to fit the object in the render frame.

Any ideas on this ?
Is it possible to change the viewsize or a work around for this ?

thanks !

Sven[/QUOTE]

Yup you can totally fix the viewsize it’s a flag in the viewfit. try pm.viewFit([camera],f=[some number],[whatever you want to focus on])
the f is the focus amount and how far the camera will go in and out. Remember you can change select the camera and set up the reticle (set the gate to resolution gate) then run the view fit it will respect the it.