Skip to content

Guard module-scope addCommand() calls with FreeCAD.GuiUp - #325

Merged
oliveroxtoby merged 1 commit into
jaheyns:devfrom
scotCW:upstream-pr-headless-import
Aug 25, 2026
Merged

Guard module-scope addCommand() calls with FreeCAD.GuiUp#325
oliveroxtoby merged 1 commit into
jaheyns:devfrom
scotCW:upstream-pr-headless-import

Conversation

@scotCW

@scotCW scotCW commented Aug 20, 2026

Copy link
Copy Markdown

Summary

FreeCADGui.addCommand() is called unguarded at module scope in 15 files, even though the import FreeCADGui immediately above each call is already guarded with if FreeCAD.GuiUp:. Importing any of these modules without a GUI present (e.g. via freecadcmd, or any headless script/test harness) raises NameError: name 'FreeCADGui' is not defined.

This makes it impossible to script or test the workbench headlessly — every CfdOF module that registers a command hits this.

Fix

Wrap each addCommand() call (or contiguous run of them) in the same if FreeCAD.GuiUp: guard already used for the import above it. GUI behaviour is unchanged: Init.py only registers a lazy import type, and the affected modules are imported inside CfdOFWorkbench.Initialize(), which runs after FreeCAD.GuiUp is set — so the guard only takes effect in the headless case, where the commands couldn't have registered anyway.

CfdOpenPreferencesPage.py is intentionally left alone: it imports FreeCADGui unconditionally at module scope and is a GUI-only command, so there's no headless code path to protect.

Testing

Verified by importing the affected modules and running checkCfdDependencies() under freecadcmd — all import cleanly and the dependency check completes without error. Not run through the full FreeCAD -t TestCfdOF GUI-based suite (no GUI-visible behaviour is touched, so no regression expected there, but flagging for visibility).

The import FreeCADGui line above each of these is already guarded this way,
but the addCommand() call itself was not, so importing any of these 15
modules without a GUI (e.g. via freecadcmd, or any headless script/test)
raises NameError.

CfdOpenPreferencesPage.py is intentionally left as-is: it imports FreeCADGui
unconditionally and is a GUI-only command.
@oliveroxtoby
oliveroxtoby merged commit edffe2d into jaheyns:dev Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants