Get/Set values of USD camera via Python?

   1759   4   0
User Avatar
Member
14 posts
Joined: Dec. 2017
Offline
I'm quite new to the "stage" stuff in Houdini so forgive me if this is obvious.

How would I get/set the values of a camera in the usd/stage context (vop?) at specific times? The below snippet seems to work to get the matrix, but the "time" parameter of ComputeLocalToWorldTransform does not seem to do anything. Do I need to get an "animated" stage first somehow? And what about setting values at specific times for camera?

import hou
from pxr import Usd, UsdGeom


node = hou.selectedNodes()[0]
primpath = node.parm("primpath").evalAsString()
stage=node.stage()
prim = stage.GetPrimAtPath(primpath)

xform = UsdGeom.Xformable(prim)
wm = xform.ComputeLocalToWorldTransform(0)

print(wm)
Edited by sbjork - Jan. 27, 2025 06:18:55
User Avatar
Member
109 posts
Joined: Aug. 2017
Offline
Drop down a Cache LOP before using your script and make it cache desired frame range, or the whole timeline.
Edit: On second thought, no, simply set the "Frame Range/Subframes" tab on your camera to "Sample Frame Range" instead.

After making that one change your code started working for me.
Edited by alexmajewski - Jan. 27, 2025 12:51:16
User Avatar
Member
14 posts
Joined: Dec. 2017
Offline
Thanks @alexmajewski, works well!

Any suggestion for setting values, i.e translate or focal length (at different times).

I tried node.editableStage but it returns None. Not sure if that is the correct way though.
User Avatar
Member
14 posts
Joined: Dec. 2017
Offline
Sorry, my mistake, didn't realize it used the same syntax as other nodes via
node.parm("tx").set
.

All good.
User Avatar
Member
109 posts
Joined: Aug. 2017
Offline
node.parm("tx").set
Not exactly the solution I expected, but hey, if it works it works
From what I understand, the stage of nodes is locked most of the time. It is unlocked and editable under certain conditions, like when using Python Script lop on itself with stage=hou.pwd().editableStage().

If you happened to end up with an editable stage somehow, here's how you could set those attributes:
prim = stage.GetPrimAtPath(primpath)

focal_len = prim.GetAttribute("focalLength")
focal_len.Set(2.0, 10) # sets 2.0 at time sample 10
focal_len.Set(5.0, 30) # sets 5.0 at time sample 30
  • Quick Links