HighlightPickedActor

VTKExamples/Python/Interaction/HighlightPickedActor

Description

Shows how to customize vtkInteractorStyleTrackballCamera and vtkPropPicker to select a actor in the scene by pressing the left mouse button.

Code

HighlightPickedActor.py

import vtk

NUMBER_OF_SPHERES = 10


class MouseInteractorHighLightActor(vtk.vtkInteractorStyleTrackballCamera):

    def __init__(self, parent=None):
        self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)

        self.LastPickedActor = None
        self.LastPickedProperty = vtk.vtkProperty()

    def leftButtonPressEvent(self, obj, event):
        clickPos = self.GetInteractor().GetEventPosition()

        picker = vtk.vtkPropPicker()
        picker.Pick(clickPos[0], clickPos[1], 0, self.GetDefaultRenderer())

        # get the new
        self.NewPickedActor = picker.GetActor()

        # If something was selected
        if self.NewPickedActor:
            # If we picked something before, reset its property
            if self.LastPickedActor:
                self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)

            # Save the property of the picked actor so that we can
            # restore it next time
            self.LastPickedProperty.DeepCopy(self.NewPickedActor.GetProperty())
            # Highlight the picked actor by changing its properties
            self.NewPickedActor.GetProperty().SetColor(1.0, 0.0, 0.0)
            self.NewPickedActor.GetProperty().SetDiffuse(1.0)
            self.NewPickedActor.GetProperty().SetSpecular(0.0)

            # save the last picked actor
            self.LastPickedActor = self.NewPickedActor

        self.OnLeftButtonDown()
        return


# A renderer and render window
renderer = vtk.vtkRenderer()
renderer.SetBackground(.3, .4, .5)


renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(renderer)

# An interactor
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renwin)

# add the custom style
style = MouseInteractorHighLightActor()
style.SetDefaultRenderer(renderer)
interactor.SetInteractorStyle(style)

# Add spheres to play with
for i in range(NUMBER_OF_SPHERES):
    source = vtk.vtkSphereSource()

    # random position and radius
    x = vtk.vtkMath.Random(-5, 5)
    y = vtk.vtkMath.Random(-5, 5)
    z = vtk.vtkMath.Random(-5, 5)
    radius = vtk.vtkMath.Random(.5, 1.0)

    source.SetRadius(radius)
    source.SetCenter(x, y, z)
    source.SetPhiResolution(11)
    source.SetThetaResolution(21)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(source.GetOutputPort())
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    r = vtk.vtkMath.Random(.4, 1.0)
    g = vtk.vtkMath.Random(.4, 1.0)
    b = vtk.vtkMath.Random(.4, 1.0)
    actor.GetProperty().SetDiffuseColor(r, g, b)
    actor.GetProperty().SetDiffuse(.8)
    actor.GetProperty().SetSpecular(.5)
    actor.GetProperty().SetSpecularColor(1.0, 1.0, 1.0)
    actor.GetProperty().SetSpecularPower(30.0)

    renderer.AddActor(actor)

# Start
interactor.Initialize()
interactor.Start()