Iterate Through Scene Object to Find Parent and Apply Gizmo Transformation To It

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
SBlade
Posts: 40
Joined: Fri May 01, 2020 8:38 pm

Iterate Through Scene Object to Find Parent and Apply Gizmo Transformation To It

Post by SBlade »

Dear All,

In my scene I have primitive objects and other nested objects(mostly precast from Arch workbench) under the part container please refer to image for scene composition. In order to have easy to manage scene to user, I'm planning to use everything in my scene as modular blocks so that end-user can select modify move, rotate and manipulate it easily with Gui.runCommand('Std_TransformManip',0) command. It is convenient and visually appealing.
image.png
image.png (19.53 KiB) Viewed 669 times
Having said that, based on the user selection if user selects an object in scene(say that it is Pillar004) and if it has highest order parent ( in that case it Part) then I'll have to iterate programmatically to select parent (i.e. Part) and apply the transformation to it. In order to achieve that, I followed the instructions in that post https://forum.freecadweb.org/viewtopic.php?t=39779 and code here https://github.com/s-light/io_import_fc ... cts.py#L98

I tried following code w/o success: It simply returns nothing, but doesn't posts an error as well. I think that iteration never enters after the if len(o.Hosts)==0 code line.

Code: Select all

host_objects = []
res_objects=[]
sel = FreeCADGui.Selection.getSelection() # " sel " contains the items selected
if len(sel)> 0:
    for o in sel:
        if len(o.Parents)==0:
            if hasattr(o, "Hosts"):
                if len(o.Hosts)==0:
                    res_objects.append(o)
                else:
                    host_objects.append(o)
                    FreeCADGui.Selection.clearSelection()
                    FreeCADGui.Selection.AddSelection(o)
                    Gui.runCommand('Std_TransformManip',0)
            else:
                res_objects.append(o)
                FreeCADGui.Selection.clearSelection()
                FreeCADGui.Selection.AddSelection(o)
                Gui.runCommand('Std_TransformManip',0)
                print(len(res_objects))
I believe that The problem is 2 fold:
1st: Algorithm to iterate through parent objects.
2nd: After finding the highest order parent element select and apply the Transformation to it.

P.S. Just for clarification if I don't place the Arch precast elements in Part container transformation is not applicable to them, for other primitive objects tranformation works fine.

Your guidance will be appreciated.
OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Installed mods:
* BIM 2021.12.0
* Manipulator 1.4.5
Post Reply