This sort of relates to my previous post, but most of those issues have been resolved. I am working on a script that will scale my prim to fit the viewport of a camera generated with a custom focus distance, focal length, and horizontal/vertical apertures. These are the 4 factors I was able to determine affect the perceived size of an object through the viewport, however I am not sure the relationship between these that would allow me to properly scale the object so that it fits properly in the viewport regardless of how I change these features.
Hopefully someone could either refer me to some documentation that explains something similar, or can outright help me.
Hi @Dinonel, I’m afraid we don’t have much documentation on the cameras math, it seems. The best I can do is point you at the implementation of GfCamera, which is where most of the magic happens…
Thanks @spiff ! I ended up figuring it out by brute force, so I’m going to leave the code here if anyone else is interested in doing something similar.
# Get the scene and make it transformable
scene = UsdGeom.Xform(stage.GetPrimAtPath(scene_path))
# Get the size of the scene
bbox = scene.ComputeWorldBound(0, UsdGeom.Tokens.default_)
bbox_size = bbox.ComputeAlignedBox().GetSize()
# Translate and scale the scene to be centered in the camera
horiz = camera.GetHorizontalApertureAttr().Get()
vert = camera.GetVerticalApertureAttr().Get()
scale_factor = min(horiz/bbox_size, vert/bbox_size)
mid_z = bbox.ComputeAlignedBox().GetMidpoint()
Gf.Vec3f(0, 0, -mid_z*scale_factor))
scene.AddScaleOp().Set(Gf.Vec3f(scale_factor, scale_factor, scale_factor))
The script assumes that the object will be centered at the origin and that the camera will be placed some distance away along a single axis