Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/8973cd0b790c389277a7f492b4aad201 to your computer and use it in GitHub Desktop.
Save BigRoy/8973cd0b790c389277a7f492b4aad201 to your computer and use it in GitHub Desktop.
Houdini Solaris set camera focus distance
string targetpath = "/focus_point"; // Target prim to focus on
// Compute Z-plane distance to target prim transform from camera transform
matrix cam = usd_worldtransform(0, @primpath);
matrix target = usd_worldtransform(0, targetpath);
vector origin = set(0, 0, 0);
matrix local_diff = target * invert(cam);
vector local_pos = ptransform(origin, local_diff);
float dist = -local_pos.z; // inverted, because -Z is direction camera looks at
// Set focus distance on the camera
f@focusDistance = dist;
printf("%s\n", dist);
from pxr import UsdGeom
node = hou.pwd()
stage = node.editableStage()
cam = stage.GetPrimAtPath("/cameras/camera1")
target = stage.GetPrimAtPath("/focus_point")
cache = UsdGeom.XformCache()
cam_xform = cache.GetLocalToWorldTransform(cam)
target_xform = cache.GetLocalToWorldTransform(target)
target_local_xform = target_xform * cam_xform.GetInverse()
distance = -target_local_xform.ExtractTranslation()[2]
UsdGeom.Camera(cam).CreateFocusDistanceAttr().Set(distance)
@BigRoy
Copy link
Author

BigRoy commented Jan 5, 2024

Note that a lot of examples on the web set the focus distance by taking just the distance between object and camera, but that's technically incorrect. It should be the z-depth distance to the focal plane - so the z-depth depth for the target object from the camera.

Also see other workflows:

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