Watching an object for property changes

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Watching an object for property changes

Post by Joel_graff »

So I'm trying to sort out how to watch another object for changes in it's properties. Essentially, I'm creating a FeaturePython object which takes a Line object as a child. I'm using the FeaturePython object to store metadata about the line, for example, it's bearing w.r.t the unit Y axis.

So, the Bearing property of the FP object, then, needs to update whenever the Line object changes...
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
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Watching an object for property changes

Post by TheMarkster »

Should be able to use setExpression() to do it. For example, given an object named Cylinder and another named Line, if we wanted Cylinder's Height property to always be equal to Line's Y2 - Y1 value:

Code: Select all

App.ActiveDocument.Cylinder.setExpression('Height',u'Line.Y2-Line.Y1')
User avatar
Chris_G
Veteran
Posts: 2602
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Watching an object for property changes

Post by Chris_G »

Joel_graff wrote: Fri Jun 15, 2018 4:46 pm So I'm trying to sort out how to watch another object for changes in it's properties. Essentially, I'm creating a FeaturePython object which takes a Line object as a child. I'm using the FeaturePython object to store metadata about the line, for example, it's bearing w.r.t the unit Y axis.

So, the Bearing property of the FP object, then, needs to update whenever the Line object changes...
What about using a App::PropertyLink property to have your FeaturePython object "triggered" when the Line object is changed :

Code: Select all

class myFeaturePythonProxy(object):
    def class __init__(self, fp):
        fp.addProperty("App::PropertyLink", "LineObject", "myFP","Link to Line object")
        fp.addProperty("App::PropertySomething", "Bearing", "myFP","Bearing")


    def execute(self, fp):
        # usually, execute is run whenever one of its child objects is touched.
        update_bearing_property(fp)
        # ... usual code here

    def update_bearing_property(self, fp):
        # check fp.LineObject properties
        fp.Bearing = ... something
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Watching an object for property changes

Post by ickby »

you can simply install a document observer which is called when a object changes. I did not find a python example, but in the cpp file you can see the kind of functions that are exposed:
https://github.com/FreeCAD/FreeCAD/blob ... erPython.h
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: Watching an object for property changes

Post by Joel_graff »

ickby wrote: Wed Jun 20, 2018 11:45 am you can simply install a document observer which is called when a object changes. I did not find a python example, but in the cpp file you can see the kind of functions that are exposed:
Fascinating. I'll keep that in mind. I'm starting to sort the problem out, but if I can't solve it, this might do the trick...
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
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

[SOLVED] Watching an object for property changes

Post by Joel_graff »

Solved this. The trick was to create a group object and add the object to be watched as a child. This triggers an auto update of the parent.
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