How to set the camera focus distance using expression?
2815 6 3- wanglifu
- Member
- 186 posts
- Joined: 2月 2017
- Offline
- eikonoklastes
- Member
- 396 posts
- Joined: 4月 2018
- Online
Check the below post, but please also RFE for this. This should be built into Houdini (with a better implementation than mine, obviously):
https://www.sidefx.com/forum/topic/7088/#post-374564 [www.sidefx.com]
https://www.sidefx.com/forum/topic/7088/#post-374564 [www.sidefx.com]
- wanglifu
- Member
- 186 posts
- Joined: 2月 2017
- Offline
eikonoklastes
Check the below post, but please also RFE for this. This should be built into Houdini (with a better implementation than mine, obviously):
https://www.sidefx.com/forum/topic/7088/#post-374564 [www.sidefx.com]
Yeah,this should be a basic option.Sidefx please fix this in H20.
- guan9287
- Member
- 3 posts
- Joined: 4月 2021
- Offline
When using the transform to move the camera, the method mentioned above becomes ineffective. I used the 'wrangle' with the help of ChatGPT, and the following is the code:
matrix cameraTransform = usd_worldtransform(0,"/cameras/camera1");
matrix targetTransform = usd_worldtransform(1,"/pig/target_FD");
vector4 cameraPosHomogeneous = set(0, 0, 0, 1);
vector4 targetPosHomogeneous = set(0, 0, 0, 1);
cameraPosHomogeneous = cameraTransform * cameraPosHomogeneous;
targetPosHomogeneous = targetTransform * targetPosHomogeneous;
vector cameraPos = set(cameraPosHomogeneous, cameraPosHomogeneous, cameraPosHomogeneous);
vector targetPos = set(targetPosHomogeneous, targetPosHomogeneous, targetPosHomogeneous);
float distance = distance(cameraPos, targetPos);
usd_setattrib(0,"/cameras/camera1", "focusDistance", distance);
matrix cameraTransform = usd_worldtransform(0,"/cameras/camera1");
matrix targetTransform = usd_worldtransform(1,"/pig/target_FD");
vector4 cameraPosHomogeneous = set(0, 0, 0, 1);
vector4 targetPosHomogeneous = set(0, 0, 0, 1);
cameraPosHomogeneous = cameraTransform * cameraPosHomogeneous;
targetPosHomogeneous = targetTransform * targetPosHomogeneous;
vector cameraPos = set(cameraPosHomogeneous, cameraPosHomogeneous, cameraPosHomogeneous);
vector targetPos = set(targetPosHomogeneous, targetPosHomogeneous, targetPosHomogeneous);
float distance = distance(cameraPos, targetPos);
usd_setattrib(0,"/cameras/camera1", "focusDistance", distance);
- jsmack
- Member
- 8042 posts
- Joined: 9月 2011
- Offline
guan9287
When using the transform to move the camera, the method mentioned above becomes ineffective. I used the 'wrangle' with the help of ChatGPT, and the following is the code:
matrix cameraTransform = usd_worldtransform(0,"/cameras/camera1");
matrix targetTransform = usd_worldtransform(1,"/pig/target_FD");
vector4 cameraPosHomogeneous = set(0, 0, 0, 1);
vector4 targetPosHomogeneous = set(0, 0, 0, 1);
cameraPosHomogeneous = cameraTransform * cameraPosHomogeneous;
targetPosHomogeneous = targetTransform * targetPosHomogeneous;
vector cameraPos = set(cameraPosHomogeneous, cameraPosHomogeneous, cameraPosHomogeneous);
vector targetPos = set(targetPosHomogeneous, targetPosHomogeneous, targetPosHomogeneous);
float distance = distance(cameraPos, targetPos);
usd_setattrib(0,"/cameras/camera1", "focusDistance", distance);
You can't use distance to set focal distance, as focal distance is distance between planes, not points. The target point should be transformed into the camera's local space and the dot product with the z axis (or just the z component) used as the focus distance.
- dmarkota_adm
- Member
- 1 posts
- Joined: 6月 2022
- Offline
Hi,
I revised this and the follwing works well for me:
I revised this and the follwing works well for me:
matrix cameraTransform = usd_worldtransform(0,"/cameras/camera1"); matrix targetTransform = usd_worldtransform(0,"/focusTarget"); vector cameraPos = cameraTransform * {0,0,0}; vector targetPos = targetTransform * {0,0,0}; float dist = distance(cameraPos, targetPos); usd_setattrib(0,"/cameras/camera1", "focusDistance", dist);
Edited by dmarkota_adm - 2024年4月20日 03:45:37
- pixelninja
- Member
- 48 posts
- Joined: 3月 2017
- Offline
For completeness here's the vex for the planar distance between a camera prim and a focus prim with a variable to control which input the focus prim is sourced from.
string cam_path = chs('camera_primitive'); string focus_path = chs('focus_primitive'); int focus_source = chi('focus_source'); matrix cameraTransform = usd_worldtransform(0, cam_path); matrix focusTransform = usd_worldtransform(focus_source, focus_path); vector focus_p = cracktransform(0,0,0,{0,0,0},focusTransform); focus_p *= invert(cameraTransform); usd_setattrib(0,cam_path, "focusDistance", max(-focus_p.z, 0));
-
- Quick Links