1
$\begingroup$

How can i import a blender python script inside another. I am working a python file using blender api named - generate.py, which contains some functions. I want to call those functions in another file called test.py. I opened both the files in the blender text editor and on top of the test.py file, i wrote "from genrate import *", but when i call a function defined inside generate.py in test.py, i get an error saying module named generate.py does not exist. So what is the right way to define modules in blender. I also have both the files in the same directory

$\endgroup$
6
$\begingroup$

The issue was that the path of the python module - generate.py was not in the locations where python searches for modules. To make generate.py visible to python add the following code on top of test.py

import bpy
import sys
import os

dir = os.path.dirname(bpy.data.filepath)
if not dir in sys.path:
    sys.path.append(dir )

import generate

# this next part forces a reload in case you edit the source after you first start the blender session
import imp
imp.reload(generate)

# this is optional and allows you to call the functions without specifying the package name
from generate import *
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.