Compound Objects created in Python do not Behave like Compounds created through GUI

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
jmdodd95682
Posts: 2
Joined: Mon Jun 21, 2021 11:52 pm

Compound Objects created in Python do not Behave like Compounds created through GUI

Post by jmdodd95682 »

If I have N objects and I call the Python script:

self.mCompound=FreeCAD.activeDocument().addObject("Part::Compound",gname)
self.mCompound.Links=[App.activeDocument().obj0,App.activeDocument().obj1,App.activeDocument().obj2,...]
FreeCAD.activeDocument().recompute()

I get no errors. However, when I open the file I create which has this compound object, it has a strange behavior.
If I select the compound object and try to move it, it leaves behind a "Shadow" copy of the linked objects. If I select any of the linked
objects it behaves as if I'm selecting the object in both places. It's like the object exists in the GUI has both Linked and not linked, although
in the Tree View it is clearly shown as a subcomponent on the Compound object.

Now, when I do this through the GUI Part->Compound->Make Compound I do not see this behavior. Moving the compound object
does NOT leave a shadow copy of the linked objects behind. The Python code it generates to the Console is identical to what I have above.
So, it seems there is some "extra" scripting I need to avoid this behavior.

I have tried many things, but there is no way I can determine of avoiding this if the Compound object was made via a Python script and then written to a file. Any help here would be appreciated.
chrisb
Veteran
Posts: 54197
Joined: Tue Mar 17, 2015 9:14 am

Re: Compound Objects created in Python do not Behave like Compounds created through GUI

Post by chrisb »

Hi and welcome to the forum!

Is this possibly only a visibility thing? Make all objects inside of the compound invisible (that's what make compound does), and check if it then behaves as expected.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
jmdodd95682
Posts: 2
Joined: Mon Jun 21, 2021 11:52 pm

Re: Compound Objects created in Python do not Behave like Compounds created through GUI

Post by jmdodd95682 »

Yes! That works! I had not tried that. Thanks!

This leads me to another related question then...

How can I, through a Python script, can I control visibility of an object. Obviously I can do this:

FreeCADGui.ActiveDocument.getObject(obj.Name).show()

But that only works if you are running the script through the tool.
I prefer to run my scripts through VS Code and then use FreeCAD to view the results.
I've noticed that when I generate objects in this way, I must manually make them visible with a Macro which I run
after loading the file.

Is there a way to force objects to be visible other than through the GUI or FreeCADGui? Clearly I need some way of doing
this (make them NOT visible in that case) for Compound parts as you have pointed out.
chrisb
Veteran
Posts: 54197
Joined: Tue Mar 17, 2015 9:14 am

Re: Compound Objects created in Python do not Behave like Compounds created through GUI

Post by chrisb »

jmdodd95682 wrote: Tue Jun 22, 2021 12:56 am How can I, through a Python script, can I control visibility of an object. Obviously I can do this:

FreeCADGui.ActiveDocument.getObject(obj.Name).show()
If you have already obj, there is no need to get its name and then search the active document for the object with this name. It will of course be the object you started with.

To hide an object set its Visibility property to False.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply