Recommendations for speeding up development

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
David_D
Posts: 81
Joined: Fri Jun 29, 2018 6:43 am
Location: Christchurch, New Zealand

Recommendations for speeding up development

Post by David_D »

Hello, I've recently started coding a python only module/workbench, and I'm wondering how everyone else speeds up their Python for FreeCAD development.

By that I mean reducing the time between you saving the changes on a python file, and actually running those changes within FreeCAD.

My current strategy is to use:

Code: Select all

importlib.reload(module.i.just.changed)
from within the FreeCAD interpreter to be able to use the updated code, but does anyone else have their own strategies? Have they scripted that reload process?
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: Recommendations for speeding up development

Post by Joel_graff »

David_D wrote: Tue Mar 05, 2019 2:52 am My current strategy is to use:

Code: Select all

importlib.reload(module.i.just.changed)

from within the FreeCAD interpreter to be able to use the updated code, but does anyone else have their own strategies? Have they scripted that reload process?
That's about how you do it. I'm not even that efficient... You could probably write a lambda function in the console to do that, I guess.. ?
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
User avatar
David_D
Posts: 81
Joined: Fri Jun 29, 2018 6:43 am
Location: Christchurch, New Zealand

Re: Recommendations for speeding up development

Post by David_D »

Joel_graff wrote: Tue Mar 05, 2019 3:27 am That's about how you do it. I'm not even that efficient... You could probably write a lambda function in the console to do that, I guess.. ?
I was also considering that. I noticed that (perhaps due to a screwup on my module organisation) I need to run the method on each of the submodules, individually if I want to have the entire module be updated, so I thought it might be worthwhile to have some simple function to run it recursively.
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: Recommendations for speeding up development

Post by Joel_graff »

David_D wrote: Tue Mar 05, 2019 3:35 am I was also considering that. I noticed that (perhaps due to a screwup on my module organisation) I need to run the method on each of the submodules, individually if I want to have the entire module be updated, so I thought it might be worthwhile to have some simple function to run it recursively.
Someone's probably got that all figured out. I'm too lazy to care, tbh. But yes, you need to reload each module that gets changed.
You can probably do it something like this:

Code: Select all

my_modules = ['moduleA', 'moduleB', 'moduleC']
my_reload = lambda mod_list: [reload(mod) for mod in mod_list]
Or just substitute the list directly into the lambda function. Either way works.
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
User avatar
furti
Posts: 344
Joined: Mon Nov 27, 2017 5:27 pm

Re: Recommendations for speeding up development

Post by furti »

I normally change the freecad macro directory to the source of my workbench. Then I execute the file I am working on as a simple macro.

I have a "if __name__ == '__main'__" in every file and execute what is needed to test the code.

This also saves some time because i can setup the objects or whatever with some defaults i need for testing.

The only disadvantage of this procedure is, that changes of modules the file depends on are not reloaded. But when this is the case i simply restart FreeCAD :)
User avatar
furti
Posts: 344
Joined: Mon Nov 27, 2017 5:27 pm

Re: Recommendations for speeding up development

Post by furti »

https://github.com/furti/FreeCAD-Lithop ... e_image.py this is a example if setting up the object as i need for testing.
User avatar
David_D
Posts: 81
Joined: Fri Jun 29, 2018 6:43 am
Location: Christchurch, New Zealand

Re: Recommendations for speeding up development

Post by David_D »

furti wrote: Tue Mar 05, 2019 10:42 am https://github.com/furti/FreeCAD-Lithop ... e_image.py this is a example if setting up the object as i need for testing.
That's a clever way of doing it. It's a shame about the not reloading the deps, though.
Joel_graff wrote: Tue Mar 05, 2019 3:48 am Someone's probably got that all figured out. I'm too lazy to care, tbh. But yes, you need to reload each module that gets changed.
I adapted some code on stack exchange to work for FreeCAD, and added this function to one of my modules, to do what you're recommending. In my case, the module "Detailing" is the one I'm working on and want to reload.

Code: Select all


import os
import types
import importlib

import Detailing

def reload_code():

    reload_module = Detailing
    
    fn = reload_module.__file__
    fn_dir = os.path.dirname(fn) + os.sep
    module_visit = {fn}
    del fn

    def reload_recursive_ex(module):
        importlib.reload(module)

        for module_child in vars(module).values():
            if isinstance(module_child, types.ModuleType):
                fn_child = getattr(module_child, "__file__", None)
                if (fn_child is not None) and fn_child.startswith(fn_dir):
                    if fn_child not in module_visit:
                        FreeCAD.Console.PrintMessage("Reloading: {}, from: {}\n".format(
                            fn_child, module
                            ))
                        module_visit.add(fn_child)
                        reload_recursive_ex(module_child)

    return reload_recursive_ex(reload_module)

This seems to be working well for me. I even bound it to a gui button.
Post Reply