FeaturePyton. What happens when a user opens a document?

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
Evgeniy
Posts: 477
Joined: Thu Jul 15, 2021 6:10 pm

FeaturePyton. What happens when a user opens a document?

Post by Evgeniy »

FeaturePyton. What happens when a user opens a document?

Is this sequence described somewhere?

I noticed that when opening a FeaturePython document, properties are assigned, but for some reason _init_() is not called. What then should initialize the object?
User avatar
Evgeniy
Posts: 477
Joined: Thu Jul 15, 2021 6:10 pm

Re: FeaturePyton. What happens when a user opens a document?

Post by Evgeniy »

If I understood correctly

When loading, all values are iterated, starting from Proxy value, and the onChanged(self, obj, prop) method is called for each itarated value.

There is a method onDocumentRestored(self, obj), but it starts after iterating through all the values.

https://wiki.freecadweb.org/FeaturePython_methods

After reading this branch as an output, is possible to check the connections between objects when the onChange method invoked.

https://forum.freecadweb.org/viewtopic.php?t=13740

It would be nice to have the OnInit() method for self-initialization. And so that this method is called before iterating over variables. Although it is possible to all without it.
User avatar
Chris_G
Veteran
Posts: 2578
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: FeaturePyton. What happens when a user opens a document?

Post by Chris_G »

You can deactivate the onChanged method during document loading with :

Code: Select all

def onChanged( self, fp, prop):
    if "Restore" in fp.State:
        return
    ...
Then I think you can use onDocumentRestored as your an init method, called once all the properties of the object are fully restored.
User avatar
Evgeniy
Posts: 477
Joined: Thu Jul 15, 2021 6:10 pm

Re: FeaturePyton. What happens when a user opens a document?

Post by Evgeniy »

Chris_G wrote: Tue Dec 06, 2022 2:26 pm Then I think you can use onDocumentRestored as your an init method, called once all the properties of the object are fully restored.
I use construction like this:

Code: Select all

    def onChanged(self, obj, prop):
        App.Console.PrintMessage("Change property: " + str(prop) + "\n")
        val = obj.getPropertyByName(prop)
        if prop == "ObjectData":
            featureobj = self.createObjectIfItNotExists()
            featureobj.data = val
    ...
onDocumentRestored() turned out to be useless for me, because I can't apply properties to an object that hasn't been created yet. Therefore, a check is performed, if the object does not exist, then it is created. And only then the property is applied.
User avatar
Chris_G
Veteran
Posts: 2578
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: FeaturePyton. What happens when a user opens a document?

Post by Chris_G »

I am sorry, but I lack more context and info to help further.
What I understand is that you have a FeaturePython object (obj) that creates a FeatureObject (featureobj) and transfers the data of the ObjectData property (what type of property is it ?) to it.
Without more context, this looks quite convoluted.
User avatar
Evgeniy
Posts: 477
Joined: Thu Jul 15, 2021 6:10 pm

Re: FeaturePyton. What happens when a user opens a document?

Post by Evgeniy »

Chris_G wrote: Wed Dec 07, 2022 9:46 am I am sorry, but I lack more context and info to help further.
Thanks. All fine, the issue is resolved.
Post Reply