[vtkusers] Retriving vtkInformation from the actor
Fahlgren, Eric
eric.fahlgren at smith-nephew.com
Tue Nov 6 09:57:52 EST 2018
I can’t tell what version of VTK you are running, but your code fails for me on the GetProperty().GetInformation() calls (I’m using VTK 8.0 today), so I just hacked a local member on the actor:
# info = actor.GetProperty().GetInformation()
# key = vtk.vtkInformationStringKey.MakeKey("CPHY_ID", "vtkActor")
# info.Set(key, "CON_"+str(i))
actor.key = "CON_"+str(i)
Ran with that, and the “CON_nnn” values are reported as expected.
From: vtkusers <vtkusers-bounces at public.kitware.com> On Behalf Of Abhishek
Sent: Monday, November 5, 2018 9:40 PM
To: vtkusers at public.kitware.com
Subject: Re: [vtkusers] Retriving vtkInformation from the actor
Anyone? how to determine which object it is in the mouse event?
On Mon, Nov 5, 2018 at 4:10 PM Abhishek <abhishekworld at gmail.com<mailto:abhishekworld at gmail.com>> wrote:
Hello,
I have multiple actors in the scene, and I have re-implemented the vtkInteractorStyleTrackballCamera to capture the click event on them (modified https://lorensen.github.io/VTKExamples/site/Python/Interaction/HighlightPickedActor/)
In the click event, I want to know which actor has been clicked to do so I am adding vtkInformationStringKey to every actor. But at the click event, I am not able to retrieve it back.
Please let me know what am I missing? Or what am I doing wrong?
Here is my code:
```
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:
# get the information from the new Actor
info = self.NewPickedActor.GetProperty().GetInformation()
# prepare the key
key = vtk.vtkInformationStringKey.MakeKey("CPHY_ID", "vtkActor")
# get the value for the key
id = info.Get(key)
print("Id is : ")
print(id)
# 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)
info = actor.GetProperty().GetInformation()
key = vtk.vtkInformationStringKey.MakeKey("CPHY_ID", "vtkActor")
info.Set(key, "SPH_"+str(i))
#actor.GetProperty().SetInformation(info)
renderer.AddActor(actor)
for i in range(NUMBER_OF_SPHERES):
source = vtk.vtkConeSource()
# random position and radius
x = vtk.vtkMath.Random(-8, 5)
y = vtk.vtkMath.Random(-8, 5)
z = vtk.vtkMath.Random(-8, 5)
radius = vtk.vtkMath.Random(.5, 1.0)
angle = vtk.vtkMath.Random(5, 10)
source.SetAngle(angle)
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)
info = actor.GetProperty().GetInformation()
key = vtk.vtkInformationStringKey.MakeKey("CPHY_ID", "vtkActor")
info.Set(key, "CON_"+str(i))
#actor.GetProperty().SetInformation(info)
renderer.AddActor(actor)
# Start
interactor.Initialize()
interactor.Start()
```
Regards,
--
Abhishek
http://zeroth.me
--
Abhishek
http://zeroth.me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20181106/f444a961/attachment.html>
More information about the vtkusers
mailing list