Saving changes to properties

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
tomkcook
Posts: 87
Joined: Wed Jul 20, 2016 3:39 pm

Saving changes to properties

Post by tomkcook »

I have an App::FeaturePython which has an App::PropertyVectorList. I create it like this:

Code: Select all

    obj.addProperty('App::PropertyVectorList', 'Points', 'Hull', 'Net of points').Points = [FreeCAD.Vector(x,y,z) for x,z,y in coords]
I've got a custom Marker type that uses an element from that property as its point:

Code: Select all

class HullPoint(graphics.Marker):
    def __init__(self, vertices, index):
        super().__init__([vertices[index]], True)
        self.vertices = vertices
        self.index = index
        self.on_drag_release.append(self.moved)
    
    def _update_vertex(self, v):
        self.vertices[self.index] = v
    
    def moved(self):
        self._update_vertex(FreeCAD.Vector(self.points[0]))
The idea is that when the marker is dragged, it will update the co-ordinates in the PropertyVectorList. I add a bunch of markers to the scene:

Code: Select all

	points = [[None] for jj in range(n)] for ii in range(m)]
        vertex_data = obj.Object.getPropertyByName('Points')
        for ii in range(m):
            for jj in range(n):
                points[ii][jj] = HullPoint(vertex_data, ii * n + jj)
This all appears to work... but when I grab a marker and move it, save and reload the document, it's back in its original place. Am I doing something wrong?
tomkcook
Posts: 87
Joined: Wed Jul 20, 2016 3:39 pm

Re: Saving changes to properties

Post by tomkcook »

To follow up on this, it seems that obj.getPropertyByName("Points") is returning a plain Python list, not an App::PropertyVectorList proxy. So there's no way to call eg set1Value() on it. Changing the values in the list isn't being reflected in the property value.

Is there some way to get a reference to the actual property object so that it can be updated?
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Saving changes to properties

Post by openBrain »

Dose 'obj.Points' exist after you create the property?
tomkcook
Posts: 87
Joined: Wed Jul 20, 2016 3:39 pm

Re: Saving changes to properties

Post by tomkcook »

openBrain wrote: Wed Jul 21, 2021 10:47 am Dose 'obj.Points' exist after you create the property?
Yes, but this returns the same result as obj.getPropertyByName("Points").
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Saving changes to properties

Post by openBrain »

tomkcook wrote: Thu Jul 22, 2021 8:48 am Yes, but this returns the same result as obj.getPropertyByName("Points").
OK, I get some issue exactly understanding your case, so I took a simple example.
I created a simple document with just a Part/Cube and ran this :

Code: Select all

obj = App.ActiveDocument.Box
obj.addProperty("App::PropertyVectorList", "PropName", "PropGroup", "PropTooltip")
obj.PropName = [(10,10,10),(20,20,20)]
obj.Document.recompute()
obj.Document.saveAs("/tmp/test.FCStd")
I then closed and reopen the document and property is perfectly saved and restored. Does this help in some way ?

BTW, I saw 2 issues while doing this :
1) When running the 'save()/saveAs()' functions on a document --code below--, it is correctly saved but still marked with a '*' (as unsaved) and if you try to close it on the GUI, it will prompt for saving...

Code: Select all

App.ActiveDocument.saveAs("/tmp/test.FCStd")
2) There is no check that the added property doesn't overwrite an existing attribute. Eg. if you create a property called 'Name' --code below--, it works and object name is no more reachable

Code: Select all

obj.addProperty("App::PropertyVectorList", "Name", "PropGroup", "PropTooltip") #obj to be defined previously
If one can confirm these 2 issues exist and are bugs, I'll create tickets for them
Post Reply