How to automatically load a dependant module

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
grandcross
Posts: 350
Joined: Sun Oct 11, 2015 3:08 pm
Contact:

How to automatically load a dependant module

Post by grandcross »

I'm reusing a command from an internal workbench, but of course if that workbench isn't loaded then error messages are shown and the command isn't available. Is there a way to load it programmatically so it's always available?

I have the workbench listed as a dependency in my package.xml file, but this doesn't seem to do anything about making sure the module is loaded. Requiring the user to set the module to load on start up using the preferences works, but is not really desirable. This really should be done automatically.

Code: Select all

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
Locale: English/United States (en_US)
Installed mods: 
  * Assembly3 0.11.3
  * fasteners 0.3.49
  * fcgear 1.0.0
  * LCInterlocking
  * parts_library
  * Rocket 2.7.1
  * TabBar
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to automatically load a dependant module

Post by onekk »

From what I could guess from your question when telling about package.xml seems to reveal that you are speaking of about a WB or a Macro.

If your code is using a command from an internal WB you could ever load it in the initiaziation phase of the WB or esplictly in the Macro code.

If not please explain.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
grandcross
Posts: 350
Joined: Sun Oct 11, 2015 3:08 pm
Contact:

Re: How to automatically load a dependant module

Post by grandcross »

onekk wrote: Sun Sep 18, 2022 1:52 am From what I could guess from your question when telling about package.xml seems to reveal that you are speaking of about a WB or a Macro.

If your code is using a command from an internal WB you could ever load it in the initiaziation phase of the WB or esplictly in the Macro code.
It was for a workbench, but as it turns out I don't need it anymore. I wound up doing things another way.

But, it was the initialization code I was asking about. I have no idea how that would be done.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to automatically load a dependant module

Post by onekk »

grandcross wrote: Mon Sep 19, 2022 6:19 am ...
But, it was the initialization code I was asking about. I have no idea how that would be done.
Just to be complete it is not difficult to find informations about WB creation:

https://wiki.freecadweb.org/Workbench_creation


Or maybe seeing how things are done in other WB or in some not too much complicated addons.

There are not much informations, but usually if you put together some code, even not fully working and post an example here, it is not unusual that you will get some answers and maybe some "amended code" in return.

It may be a good way to improve documentation, as in this forum posts are not deleted, so a future user could read them and learn something.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
grandcross
Posts: 350
Joined: Sun Oct 11, 2015 3:08 pm
Contact:

Re: How to automatically load a dependant module

Post by grandcross »

That's not the question I'm asking. I know how to write a workbench, and have a very complete one already in the addons list.

The problem is that from within this workbench, I want to address code in another workbench, so I need to ensure it's already loaded. By default it's not.
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: How to automatically load a dependant module

Post by adrianinsaval »

what about running import WorkbenchName ? doesn't that initialize it? You might need to load the GUI part of it too. With this kind of question it's better if you point us to an example of what you are doing so that people can see if something is missing or incorrect.
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: How to automatically load a dependant module

Post by Zolko »

grandcross wrote: Mon Sep 26, 2022 6:01 pm I want to address code in another workbench, so I need to ensure it's already loaded.
you can test whether a specific command is available. For example, to test for the Draft WB, this will return True or False :

Code: Select all

'Draft_Clone' in Gui.listCommands()
try the Assembly4 workbench for FreCAD — tutorials here and here
User avatar
grandcross
Posts: 350
Joined: Sun Oct 11, 2015 3:08 pm
Contact:

Re: How to automatically load a dependant module

Post by grandcross »

adrianinsaval wrote: Mon Sep 26, 2022 7:56 pm what about running import WorkbenchName ? doesn't that initialize it? You might need to load the GUI part of it too. With this kind of question it's better if you point us to an example of what you are doing so that people can see if something is missing or incorrect.
Sorry for the slow response but I think that's the missing piece. I was trying all variety of imports to import the piece I needed, but never tried 'import workbenchname'
Post Reply