geometry-script icon indicating copy to clipboard operation
geometry-script copied to clipboard

How to work with multiple script files

Open rsaccon opened this issue 1 year ago • 5 comments

The currently supported workflow seems to support only one single script. I would like to split my project into multiple files where I can import modules from the main script. Is it somehow possible ?

rsaccon avatar Nov 14 '23 23:11 rsaccon

Here's how I tackle this:

In Blender I have a 'Text Editor' area opened to a 'reload.py' file. After making some edits to the modules which contain tree definitions you just click the play button to run the reload script which you can use to reload all your trees.

image

All files are in the same folder

#reload.py
import sys
import pathlib
import importlib
import bpy

current_text = bpy.context.space_data.text
path=str(pathlib.Path(current_text.filepath).parent.resolve())
sys.path.append(path)

import othermodule, somemodule

def reload_modules():
    importlib.reload(othermodule)
    importlib.reload(somemodule)
   
reload_modules()
# othermodule.py
from geometry_script import *

@tree
def othertree():
    return cube().mesh
# somemodule.py
from geometry_script import *
from othermodule import *

@tree
def sometree():
    mesh = othertree()
    # ...
    return mesh

You have to be careful about the order in which you reload the modules. If you reload tree1 after tree2 which uses tree1 some links to tree1 within tree2 may be broken.

alphadito avatar Nov 15 '23 01:11 alphadito

Very clever. Works perfectly. Thanks a lot.

rsaccon avatar Nov 15 '23 02:11 rsaccon

I’d like to reopen this to consider a possible interface for selecting folders to link. It should also be possible to avoid breaking links when nodes rebuild.

carson-katri avatar Nov 15 '23 03:11 carson-katri

That would be great. On one blender addon I messed around with, I had a reimport of everything as soon as a filechange on a monitored directory was detected (by a timer based polling for file timestamps changes, if I remember properly).

rsaccon avatar Nov 15 '23 08:11 rsaccon

Just an update to me previous comment: the watch-directory-and-reimport-on-filechange addon I was using, here is the link to it

rsaccon avatar Nov 16 '23 08:11 rsaccon