How to set the camera focus distance using expression?

   2833   6   3
User Avatar
Member
186 posts
Joined: Feb. 2017
Offline
In sops,i konw how to do this.But in solaris,how could i bind the focus distance with a null?
Edited by wanglifu - Dec. 7, 2022 04:11:28

Attachments:
Snipaste_2022-12-07_17-11-02.jpg (234.2 KB)

User Avatar
Member
396 posts
Joined: April 2018
Offline
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]
User Avatar
Member
186 posts
Joined: Feb. 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.
User Avatar
Member
3 posts
Joined: April 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);

Attachments:
微信图片_20240103102433.png (1.5 MB)

User Avatar
Member
8043 posts
Joined: Sept. 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.
User Avatar
Member
1 posts
Joined: June 2022
Offline
Hi,

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 - April 20, 2024 03:45:37

Attachments:
Screenshot 2024-04-19 091748.jpg (13.2 KB)

User Avatar
Member
48 posts
Joined: March 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