HDA Python - create cam and frame selected geo issues

   3580   3   1
User Avatar
Member
17 posts
Joined: Aug. 2012
Offline
Hey guys,

I am currently working on an asset browser and its corresponding export from Houdini.
Currently, I am at the point where I would like to create, export and render multiple views from the HIP.
I am running into issues with the frameSelected() function in the Geometry Viewport class.

A) This works:
- get the viewport
- frame selected
- save view to camera
Result: camera with framed geo

Python extract:
viewer = toolutils.sceneViewer()
view = viewer.curViewport()
cam = obj_level.createNode('cam', 'asset_cam')
node.setSelected(True,True)
view.frameSelected()
view.saveViewToCamera(cam)
view.setCamera(cam)

B) This does not work:
- create a camera
- set camera to viewport
- lock camera to viewport
- frame selected
Result: camera as it was initially created
Running the frameSelected function from the shell after my external script, it correctly frames the selection.

Python extract:
viewer = toolutils.sceneViewer()
view = viewer.curViewport()
cam_top = obj_level.createNode('cam', 'asset_cam_top')
cam_top.parm('rx').set(-180)
cam_top.parm('ry').set(-90)
cam_top.parm('rz').set(90)
view.setCamera(cam_top)
view.lockCameraToView(1)
node.setSelected(True,True)
view.frameSelected()


C) This also does not work:
- create a camera
- set camera to viewport
- frame selected
- save view to camera
Result: camera with geometry that was framed inside the viewport before camera assignment

Python extract:
viewer = toolutils.sceneViewer()
view = viewer.curViewport()
cam_top = obj_level.createNode('cam', 'asset_cam_top')
cam_top.parm('rx').set(-180)
cam_top.parm('ry').set(-90)
cam_top.parm('rz').set(90)
view.setCamera(cam_top)
node.setSelected(True,True)
view.frameSelected()
view.saveViewToCamera(cam_top)

I use the first scenario to send the current view or camera to render. But I would like to also send custom top and side view cameras. For this I would like to make the other scenario work (creating a cam with specific initial rotations and then framing the geo with it).

Does anyone have any idea why it does not frame the selection from the created camera's view?

What I tried additionally:
- I explicitly select the geo in my script after camera creation to make sure the selection is correct.
- After cam creation and setting the viewport to the cam, I make sure the viewport is correct by getting it again with toolutils.sceneViewer().curViewport() before trying to frame the selection.

What I will try next:
- Using the existing orthographic top and side views to frame and then save the view to a camera. This should theoretically work as it does work with the perspective view in the first scenario above.

If anyone encountered anything like this or has any ideas, please let me know! I greatly appreciate any hints and help

Big thanks and take care,
Dziga
freelance matte painter & environment concept artist
User Avatar
Member
17 posts
Joined: Aug. 2012
Offline
Update:

Using the orthographic views to frame and then save the view to a camera works and will be the workaround for now.

In case someone could still tell me if it is possible to create a cam, assign it to the viewport and then do the framing, that would be wonderful
freelance matte painter & environment concept artist
User Avatar
Member
1 posts
Joined: Aug. 2019
Offline
Hey, dzigak!

This answer may come a bit late but hope it helps anyone in the same situation. I've been struggling with this same issue but I finally found a way to make it work.

First, you have to generate a bounding box for your object...

obj = hou.node('/obj')
geo = obj.createNode('geo')
sphere = geo.createNode('sphere')

sphereGeo = sphere.geometry()
bbox = sphereGeo.boundingBox()

Then you frame your object using the frameBoundingBox() method...

sceneViewer = hou.ui.paneTabOfType(hou.paneTabType.SceneViewer)
viewport = sceneViewer.curViewport()
viewport.frameBoundingBox(bbox)

The frameBoundingBox() is the only method that seems to work when running a script and generating geometry at the same time.
Edited by anon_user_47948620 - Nov. 3, 2021 07:58:25
User Avatar
Member
17 posts
Joined: Aug. 2012
Offline
Hey Juanjo!

It's never too late

Adjusted my code to your method as it will probably be more stable than mine ^.^

Thanks a lot for sharing your findings!

Take care,
Dziga
freelance matte painter & environment concept artist
  • Quick Links