0
$\begingroup$

If I wan't to import another python script(Test_script_1) and also run it through my python script(Test_Script_2)

  1. Can this be done through init.py script ?

  2. Can anyone tell me how to do that ?

$\endgroup$
1

2 Answers 2

2
$\begingroup$

Here's what I've just done, it seems to work.

My paths are:

scripts path -

C:\Users\Drew\Documents\Development\bfd-graphics\Blender Scripts

mainscript path -

C:\Users\Drew\Documents\Development\bfd-graphics\Blender Scripts\main.py

blender file path -

C:\Users\Drew\Documents\Development\graphics\3D Assets\Prototype\Blender Projects\Templates\Builder.blend

I wanted to be able to import scripts from a completely different but known relative directory. So here is how I tackled it:

import bpy, sys
from pathlib import Path

path = Path(bpy.data.filepath)
sys.path.append(str(path.parent.parent.parent.parent.parent) + "\\Blender Scripts")

import test_import

test_import.print_a_message()

The number of parents determines where in the directory structure I am, and then I add the rest of the path as a string.

$\endgroup$
1
$\begingroup$

Yes, of course this can be done. If you have an add-on with its folder and __init__.py inside it, if you have other files like file.py in the same folder you just import stuff from it like this:

from .file import Some_stuff

Just open [Blender's installation folder]\[version]\scripts\addons\ and see how it's done with any of the add-ons in the folders there.

$\endgroup$

You must log in to answer this question.

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