2
$\begingroup$

I am working toward have a multi-file add-on, as our add-on is getting pretty long. I'm starting with a trivial example to build up from and can't even get that to work.

I have two files at top level in my project, simple_addon.py and simple_panel.py, borrowed from the Blender Scripting Tutorial. If I put the panel contents into the simple_addon.py file, all works. But when I separate it out, Blender cannot find it to import.

I've tried

import simple_panel

  File "/Users/ron/Library/Application Support/Blender/4.0/scripts/addons/simple_addon.py", line 1, in <module>
    import simple_panel
ModuleNotFoundError: No module named 'simple_panel'

from simple_panel import SimplePanel

  File "/Users/ron/Library/Application Support/Blender/4.0/scripts/addons/simple_addon.py", line 1, in <module>
    from simple_panel import SimplePanel
ModuleNotFoundError: No module named 'simple_panel'

I tried adding an __init__.py and using that as the addon, which doesn't load anything.

I tried

from .simple_panel import SimplePanel

  File "/Users/ron/Library/Application Support/Blender/4.0/scripts/addons/simple_addon.py", line 1, in <module>
    from .simple_panel import SimplePanel
ImportError: attempted relative import with no known parent package

I'm sure that from a working example with two files, I could grow my solution. Can anyone show me an example that will work? Ideally, it would allow subfolders but I can either live without those or work it out from a simpler working case.

$\endgroup$
2
  • $\begingroup$ The existing add-ons all have an __init__.py that says, for example: from . import foo, but when I create an init like that, Blender doesn't even add anything. Still confused. A working example that can be added as an add-on, with a couple of separate files, would be great. BTW, Blender 4.0 and MacOS. Thanks! $\endgroup$ Commented Apr 29 at 13:44
  • $\begingroup$ i created an addon template with some simple automatic config github.com/hkunz/blender-addon-template read the README.md for more info. let me know where u find problems. $\endgroup$
    – Harry McKenzie
    Commented Apr 29 at 16:34

1 Answer 1

1
$\begingroup$
  1. create a folder

  2. put your working (!) add-on with the name ´__init__.py´ in it

  3. add another .py file with additional code (let's name it "another.py" as example

  4. add this to your ´__init__.py´ file: "from .another import *"

  5. now you can call/use whatever you defined in another.py in ´__init__.py´

  6. right click on your folder of 1) -> compress

  7. -> now you got a zip file, which you can install as add-on

Hint: in Blender 4.2 use install legacy add-on (don't know whether that's in 4.0 the same, else just install "normal")

$\endgroup$
3
  • $\begingroup$ Thank you! I didn't know that I had to zip the files, but that, plus your other instructions, has me on the right track I think. To get the __init__.py to have the underbars, use a single back tick on each side of the string. And in English, the Menu item is "compress". Thank you kindly! I think I'm on the way! $\endgroup$ Commented Apr 29 at 17:28
  • $\begingroup$ I am going to assume that I need to do the reload / optional import trick that is shown in the various examples and add-ons that ship with Blender. Is that necessary, a good idea, or too much? I am sure that we'll be updating this add-on from time to time, so either we'll have to delete it manually or do the reload? $\endgroup$ Commented Apr 29 at 17:46
  • $\begingroup$ @JanetRossini: thank you, i updated my answer. tbh i would recommend using VSC to write you add-on. There is an add-on from Jacques Lucke for VSC with which you can automatically reload your add-on without opening + closing Blender. Video tutorial: youtube.com/watch?app=desktop&v=q06-hER7Y1Q $\endgroup$
    – Chris
    Commented Apr 29 at 18:06

You must log in to answer this question.

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