carson-katri / geometry-script

A scripting API for Blender's Geometry Nodes
https://carson-katri.github.io/geometry-script/
GNU General Public License v3.0
252 stars 24 forks source link

How to work with multiple script files #33

Open rsaccon opened 9 months ago

rsaccon commented 9 months ago

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 ?

alphadito commented 9 months ago

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.

rsaccon commented 9 months ago

Very clever. Works perfectly. Thanks a lot.

carson-katri commented 9 months ago

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.

rsaccon commented 9 months ago

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 commented 9 months ago

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