Viewers

From Odwiki

Jump to: navigation, search

Contents

Viewers

Viewers are pane tabs that represent a view of some data in your file. These include SceneViewers (objects and geometry), ParticleViewers, CompositorViewers and ContextViewers (all types of data depending on selection).


Scene Viewers

hou.SceneViewer, as well as a hou.ContextViewer, represent a standard scene view tab that displays your objects, geometry, lights, cameras, etc. These viewers allow for the selection of various objects and components, transform and custom state manipulation, snapping and manipulation of Create In Context.

Viewports

While a hou.SceneViewer is merely a pane tab type, it provides access to hou.GeometryViewport objects that represent the actual viewing areas such as the perspective and orthographic views. Access to these views enables you to manipulate the view by homing and framing on different items as well as setting a camera.

Accessing the Viewport Transform

In some cases it may be necessary to query the transform of your camera or viewer. Since a camera is simply an object, it is trivial to do. However, to access the transform of a scene viewer it is necessary to first get access to the hou.GeometryViewport object. Once that object is available you can get the 4x4 transform matrix using viewTransform().

import toolutils
viewer = toolutils.sceneViewer()
viewport = viewer.curViewport()
xform = viewport.viewTransform()

Accessing Scene Viewers

SceneViewers can be accessed a number of different ways. The most straightforward way it to use the toolutils module:

import toolutils
viewer = toolutils.sceneViewer()

This method will always give you a scene viewer if one exists. It first searches for a viewer that is the current (displayed) tab. If a normal hou.SceneViewer cannot be found, it will attempt to return an active current hou.ContextViewer. If neither of those two types are current tabs, it will try to find one that are not current.

When invoking a tool from the Tab menu in the viewer, the available kwargs dictionary will have a pane key that points to the viewer the tool was invoked in (the active pane). Otherwise the tool was launched from the shelf so the pane will be None. The toolutils.activePane() function will process the kwargs dictionary to get the pane the tool was invoked from and it will attempt to look for an open viewer pane of the right type for the running tool if there is not a specific pane.

Thus, the preferred way of accessing the current viewer from a tool context is as follows:

import toolutils
activepane = toolutils.activePane(kwargs)

A new option available in Houdini 11.0 is the ability to drag and drop different interface elements to the Python Shell. If you drag a Scene View tab to the shell you will be given a reference to that SceneViewer.

Selecting from Viewers

Once you have a viewer it is most likely the case you wish to select something from it. The hou.SceneViewer class offers the ability to select various types of objects and components from within the viewer.

Selecting Objects

Selecting objects is quite straightforward. You select one or more objects and receive a tuple of hou.ObjNode in return. You can restrict selection to only a single object, or use a tuple of strings with wild cards of node type names to only be allowed to select certain types.

import toolutils
viewer = toolutils.sceneViewer()
# Select as many objects of any type as you want.
selected_objects = viewer.selectObjects("Select some objects and press Enter to complete.")
 
# Select only a single object.
selected_objects = viewer.selectObjects("Select an object an press Enter to complete.", allow_multisel=False)
my_object = selected_objects[0]
 
# Select only nulls. Note the '*null*' type name that is allowed.  We could use just ‘null’ but these 
# extra wild cards would allow us to use our own nulls as well if we had some fancier custom 
# types.
nulls = viewer.selectObjects("Select some nulls and press Enter to complete.", allowed_types=("*null*",))

Selecting Geometry

Selecting geometry is a more complicated affair. When selecting your geometry the results are stored in a hou.GeometrySelection object. This object provides details about what type of geometry, what exact components, the source nodes, connectivity, the ability to shrink and grow selections as well as change the types that are selected.

import toolutils
viewer = toolutils.sceneViewer()
# Select a bunch of points.
selection = viewer.selectGeometry("Select some points and press Enter to complete.", geometry_types=(hou.geometryType.Points,))
 
# Get the point numbers
point_nums = selection.mergedSelectionString()
# The nodes containing the geometry selected.
selected_nodes = selection.nodes()
 
# Convert the point selection to primtives.
selection.setGeometryType(hou.geometryType.Primitives)
prim_nums = selection.mergedSelectionString()
 
# Select all connected geometry.
selection.setConnectivity(hou.connectivityType.Position)
 
# Filter the allowed primtive types to only allow selecting Sphere primtiives.
selection = viewer.selectGeometry(geometry_types=(hou.geometryType.Primitives,), primitive_types=(hou.primType.Sphere,))

Note:

  • Once you perform a geometry selection you should always extract data from your hou.GeometrySelection object immediately. If you enter a different view state or begin selecting other things you may lose all the information obtained from your geometry selection.

Selecting Dynamics Objects

Selecting dynamic objects is also straightforward. You select one or more objects that are simulated and in return receive a tuple of hou.DopObject. These objects represent actual simulated objects and can be used to query information about the object, the simulation and various creator data. There are the same objects that are processed using custom Python DOPs.

Selecting Positions

When you execute shelf tools on the Create tab, you usually begin with selecting a position in world space to place the new object. This is accomplished by calling hou.SceneViewer.selectPosition(). This function provides you the ability to select one or more positions in world space, or in the viewports XY or UV space and returns the positions as a tuple of hou.Vector3. You can also pass a hou.BoundingBox to the function to represent the bounds of some object and Houdini will display that bounding box as you select.

import toolutils
viewer = toolutils.sceneViewer()
# Select a position in world space.
positions = viewer.selectPosition("Select your favorite position and press Enter to complete.")
my_position = positions[0]
 
# Select a position in viewport XY space.
positions = viewer.selectPosition("Select your favorite position and press Enter to complete.", position_type=positionType.ViewportXY)
my_position = positions[0]
 
# Add a bounding box display to the selection. 
# Display a unit sized cube, like when using the Box tool.
bounding_box = hou.BoundingBox(-0.5, -0.5, -0.5, 0.5, 0.5, 0.5)
positions = viewer.selectPosition("Select your favorite position and press Enter to complete.", bbox=bounding_box)
my_position = positions[0]
© 2009 od[force].net | advertise