3
$\begingroup$

I created an add-on that uses a Geometry Nodes set up and a material. The problem is that when I save the Phyton code as a zip file (for later install it on the preferences) the node is lost and the add-on doesn't work.

One solution is to append the node and the material from the original file every time that the add-on needs to be used; but I would like to know if there is a solution for including everything when the add-on is installed, so if I want to share it only installing the .zip file on the preferences would be needed. I assume that adding code to the script is needed.

This is the folder from the Add-on (the .zip one), as you can see on it there is the script and a blend file with the node and the material needed.

The node is called ("Node"), the material ("mat") and the .zip file is called ("AddOn.zip")

enter image description here

I found this question (similar question) but I tried to run the code and it doesn't work, so I would be very thankful if someone can send my the solution for my case.

$\endgroup$
4
  • $\begingroup$ You can bundle blend files in the addon zip, have you tried that solution ? $\endgroup$
    – Gorgious
    Mar 7, 2022 at 15:56
  • $\begingroup$ You can always write a function that generates the GN node tree and/or modifier and invoke it when you need the GN but it doesn't exist. $\endgroup$ Mar 7, 2022 at 16:43
  • $\begingroup$ May not be the right approach for you if you don't already know how. Perhaps you could post a blend file with the GN setup you want to preserve and I can show you how to convert it specifically into code? $\endgroup$ Mar 7, 2022 at 22:44
  • $\begingroup$ But I would like to know the code for appending the node from the zip file, not how to translate it (because I need also to append other stuff, like materials). Thanks anyway $\endgroup$
    – Artichoke
    Mar 7, 2022 at 23:46

1 Answer 1

3
+50
$\begingroup$
  1. Place the Geometry Node Tree in a blend file. I'll call it "nodetree.blend" but you can call it whatever you want.

  2. Place the file in the directory containing your add-on.

  3. When the user installs the add-on, blender will unzip your file into the same directory as the addon.

  4. Use code to determine the path to your addon. See Directory Layout for details, but if your addon is name "myaddon", then you want code like this:

from bpy.utils import resource_path
from pathlib import Path

USER = Path(resource_path('USER'))
src = USER / "scripts/addons" / "myaddon"
  1. Add code like this to your addon to append the node tree:
file_path = src / "myGN.blend"
inner_path = "NodeTree"
object_name = "Geometry Nodes group"

bpy.ops.wm.append(
    filepath=str(file_path / inner_path / object_name),
    directory=str(file_path / inner_path),
    filename=object_name
)

replacing "Geometry Nodes group" by whatever name you gave the node group in the blend file.

This works because

  • Blender unzips everything from the zip file into the addon directly
  • bpy.ops.wm.append knows how to append geometry nodes.

Once you've appended the node tree, you can find it in bpy.data.node_groups["Geometry Nodes group"], again replacing "Geometry Nodes group" by whatever you called the node group in the source file.

EDIT: If you have multiple '.blend' files in your addon top folder, Blender won't know which one to execute when you try to enable the addon, unless the addon's main file is called __init__.py. Since this is a widely used practice, I recommend following it.

If you don't want to change the name of your addon file, you can put the gn file in a subdirectory. In that case, add the subdirectory name to the definition of src. I like to use assets as a subdirectory in this case, so I would set src:

src = USER / "scripts/addons" / "myaddon" / "asset"
$\endgroup$
2
  • $\begingroup$ Sorry I didn't edit it well. Your solution works but I need to install the script of the addon (ziped) and the folder with the script and the blend file (also ziped) separetly. If I only install the folder, then the add-on doesn't work. $\endgroup$
    – Artichoke
    Mar 12, 2022 at 14:49
  • $\begingroup$ If the user has chosen a system version of python and put the addon reacheable there, or installed the addon manually to the lib folder of a stand-alone version (non-install) of Blender, I dont think this solution will work. Why not just use "os.path.abspath(_ file _))"? Does that solution have drawbacks? $\endgroup$ Jan 2, 2023 at 2:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .