SHELF_center_pivotCenter Pivot to Points object tool

Center the pivot of one or more objects to the center of one or more points.

Info
Date 01/12/2011
Compatibility Houdini 10.0
Change Log Added the Center Pivot to Points shelf tool.
Tool Info
Submenu Modify
Viewer OBJ
#
# Produced by:
#       Graham Thompson
#       captainhammy@gmail.com
#       www.captainhammy.com
#
# Name:         object_centerpivottopoints.shelf
#
# Comments:     Center the pivot of objects to the center of selected points.
#
# Version:      1.0
#
# Compatibility: Houdini 10.0
#
 
import toolutils
 
# Get the pane the tool was invoked in
activepane = toolutils.activePane(kwargs)
 
# Throw an error if it wasn't a scene viewer.
if not isinstance(activepane, hou.SceneViewer):
    raise hou.OperationFailed("No scene viewer detected.")
 
# Select the object(s) to center the pivots to.
object_prompt = "Select the object(s) to center the pivot of and press Enter to continue."
selected_objects = activepane.selectObjects(object_prompt)
# Throw an error if nothing was selected.
if len(selected_objects) is 0:
    raise hou.OperationFailed("No object was selected.")
# Clear the selection.
selected_objects[-1].setSelected(False, True)
# Select the points to center to.
points_prompt = "Select the point(s) to center to and press Enter to complete."
geometry_selection = activepane.selectGeometry(points_prompt,
                                               allow_obj_sel=False,
                                               use_existing_selection=False,
                                               geometry_types=(hou.geometryType.Points,))
 
# Throw an error if no points were selected. In ordere for points to have been selected
# there must be a selected node.
if len(geometry_selection.nodes()) is 0:
    raise hou.OperationFailed("No geometry was selected.")
# Get the geometry from the geometry node that was selected.
geometry = geometry_selection.nodes()[0].geometry()
# Get a tuple of points using the point numbers from our selection.
points = geometry.globPoints(geometry_selection.mergedSelectionString())
 
# Calculate the center of the selected points.
position = sum((point.position() for point in points), hou.Vector3()) * (1.0 / len(points))
 
# For each object we selected, set the Pivot parameter to the center of our point selection.
for target in selected_objects:
    target.parmTuple("p").set(position)
    target.setSelected(True)
# Enter the transform state.
activepane.enterCurrentNodeState()
 

Valid XHTML 1.0 Strict