addEventCallPivy - type of event

Need help, or want to share a macro? Post here!
Chri1
Posts: 68
Joined: Wed Oct 17, 2018 9:00 am

addEventCallPivy - type of event

Postby Chri1 » Thu Oct 10, 2019 9:52 am

Hi
I am able, to make an observer by using addEventCall.
Apparently its better to use addEventCallPivy. Really, why?

I made an "Soevent" and don't know, how to recognize, which event has happened:
With addEventCall I used:

Code: Select all

if arg["Type"] == "SoKeyboardEvent":
     .....   
But with addEventCallPivy?

(The step after this I know:

Code: Select all

inp = arg.getEvent().getKey()
if inp == coin.SoKeyboardEvent.ESCAPE:
   ...
)
vocx
Posts: 4074
Joined: Thu Oct 18, 2018 9:18 pm

Re: addEventCallPivy - type of event

Postby vocx » Thu Oct 10, 2019 6:58 pm

Chri1 wrote:
Thu Oct 10, 2019 9:52 am
Hi
I am able, to make an observer by using addEventCall.
Apparently its better to use addEventCallPivy. Really, why?
...
I'm also trying to figure things out. In some DraftGui function I see something like this.

Code: Select all

self.view = Draft.get3DView()
self.callbackMove = self.view.addEventCallbackPivy(coin.SoLocation2Event.getClassTypeId(), self.move)

def move(self, event_cb):
    event = event_cb.getEvent()
    mousepos = event.getPosition().getValue()
    ctrl = event.wasCtrlDown()
    self.point = Gui.Snapper.snap(mousepos, active=ctrl)
It seems the callback passes event_cb to move(), and the information is extracted from this even_cb class.

The Gui.Snapper.snap() is a function that uses the Draft snapper to return a point attached to the Draft grid, if applicable. I guess it's optional, and if you don't need it, you can use self.point = mousepos directly.

See this as well, Pivy_trackers 2D geometry library. It's an attempt to abstract the 2D trackers that are present in particular in DrafrTrackers.py.
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
User avatar
Joel_graff
Posts: 1729
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: addEventCallPivy - type of event

Postby Joel_graff » Fri Oct 11, 2019 9:41 am

There are two ways to add event callbacksm using either addEventCallback() or addEventCallbackPivy(). Either one is fine. Just a note: if you want to ever remove the event callback you've added, you need to save the object reference these functions return and use that (instead of the original callback function itself). This has to do with the way it's implemented in pivy.

The issue in your particular case appears to be testing for the event type. addEventCallback() returns everything in a nice Python dictionary, whereas addEventCallbackPivy() returns what you'd get if you were using coin3d directly in C++.

Example:

Code: Select all

def my_keyboard_callback(self, so_event_cb):
    print(so_event_cb.getEvent().getClassTypeId())

def add_event_callback(self):
    Gui.ActiveDocument.ActiveView.addEventCallbackPivy(coin.SoKeyboardEvent().getClassTypeId(), self.my_keyboard_callback)
Note your callback must be part of a class object. Static methods (with no self parameter), so far as I know, will not work.

Refer to the https://grey.colorado.edu/coin3d/classS ... Event.html for more information on what to do with the event returned by getEvent(), if you haven't already.
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
User avatar
looo
Posts: 3301
Joined: Mon Nov 11, 2013 5:29 pm

Re: addEventCallPivy - type of event

Postby looo » Fri Oct 11, 2019 10:23 am

Joel_graff wrote:
Fri Oct 11, 2019 9:41 am
Note your callback must be part of a class object. Static methods (with no self parameter), so far as I know, will not work.
hmm, I don't think there is such a limitation. At least if you use a coin.SoEvenCallback node:

Code: Select all

from pivy import coin

sg = Gui.ActiveDocument.ActiveView.getSceneGraph()
class test_class(object):
    @staticmethod
    def test1(*args):
        print("this is a static-function")
        print([arg for arg in args])
    @classmethod
    def test2(*args):
        print("this is a class-function")
        print([arg for arg in args])
    def test3(*args):
        print("this is a object-fuction")
        print([arg for arg in args])
    def __init__(self, sg):
        self.cb_node = coin.SoEventCallback()
        sg += self.cb_node
        self.cb1 = self.cb_node.addEventCallback(coin.SoLocation2Event.getClassTypeId(), test_class.test1, "extra arg")
        self.cb2 = self.cb_node.addEventCallback(coin.SoLocation2Event.getClassTypeId(), test_class.test2, "extra arg")
        self.cb3 = self.cb_node.addEventCallback(coin.SoLocation2Event.getClassTypeId(), self.test3, "extra arg")

test_obj = test_class(sg)

results in:

Code: Select all

this is a static-function
['extra arg', <pivy.coin.SoEventCallback; proxy of <Swig Object of type 'SoEventCallback *' at 0x7f3ad1db5360> >]

this is a class-function
[<class '__main__.test_class'>, 'extra arg', <pivy.coin.SoEventCallback; proxy of <Swig Object of type 'SoEventCallback *' at 0x7f3ad1db5360> >]

this is a object-fuction
[<__main__.test_class object at 0x7f3ad1dc05c0>, 'extra arg', <pivy.coin.SoEventCallback; proxy of <Swig Object of type 'SoEventCallback *' at 0x7f3ad1db5360> >]
only the passed arguments differ.
User avatar
Joel_graff
Posts: 1729
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: addEventCallPivy - type of event

Postby Joel_graff » Fri Oct 11, 2019 1:56 pm

Good to know. I tried it once a couple weeks ago and it failed, but didn’t put a lot of effort into sorting out why. Obviously messed something up...
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
Chri1
Posts: 68
Joined: Wed Oct 17, 2018 9:00 am

Re: addEventCallPivy - type of event

Postby Chri1 » Sat Oct 12, 2019 12:14 pm

Joel_graff wrote:
Fri Oct 11, 2019 9:41 am
Either one is fine.
I'll stay at addEventCallback()
Somehow it seems more handy for me ...

Incidentally, I just noticed that I forgot on "back" in my post (addEventCallback)

Thanks to all of you