FreeCAD / FreeCAD/FreeCAD

Core: GUI race condition when deleting then re-adding a Body object

Open
#25,061 5 comments 0 reactions 0 assignees View on GitHub
Mod: Core Priority: Low Status: Needs confirmation Status: Needs test on stable version
Dominant language
C++
Stars
33.6k
Forks
6k
Avg merge
3d 17h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Problem description

Originally reported here: https://forum.freecad.org/viewtopic.php?t=100931

In a new project, create a Part container and name it "boat". Then run the macro attached to the forum thread or the one pasted below. The first run will be ok but the second one will attach the "hull" body outside the Part container.

```
# vim: set ft=python :
# vim: set expandtab :

import logging

import FreeCAD as App
import FreeCADGui as Gui

logger = logging.getLogger(__name__)
logging.basicConfig(encoding='utf-8', level=logging.DEBUG)

def float_boat(boat_name):
logger.info("start")

label = "hull"
doc = App.ActiveDocument or App.newDocument()

# Get reference to Part object.
boat = (
doc.getObject(boat_name) or
doc.getObjectsByLabel(boat_name))[0]
if not boat:
logger.info(f"No object called {boat_name}.")
assert False, f"No object called {boat_name}."

# Delete Body object if it already exists.
if label in [o.Label for o in FreeCAD.ActiveDocument.Objects]:
logger.debug(f"deleting Body: {label}")
doc.getObject(label).removeObjectsFromDocument()
doc.removeObject(label)
doc.recompute()

# Create new Body object.
logger.debug(f"creating Body: {label}")
hull = doc.addObject("PartDesign::Body", label)
doc.recompute()

# Add Body to Part.
boat.addObject(hull)
doc.recompute()
Gui.activateView('Gui::View3DInventor', True)
Gui.activeView().setActiveObject('pdbody', hull)

# Sometimes it shows up where it's supposed to in the GUI Model view.
# Sometimes it does not.

# Regardless of the GUI representation,
# the objects themselves look as expected.
logger.debug(f"{boat=}\t{boat.OutList=}")
for child in boat.OutList:
logger.debug(f"\t{child=} {child.Label=}")
logger.debug(f"{hull=}\t{hull.getParent()=}")
for parent in hull.Parents:
logger.debug(f"\t{parent[0]=} {parent[0].Label=}")

logger.info("end")

float_boat("boat")
```

### Full version info

```shell
OS: Windows 11 build 26200
Architecture: x86_64
Version: 1.1.0dev.20251031 (Git shallow)
Build date: 2025/10/31 03:23:50
Build type: Release
Branch: main
Hash: c270a8ac666dff22a92baf2a3a4f5200197bb678
Python 3.11.14, Qt 6.8.3, Coin 4.0.3, Vtk 9.3.1, boost 1_86, Eigen3 3.4.0, PySide 6.8.3
shiboken 6.8.3, xerces-c 3.3.0, IfcOpenShell 0.8.2, OCC 7.8.1
Locale: Polish/Poland (pl_PL)
Navigation Style/Orbit Style/Rotation Mode: Blender/Rounded Arcball/Window center
Stylesheet/Theme/QtStyle: FreeCAD.qss/FreeCAD Light/
Logical DPI/Physical DPI/Pixel Ratio: 96/81.5973/1
```

### Subproject(s) affected?

Core

### Anything else?

_No response_

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the pasted Python macro in a new FreeCAD project, running it twice and observing the Model view. Trace the Core GUI handling of object removal, re-creation, and Part containment updates; done means the re-added Body appears inside the Part container on the second run as well as in the object relationships.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.