Reload module/workbench
Reload module/workbench
Forgive me if this is trivial.
Is it possible to reload a workbench from the console in a way that it loads its modules again? I would like to change code in the python files and not having to open and close freecad to test it.
I have tried importing the module file in the console but it does not seem to do anything
Is it possible to reload a workbench from the console in a way that it loads its modules again? I would like to change code in the python files and not having to open and close freecad to test it.
I have tried importing the module file in the console but it does not seem to do anything
Re: Reload module/workbench
In Python you can reload modules like:
But note that reload function does not work if you do something like:
I would assume that this would work in FreeCAD as well, but it is true that I never tried.
Code: Select all
import mymodule
reload(mymodule)
Code: Select all
from mymodule import *
reload(mymodule)
Re: Reload module/workbench
I thought so but it does not seem to work for me.
Re: Reload module/workbench
Mhh,
the way Werner descriped to reload modules actually work in FreeCAD to - with some exceptions.
e.g. the Workbench description class is transfered to FreeCAD while start. So if you change the
Workbench and reload the python module it will have no effect.
So the answer is - It depends.
So maybe you can tell us what you change so we can look into it....
the way Werner descriped to reload modules actually work in FreeCAD to - with some exceptions.
e.g. the Workbench description class is transfered to FreeCAD while start. So if you change the
Workbench and reload the python module it will have no effect.
So the answer is - It depends.
So maybe you can tell us what you change so we can look into it....
Stop whining - start coding!
Re: Reload module/workbench
The main problem that I have is that since I'm just starting I make many mistakes. Also I do a lot of trial and error so I end up spending some time opening and closing Freecad. I know some things can be tested from the console but some others can not.
A little off topic, it would be great if the console would have command history.
Anyhow, I'll give you an example to see what I was doing:
In the Mod directory I have created a BoatDesign directory.
Inside that directory I have two files: BoatDesign.py and InitGui.py
InitGui.py
BoatDesign.py
Ok, that works but originally I have written App.activeDocument, wrong case. This is just an example, but the reload method did not work this time. What I did was:
import BoatDesign
reload(BoatDesign)
no errors but when I call the function again the syntax error is still there.
A little off topic, it would be great if the console would have command history.
Anyhow, I'll give you an example to see what I was doing:
In the Mod directory I have created a BoatDesign directory.
Inside that directory I have two files: BoatDesign.py and InitGui.py
InitGui.py
Code: Select all
class BoatWorkbench (Workbench):
MenuText = "Boat Design"
def Initialize(self):
import BoatDesign # assuming Scripts.py is your module
list = ["LoadHull","AddKeel","CalcDispl"] # That list must contain command names, that can be defined in BoatDesign.py
self.appendToolbar("Boat Design",list)
Gui.addWorkbench(BoatWorkbench())
Code: Select all
import FreeCAD as App
import FreeCADGui as Gui
import Part
import FreeCAD.Base as Base
class LoadHull:
def Activated(self):
'''Loads a hull in any fo the formats supported by FreeCAD'''
if App.ActiveDocument is None:
App.Console.PrintMessage('Creating Document')
App.newDocument("Boat")
App.ActiveDocument=App.getDocument("Boat")
Gui.ActiveDocument=Gui.getDocument("Boat")
App.Console.PrintMessage('Select hull file')
Gui.runCommand("Std_Import")
Gui.runCommand("Std_FitAll")
hull = App.ActiveDocument.Objects[0]
hull.Label='Hull'
def GetResources(self):
return {'Pixmap' : 'path_to_an_icon/myicon.png', 'MenuText': 'Load Hull', 'ToolTip': 'Load a hull for the boat'}
import BoatDesign
reload(BoatDesign)
no errors but when I call the function again the syntax error is still there.
Re: Reload module/workbench
Hi,
unfortunately GuiCommands (your LoadHull) is also created once and stored in FreeCAD. So reloading the the module will have no effect.
Thats because such basic objects like Commands and Workbenches are handled in the FreeCAD C++ code and are not handled by python.
So a good idea is to write first a script which does all you need and test it with e.g. FreeCADCmd.exe till its flawless. And then put it
in commands.
And the Console has a history (Ctrl + Up).
Yea I know, not enough documentation
unfortunately GuiCommands (your LoadHull) is also created once and stored in FreeCAD. So reloading the the module will have no effect.
Thats because such basic objects like Commands and Workbenches are handled in the FreeCAD C++ code and are not handled by python.
So a good idea is to write first a script which does all you need and test it with e.g. FreeCADCmd.exe till its flawless. And then put it
in commands.
And the Console has a history (Ctrl + Up).
Yea I know, not enough documentation

Stop whining - start coding!
Re: Reload module/workbench
Yes!!!
Cut and paste was driving me nuts

Cut and paste was driving me nuts
- mangtronix
- Posts: 44
- Joined: Fri Apr 18, 2014 3:58 am
- Location: Berlin, Germany
- Contact:
Re: Reload module/workbench
Found this old thread since I was having the same problem. Here's a solution! The trick is to load and run your modules from the Python console, rather than through the GUI.
Here's an example for testing/developing the MakeConnectors functionality inside the Polycon module.
>>> import Polycon
>>> import PolyconGui
>>> PolyconGui.MakeConnectors().Activated() # create Polycon command and run it
>>> # make code changes
>>> reload(Polycon)
<module 'Polycon' from '/Users/mangtronix/Library/Preferences/FreeCAD/Mod/Polycon/Polycon.pyc'>
>>> PolyconGui.MakeConnectors().Activated() # run command with new code
Here's an example for testing/developing the MakeConnectors functionality inside the Polycon module.
>>> import Polycon
>>> import PolyconGui
>>> PolyconGui.MakeConnectors().Activated() # create Polycon command and run it
>>> # make code changes
>>> reload(Polycon)
<module 'Polycon' from '/Users/mangtronix/Library/Preferences/FreeCAD/Mod/Polycon/Polycon.pyc'>
>>> PolyconGui.MakeConnectors().Activated() # run command with new code
http://michaelang.com - Art / Engineering
- mangtronix
- Posts: 44
- Joined: Fri Apr 18, 2014 3:58 am
- Location: Berlin, Germany
- Contact:
Re: Reload module/workbench
You can even make a nice little function in the FreeCAD console that puts it all together.
>>> def go(): reload(Polycon); PolyconGui.MakeConnectors().Activated()
>>> go()
Edit your code in your text editor, then it's as simple as
>>> go()
>>> def go(): reload(Polycon); PolyconGui.MakeConnectors().Activated()
>>> go()
Edit your code in your text editor, then it's as simple as
>>> go()
http://michaelang.com - Art / Engineering
- sliptonic
- Veteran
- Posts: 3316
- Joined: Tue Oct 25, 2011 10:46 pm
- Location: Columbia, Missouri
- Contact:
Re: Reload module/workbench
Sorry to necrobump this topic but it is still the top result on google if you search for 'FreeCAD module reload' and it's wrong.
These days you have to do this:
These days you have to do this:
Code: Select all
>>> from importlib import reload
>>> reload(<mymodule>)