Last active
January 5, 2024 11:10
-
-
Save BigRoy/8973cd0b790c389277a7f492b4aad201 to your computer and use it in GitHub Desktop.
Houdini Solaris set camera focus distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: