On this page |
Overview ¶
A viewer is a type of pane that shows the scene. The viewer’s contents is conceptually divided into viewports. By default, the scene viewer shows a single viewport, the Perspective view. However, you can use the view layout controls to, for example, split the view into four viewports (Perspective, Top, Front, and Right views). Viewports are the parts of the interface that actually display the scene to the user.
To access a viewport, you must first get a reference to a Scene Viewer pane tab. See the following methods:
Use hou.ui.paneTabOfType(hou.paneTabType.SceneViewer)
to get a reference to a Scene Viewer pane tab in the current pane layout. If the current layout has no Scene Viewer pane tab, this returns None
.
To be more precise about which viewer you grab in a possible multi-viewer layout, see also hou.ui.curDesktop to get a hou.Desktop object representing the current pane layout, and hou.Desktop.sceneViewers to get a list of scene viewer pane tabs in the current layout.
Once you have a reference to a hou.SceneViewer pane object, you can use it to access the viewer pane’s viewport or viewports. See the following methods:
Returns a list of the viewports in the viewer pane.
Gets a single viewport by name, for example "Top"
.
hou.SceneViewer.selectedViewport
Gets the currently “selected” viewport. The user can select a viewport by pressing Space + N in the viewport. The view menus in the upper right corner are drawn brighter in the selected viewport.
Methods ¶
Viewport framing ¶
In general, homing resets the view direction to the default view, while framing keeps the current view direction and just moves the viewpoint.
home()
Moves/tumbles the viewport to the default view.
homeAll()
Moves/tumbles the view to show all geometry/objects (including templated geometry). See also homeNonTemplated().
homeSelected()
Moves/tumbles the view to show the selected geometry/objects.
homeGrid()
Homes the viewport on the grid.
homeNonTemplated()
Moves/tumbles the view to show all non-templated geometry.
homeBoundingBox(bbox, center_to_origin=False)
Moves/tumbles the viewport to show an arbitrary area in the scene.
bbox
A hou.BoundingBox representing the volume of space to focus on.
center_to_origin
frameAll()
Moves the view to show all geometry/objects. See also frameNonTemplates().
frameBoundingBox(bbox)
Moves the view to show an arbitrary area in the scene.
bbox
A hou.BoundingBox representing the volume of space to focus on.
frameSelected()
Moves the view to show the selected geometry/objects.
frameGrid()
Frames the view on the grid.
frameNonTemplated()
Moves the view to show all non-templated geometry.
Viewpoint and cameras ¶
-
Unlike some other 3D packages, in Houdini the view is not always linked to an actual Camera object. The may be “locked” to a camera (so changing the view tumbles/moves the camera), or it may be a “free” (“No camera”) view not tied to a camera, or it may be temporarily looking through a camera (or light), but the view is not locked to the camera (so changing the view will not affect the camera). The user interface for these settings is the camera menu in the top right corner of each viewport, and the Lock Camera button on the view toolbar.
Most of the methods in this section are not affected by the difference. For example, if the view is “free”, the defaultCamera() method will return an object that manipulates just the view, but if the view is locked to a camera, it will return an object that manipulates the linked camera.
-
You can tell if the view is currently looking through a camera/light if the result of camera() is not
None
. This does not tell you whether the view is locked to the camera or not. -
The following talks about the camera representing the “viewpoint” as a shorthand, however the viewport camera stores many settings besides the viewpoint position and direction, including pivot, aspect ratio, clipping planes, and so on.
viewTransform()
→ hou.Matrix4
Returns the transform matrix of the view. To set the viewpoint programmatically, use defaultCamera() to get a hou.ViewportCamera object and manipulate that.
modelToGeometryTransform()
→ hou.Matrix4
Returns the transform from the modeling space to the space of the points in the geometry. When modeling at object level, this converts from the object space into the SOP’s space. When modeling at the SOP level, this is identity as the model space was the SOP space.
cameraToModelTransform()
→ hou.Matrix4
Equivalent to viewTransform, this is the transform matrix of the view. It converts from the camera’s space into the space that modeling is currently being done at.
ndcToCameraTransform()
→ hou.Matrix4
Returns the transform from normalized device coordinates to the viewport camera’s space. This is often a projective transform, so if used the resulting points will have to be dehomogenized by dividing by w.
viewportToNDCTransform()
→ hou.Matrix4
Returns the matrix converting from the viewport pixel coordinates to the normalized device coordinates. This is a two dimensional scale and translate, so the z and w components are identity.
windowToViewportTransform()
→ hou.Matrix4
Returns the matrix converting from the window pixel viewport coordinates to the viewport pixel coordinates. Window coordinates are present in hou.ViewerEvent and are unaware of quad-view viewports.
This is a two dimensional translate, so z and w components are identity.
viewPivot()
→ hou.Vector3
Returns a hou.Vector3 representing the view pivot as a point in world space.
resolutionInPixels()
→ 2-tuple of int
Returns the resolution of the viewport in pixels, as (width, height).
camera()
→ hou.ObjNode or None
If the viewport is currently looking through a camera or light (not necessarily locked to it), this returns an object representing the camera/light’s node. Returns None
if the viewport is not looking through a camera/light.
cameraPath()
→ str
Return the path to the camera that the viewport is looking through. If the viewport isn’t looking through a camera, return an empty string. In objects, this is the path of the camera object node. In LOPs, this is the USD primitive path.
saveViewToCamera(camera_node)
Copies the viewpoint transform of the current view onto the transformation parameters of a camera or light node.
camera_node
A hou.ObjNode object representing the node to save the view to.
setCamera(camera_node)
Copies the viewport transform from the transformation parameters of a camera or light onto the current view. This is the same as choosing a camera/light from the Look through submenu in the viewport’s Camera menu.
camera_node
A hou.ObjNode object representing the node to save the view from.
defaultCamera()
→ hou.GeometryViewportCamera
Returns an object representing the viewport’s viewpoint. The returned object is “live” in that changing its settings will immediately change the view
If a camera/light is locked to the view, changing the settings of the GeometryViewportCamera
will change the camera/light node’s parameters as well.
Instead of a live object, you can get a “disconnected” version of the viewpoint using hou.GeometryViewportCamera.stash. This is a useful way to remember a certain viewpoint in code for later. You can restore a “stashed” camera using setDefaultCamera().
# Remember the current view cam = viewport.defaultCamera() saved = cam.stash() # Change the view somehow # Restore the original view viewport.setDefaultCamera(saved)
setDefaultCamera(stashed_cam)
Takes a hou.GeometryViewportCamera and copies its values into this viewport.
Set the current camera settings of the viewport to the settings stored in cam_setting
. If the viewport is looking through a camera and the view is not locked to the camera, it will be switched to “No camera”. If the view is locked to the camera, the camera object will be updated instead.
useDefaultCamera()
Set the viewport camera to “No camera” and stop looking through a camera object (or light).
isCameraLockedToView()
→ bool
Query to see if the camera is locked to the view. This returns the state of the camera lock only; this can be enabled without viewing through a camera.
lockCameraToView()
Set the viewport camera lock. When True, any changes to the view will affect the camera object being viewed though. When False, the camera will become disconnected from the viewport and revert to the default viewport camera when the user tumbles the view. It is possible to enable this without the viewport looking through a camera, though view changes will not affect any objects until the user sets the viewport to look through a camera or light.
isViewExportedToCameraContinuously()
→ bool
Query to see if the view changes are exported to a locked camera continuously (True) or only on mouse release (False).
exportViewToCameraContinuously()
Set whether the export of view changes to a locked camera happens continuously (True) or only on mouse release (False).
isActive2D()
→ bool
Query if the viewport is currently displaying 2d geometry, often implying it is displaying UV information.
isActive3D()
→ bool
Query if the viewport is currently displaying 3d geometry. This is the case for perspective and ortho viewports.
draw()
Request that the viewport redraw. Multiple draw()
calls within the same script will be merged into a single call.
Settings ¶
name()
→ str
Query of the name of the viewport (persp1
, top1
, etc).
changeName(str)
Set the name of the viewport. Setting the name does not affect its type, so naming a perspective view front
will be confusing. This is best used in conjunction with changeType()
.
settings()
→ hou.GeometryViewportSettings
Returns the hou.GeometryViewportSettings object for this viewport. By calling methods on that object, you can read/edit the viewport’s display options.
size()
→ tuple of
double
Returns the size of this viewport. The tuple elements are returned in viewport coordinates.
-
X
position (lower left) -
Y
position (lower left) -
Width
dimension -
Height
dimension
geometry()
→ tuple of
int
Returns the position and size of this viewport in the UI space. The tuple elements are returned in viewport coordinates, relative to the lower-left corner position of the scene viewer.
-
X
position (lower left) -
Y
position (lower left) -
Width
dimension -
Height
dimension
type()
→ hou.geometryViewportType enum value
Query the viewport type (UV, 3D, top, left, etc).
changeType([Hom:hou.geometryViewportType])
Set the viewport type (hou.geometryViewportType). This method first attempts to restore a stashed view for the new viewport type, but failing that, will home the viewport.
usesConstructionPlane()
→ bool
Returns whether this viewport uses the construction plane when it is on.
Selection ¶
queryNodeAtPixel(x, y, pick_templates=False)
→ hou.ObjNode, hou.SopNode, or None
Return the node draw at the specified pixel in the viewport, or None
if
there is nothing there. The type of node returned depends on the level of
the viewer.
If pick_templates is True then templated geometries will be included in the query.
queryPrimAtPixel(node, x, y)
→ hou.Prim or None
Return the primitive drawn at the specified pixel in the viewport, or
None
if there is nothing there. The primitive returned will be a
subclass of hou.Prim.
The parameter node is used to restrict the query to geometry within a particular node. If node is None, then the query is unrestricted.
queryInspectedGeometry()
→ hou.Geometry or None
Return the geometry currently being inspected in the viewport, or None
when nothing is being inspected or when called outside of an inspect
script.
queryInspectedPrim()
→ hou.Prim or None
Return the primitive currently being inspected in the viewport, or None
when nothing is being inspected or when called outside of an inspect
script. The primitive returned will be a subclass of hou.Prim.
mapToScreen(position)
→ hou.Vector2
Convert world coordinates to viewport coordinates.
position
A hou.Vector3 containing a world space position.
mapToWorld(x, y)
→ tuple of (hou.Vector3, hou.Vector3)
Convert viewport coordinates to world coordinates. Returns a ray (direction vector and an origin point).
mapFromMouseChop(x, y)
→ tuple of (int, int)
Convert from the Mouse CHOP’s X and Y screen values (which range from -1
to 1
) to viewport coordinates, where (0,0)
is the bottom left corner of
the viewport.
queryWorldPositionAndNormal(x,y,selectionRestriction)
→ tuple of (hou.Vector3, hou.Vector3, bool)
Look up the world position and normal of geometry at viewport coordinates (x,y)
where (0,0)
is the bottom left corner of the viewport. The returned tuple contains the position, normal, and a boolean flag that is True
if there is geometry at that screen position. The selectionRestriction
can optionally exclude selected or non-selected prims; it defaults to querying all prims.
Callbacks ¶
addEventCallback(callback)
Register a Python callback to be called whenever a viewport event occurs.
callback
Any callable Python object that expects keyworded arguments specific to an event type. This
callback can be used for any geometry viewport event type.
The kwargs contains the following:
-
event_type: A viewport event.
-
desktop: The desktop object holding the scene viewer.
-
viewer: The scene viewer object pointing to the viewport.
-
viewport: The viewport object that triggered the event.
import hou def onViewportCB(**kwargs): event_type=kwargs['event_type'] desktop=kwargs['desktop'] viewer=kwargs['viewer'] viewport=kwargs['viewport'] print( "event type=",event_type ) print( "desktop=",desktop ) print( "viewer=",viewer ) print( "viewport=",viewport ) cam = viewport.camera() if cam: print( "camera=%s\n\n"%(cam.name()) ) curSceneViewer = [item for item in hou.ui.curDesktop().currentPaneTabs() if item.type() == hou.paneTabType.SceneViewer][0] curSceneViewer.curViewport().addEventCallback(onViewportCB)
clearEventCallbacks()
Remove all Python callbacks that have been registered with hou.GeometryViewport.addEventCallback.
removeEventCallback(self,callback)
Remove a specific Python callback that have been registered with hou.GeometryViewport.addEventCallback.
eventCallbacks()
→ tuple
of callbacks
Return a tuple of all the Python callbacks that have been registered with hou.GeometryViewport.addEventCallback.