removeEventCallbackPivy from console

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Chri1
Posts: 86
Joined: Wed Oct 17, 2018 9:00 am

removeEventCallbackPivy from console

Post by Chri1 »

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
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: removeEventCallbackPivy from console

Post by wmayer »

Code: Select all

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

Re: removeEventCallbackPivy from console

Post by Chri1 »

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
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: removeEventCallbackPivy from console

Post by Joel_graff »

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
Post Reply