removeEventCallbackPivy from console

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

removeEventCallbackPivy from console

Postby Chri1 » Mon May 20, 2019 11:57 am

Hi

I have an EventCallback which fixes an AnnotationLabel at the Cursor and is finished by mouse-left-click:

Code: Select all

import FreeCADGui, Part
from pivy.coin import *
import FreeCAD
App = FreeCAD
import Draft

objectAnn = None
class cursorInf:
    global objectAnn
    global gp
    def __init__(self): 
        print("anf")
        FreeCADGui.runCommand("Draft_Line")
        print("end")
        global objectAnn
        objectAnn = App.ActiveDocument.addObject("App::AnnotationLabel","FCInfoToMouse")     
        self.view = FreeCADGui.ActiveDocument.ActiveView
        self.callback = self.view.addEventCallbackPivy(SoEvent.getClassTypeId(),self.getpoint)    
        print(self.callback)
        gp = self.callback
        objectAnn.LabelText=["Hello"]
     
    def getpoint(self,event_cb):
        global objectAnn
        event = event_cb.getEvent()
        if type(event) == SoMouseButtonEvent:
             self.view.removeEventCallbackPivy(SoEvent.getClassTypeId(),self.callback)
             print("Ende")
        else:
             #print("Callback")
             pos = event.getPosition()
             point = self.view.getPoint(pos[0],pos[1])        
             objectAnn.BasePosition = point
How can I finish the callback from outside - from python-console or another function?
I tried quite a bit like
Gui.ActiveDocument.ActiveView.removeEventCallbackPivy(SoEvent.getClassTypeId(), ??something with callback?? ) but I didn't succeed

Maybe this would help: How can I simulate a mouse-click by python code?

Thanks Chri1
wmayer
Site Admin
Posts: 14798
Joined: Thu Feb 19, 2009 10:32 am

Re: removeEventCallbackPivy from console

Postby wmayer » Mon May 20, 2019 3:32 pm

Code: Select all

from pivy import coin
inst = cursorInf()
inst.view.removeEventCallbackPivy(coin.SoEvent.getClassTypeId(), inst.callback)
Chri1
Posts: 50
Joined: Wed Oct 17, 2018 9:00 am

Re: removeEventCallbackPivy from console

Postby Chri1 » Tue Aug 13, 2019 3:07 pm

Another question:
The Information should be visible only until mouseclick.

But whole FreeCAD crashes with:

Code: Select all

....
def getpoint(self,event_cb):
        global objectAnn
        event = event_cb.getEvent()
        if type(event) == SoMouseButtonEvent:
             print("Ende")
             self.view.removeEventCallbackPivy(SoEvent.getClassTypeId(),self.callback)
             objectAnn.Document.removeObject(objectAnn.Name)   # !!!!!!!!!!!!!!!!!          HERE IT CRASHES
…..

Thanks
Chri1
User avatar
Joel_graff
Posts: 1499
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: removeEventCallbackPivy from console

Postby Joel_graff » Tue Sep 24, 2019 2:14 pm

Chri1 wrote:
Tue Aug 13, 2019 3:07 pm
But whole FreeCAD crashes with:
Is your object reference valid?

You might want to save a reference to the document separately, typically using App.ActiveDocument.
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