Working with PropertyEnumeration in Python

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
sliptonic
Veteran
Posts: 3459
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Working with PropertyEnumeration in Python

Post by sliptonic »

I'm typically add an enum property to a FeaturePython object and initialize its value like this:

Code: Select all

obj.addProperty("App::PropertyEnumeration", "myproperty")
obj.myproperty = ['first', 'second', 'third']
obj.myproperty = 'third'
But I have a case where I can't populate the enumeration until after the proxy is set. So when I populate it and then set it, the onChanged event is called twice. How can I detect if this onchanged event is firing because the enum is getting populated rather than the value actually changing?

I tried looking at the property's value, but in the first call it is equal to the first item in the list.
Paula
Posts: 48
Joined: Tue Jan 26, 2021 10:06 pm

Re: Working with PropertyEnumeration in Python

Post by Paula »

We often get these sorts of issues in web application development. The best method I have found is to remove the GUI widget's onChange() callback, set the data then add back the onChange() callback.
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Working with PropertyEnumeration in Python

Post by onekk »

Another way could be a "freeze" of the automagics.

In some interface, there is a way to say "stop to reacting" to something, and then "restore the normal behaviour".

In this way when dealing with complex things, you can "freeze" the object modify all the things, some operations could affect the "automagic" so some things could became broken during modifications, once done the modification you should restart the normal behaviour and update the thing.

Now I don't know if simply adding such methods (stop and ) could be viable or simple, like I have seen recently some similar methods in PathJobGui.py

Code: Select all

   FreeCAD.ActiveDocument.openTransaction(translate("Path_Job", "Create Job"))

   FreeCAD.ActiveDocument.commitTransaction()

   FreeCAD.ActiveDocument.abortTransaction()


Simply as an example.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Chris_G
Veteran
Posts: 2601
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Working with PropertyEnumeration in Python

Post by Chris_G »

Hi,
sliptonic wrote: Wed Jun 02, 2021 3:37 am How can I detect if this onchanged event is firing because the enum is getting populated rather than the value actually changing?
Maybe you can store the actual enumeration in the proxy instance and check if it has changed in onChanged() :

Code: Select all

class myProxy:
    def __init__(self, obj):
        self.prop_enumeration = ["a", "b", "c"]
        obj.addProperty("App::PropertyEnumeration", "myProp").myProp = self.prop_enumeration

    def onChanged(self, obj, prop):
        if prop == "myProp":
            if not obj.getEnumerationsOfProperty("myProp") == self.prop_enumeration:
                self.prop_enumeration = obj.getEnumerationsOfProperty("myProp")
                print("myProp : enumeration has changed")
            else:
                print("myProp : value has changed")
Post Reply