Discussion:
[vtkusers] Retriving vtkInformation from the actor
Abhishek
2018-11-05 05:10:08 UTC
Permalink
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
2018-11-06 05:39:58 UTC
Permalink
Anyone? how to determine which object it is in the mouse event?
Post by Abhishek
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?
```
import vtk
NUMBER_OF_SPHERES = 10
self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
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
# 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
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
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)
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
Fahlgren, Eric
2018-11-06 14:57:52 UTC
Permalink
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-***@public.kitware.com> On Behalf Of Abhishek
Sent: Monday, November 5, 2018 9:40 PM
To: ***@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 <***@gmail.com<mailto:***@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
Abhishek
2018-11-07 01:18:07 UTC
Permalink
Hi Eric,
Thanks for the response.
they `actor.key` hack works fine in python but what would be equivalent of
that in c++?
I don't think there is an easy way in C++ and that's why I was going
through vtkinformationStringKey.
Any thoughts?


On Wed, Nov 7, 2018 at 1:57 AM Fahlgren, Eric <
Post by Fahlgren, Eric
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),
# 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.
Abhishek
*Sent:* Monday, November 5, 2018 9:40 PM
*Subject:* Re: [vtkusers] Retriving vtkInformation from the actor
Anyone? how to determine which object it is in the mouse event?
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?
```
import vtk
NUMBER_OF_SPHERES = 10
self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
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
# 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
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
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)
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
--
Abhishek
http://zeroth.me
Fahlgren, Eric
2018-11-07 15:42:03 UTC
Permalink
Hmm, yeah, that’s much harder if the Get/SetInformation doesn’t work out. Maybe keep a map<vtkActor*, some_data> from the actor’s object pointer to your data? (Or just use Python. 😊)

From: Abhishek <***@gmail.com>
Sent: Tuesday, November 6, 2018 5:18 PM
To: Fahlgren, Eric <***@smith-nephew.com>
Cc: ***@public.kitware.com
Subject: Re: [vtkusers] Retriving vtkInformation from the actor

Hi Eric,
Thanks for the response.
they `actor.key` hack works fine in python but what would be equivalent of that in c++?
I don't think there is an easy way in C++ and that's why I was going through vtkinformationStringKey.
Any thoughts?


On Wed, Nov 7, 2018 at 1:57 AM Fahlgren, Eric <***@smith-nephew.com<mailto:***@smith-nephew.com>> wrote:
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-***@public.kitware.com<mailto:vtkusers-***@public.kitware.com>> On Behalf Of Abhishek
Sent: Monday, November 5, 2018 9:40 PM
To: ***@public.kitware.com<mailto:***@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 <***@gmail.com<mailto:***@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
--
Abhishek
http://zeroth.me
Abhishek
2018-11-08 22:46:15 UTC
Permalink
Thanks for the tip even I was thinking of keeping the list of objects as a
worst-case scenario. :)

On Thu, Nov 8, 2018 at 2:42 AM Fahlgren, Eric <
Post by Fahlgren, Eric
Hmm, yeah, that’s much harder if the Get/SetInformation doesn’t work out.
Maybe keep a map<vtkActor*, some_data> from the actor’s object pointer to
your data? (Or just use Python. 😊)
*Sent:* Tuesday, November 6, 2018 5:18 PM
*Subject:* Re: [vtkusers] Retriving vtkInformation from the actor
Hi Eric,
Thanks for the response.
they `actor.key` hack works fine in python but what would be equivalent of that in c++?
I don't think there is an easy way in C++ and that's why I was going
through vtkinformationStringKey.
Any thoughts?
On Wed, Nov 7, 2018 at 1:57 AM Fahlgren, Eric <
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),
# 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.
Abhishek
*Sent:* Monday, November 5, 2018 9:40 PM
*Subject:* Re: [vtkusers] Retriving vtkInformation from the actor
Anyone? how to determine which object it is in the mouse event?
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?
```
import vtk
NUMBER_OF_SPHERES = 10
self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
self.LastPickedActor = None
self.LastPickedProperty = vtk.vtkProperty()
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
# 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
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
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)
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
--
Abhishek
http://zeroth.me
--
Abhishek
http://zeroth.me
Loading...