Hello,
in this post I will show a quick way of adding auto-focus support to Houdini camera. By setting a target, following couple lines of code will determine correct distance to focus on. This approach will work for any renderer.
The setup is pretty simple, we need to add two parameters to our camera: target which is a string pointing to OBJ node we want to focus on and dist parameter which will contain following Python expression.
This file contains 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
# our cam object | |
cam = hou.pwd() | |
# get 4×4 matrix representing transformation of our camera | |
cam_xform = cam.worldTransform() | |
# get position of our camera | |
cam_pos = hou.Vector4(0, 0, 0, 1) * cam_xform | |
cam_pos = hou.Vector3(cam_pos) | |
# view axis of our camera | |
view_axis = hou.Vector4(0, 0, -1, 0) * cam_xform | |
# get target path | |
target_path = cam.evalParm("target") | |
# get target object | |
target_node = hou.node(target_path) | |
# get target's transformation | |
target_xform = target_node.worldTransform() | |
# get target's position | |
target_pos = hou.Vector4(0, 0, 0, 1) * target_xform | |
# cast from Vector4 to Vector3 | |
view_axis = hou.Vector3(view_axis) | |
target_pos = hou.Vector3(target_pos) | |
# normalize our view axis | |
view_axis = view_axis.normalized() | |
# get a vector pointing from camera to target | |
cam_target = hou.Vector3( target_pos – cam_pos ) | |
# project cam_target vector on view_axis, because view_axis is normalized, this results in length of our projected vector, what represents focus distance in our case | |
focus_dist = cam_target.dot(view_axis) | |
return focus_dist |
After that we can link Focal Distance parameter to our dist. Feel free to drop a helper Null object for visualizing focus plane 🙂