[vtkusers] ​SetClientData in python (David Gobbi)

Andrew Maclean andrew.amaclean at gmail.com
Sat Nov 11 17:46:20 EST 2017


Ok
I have got it.
This works:

def DummyFunc(obj, ev):
    print(ev, DummyFunc.data)

DummyFunc.data = renwin
# How do I pass my obj_2 ?
interactor.AddObserver('LeftButtonPressEvent', DummyFunc)


​David, you may be able to explain this better than I can but I assume this
is what happens:​
    Here we are adding a new attribute to DummyFunc. We can do this because
Python functions are in fact objects.

​This is something totally strange to C++ programmers!

What a neat approach!​



On Sun, Nov 12, 2017 at 8:59 AM, Andrew Maclean <andrew.amaclean at gmail.com>
wrote:

> David,
>
> When I try this I get:
> TypeError: DummyFunc() missing 1 required positional argument: 'data'
>
> I used a slightly different approach in  https://lorensen.github.io/
> VTKExamples/site/Python/VisualizationAlgorithms/StreamlinesWithLineWidget/
>
>
> Regards
>    Andrew
>
>>    2. Re:
> ​​
> SetClientData in python (David Gobbi)
>>
>> ---------- Forwarded message ----------
>> From: David Gobbi <david.gobbi at gmail.com>
>> To:
>> ​​
>>  "Serge K." <skab12 at gmail.com>
>> Cc: VTK Users <vtkusers at vtk.org>
>> Bcc:
>> Date: Fri, 10 Nov 2017 11:17:26 -0700
>> Subject: Re: [vtkusers] SetClientData in python
>> Hi Serge,
>>
>> The way to do this in Python is to add attributes to your callback
>> function.
>>
>>     def DummyFunc(obj, ev, data):
>>         print(ev, data)
>>
>>
>> ​​
>> DummyFunc.CallData = whatever
>>
>>     interactor.AddObserver('LeftButtonPressEvent', DummyFunc)
>>
>> So the vtkCallbackCommand really isn't needed in Python (and, as you have
>> noticed, it really can't be used in Python anyway).
>>
>>  - David
>>
>>
>>
>> On Fri, Nov 10, 2017 at 10:32 AM, Serge K. <skab12 at gmail.com> wrote:
>>
>>> Hi All,
>>>
>>> I am playing with MouseEventObserver.py example and I would like to add
>>> a ClientData on my callback function. There are many examples in C++ like
>>> this one: https://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/C
>>> lientData
>>>
>>> However, I do not know how to do that in Python.
>>>
>>> Indeed, if I create a  vtkCallbackCommand , SetCallBack function does
>>> not exist.
>>>
>>> Any Idea? Thanks for your answer.
>>>
>>> Serge
>>>
>>> ps: python36 - vtk 7.1.1
>>>
>>>
>>> ​​
>>> #!/usr/bin/env pythonfrom __future__ import print_function
>>> import vtksource = vtk.vtkSphereSource()source.SetCenter(0, 0, 0)source.SetRadius(1)source.Update()
>>> mapper = vtk.vtkPolyDataMapper()mapper.SetInputConnection(source.GetOutputPort())
>>> actor = vtk.vtkActor()actor.SetMapper(mapper)
>>> renderer = vtk.vtkRenderer()renderer.SetBackground(1, 1, 1)renderer.AddActor(actor)
>>> renwin = vtk.vtkRenderWindow()renwin.AddRenderer(renderer)
>>> interactor = vtk.vtkRenderWindowInteractor()interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())interactor.SetRenderWindow(renwin)
>>> def DummyFunc(obj, ev, my_obj_2):
>>>     print(obj, my_obj_2)
>>>
>>> # obj_2 = vtk.whatever()# How do I pass my obj_2 ? interactor.AddObserver('LeftButtonPressEvent', DummyFunc)interactor.Initialize()interactor.Start()
>>>
>>>
>>
>>
> --
> ___________________________________________
> Andrew J. P. Maclean
>
> ___________________________________________
>



-- 
___________________________________________
Andrew J. P. Maclean

___________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20171112/999397b4/attachment.html>


More information about the vtkusers mailing list