2
$\begingroup$

I am somewhat familiar with the Blender api, but the directory stuff still confuses me a little bit.

This is my current situation:

I am running a python script script1.py through the blender text editor. That script needs to get variables from script2.py. The only way I could find to import script2 into script1 while running in Blender is by adding script2 to the startup file in Blender (Explained here https://docs.blender.org/api/current/info_overview.html?highlight=working%20directory#the-default-environment).

This does not work by itself, the following code reloads script2 every time script1 is ran in blender since I could have changed something:

import conFig
import importlib
importlib.reload(conFig)
from conFig import *

Context: The code I am writing needs to be run on both Mac and Windows. The variables in script2 let me switch paths easily depending on which os I'm currently on. As you can tell, this makes adding script2 to setup every time I test between operating systems a clunky challenge and that I would prefer to avoid. It would be preferable if all these files (.blend included) were in the same folder that I can zip and email to my PC.

My question: I have spent the better part of two days trying to import script2 into script1 when they are both located in the same directory. And so far my current situation above is the only working result I could muster.

I tried the following methods:

https://blender.stackexchange.com/a/70744/133552

https://docs.blender.org/api/current/info_tips_and_tricks.html#executing-modules

But I couldn't manage to get it to work.

How do I import script2 into script1 in the same directory?

$\endgroup$

2 Answers 2

2
$\begingroup$

Ok so apparently I got this working right after I posted this if anyone is interested:

# In script1

import bpy
import os
import sys
dir = os.path.dirname(bpy.data.filepath) #Get directory of the .blend file
sys.path.append(dir) #Setting it as the python directory in the Blender Text editor 

import script2
import importlib
importlib.reload(conFig)
from script2 import *

I honestly don't understand it fully but this is what did it for me, I can now change the variables in script2 and the results appear when I run script1, no startup needed.

Ended up with this answer from this page: https://tabreturn.github.io/code/blender/python/2020/11/01/a_quick_intro_to_blender_creative_coding-part_3_of_3.html

Word search the page for "import" and the code is at the bottom.

$\endgroup$
1
  • $\begingroup$ Best practice since Python 3.6 is to not import os if all you want is access to OS independent pathnames, but rather to use pathlib; fwiw $\endgroup$ Commented Oct 15, 2021 at 14:06
2
$\begingroup$

The pattern keeps changing over time, so my generic advice is to look into: /usr/share/blender/{version}/scripts/addons/ and do what other addon authors are doing. The current pattern seems to be:

if "bpy" in locals():
    import importlib

    importlib.reload(awesome_module)
    # ...
else:
    import bpy

    from . import (
        awesome_module,
        # ...
    )

Also:

from .awesome_module import awesome_id
$\endgroup$

You must log in to answer this question.

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