6
$\begingroup$

I am new to blender, but not python, and have been asked to create a user-input dependent, stylized animation of a protein network I am working with:

text document >> information processed by python-script >> blender animates

So, if certain parameters in the network are changed, the differences in behavior can be observed visually as well.

Instead of coding in Blender's built-in Text Editor, I have been working in "pycharm". However, when I attempt to run the program through blender, it will throw an error and inform me that all my custom modules cannot be found and imported, though they are all in the same directory. Same goes for all the text documents. The text files in particular are a problem as we are talking about thousands of data-points that can't be manually entered.

All modules and text files are saved in the same directory as the program's master runner.

I am working on a windows 7, 64 bit windows machine.

additional information

the errors:

 # import error: 
 no module named 'x'
 # filenotfounderror:
 [errno2] No such file or directory: 'y'

blender_file_directory: C:\Users\Henry\Documents\Blender\file.blend

python_files_directory: C:\Users\Henry\PycharmProjects\project_name\runner.py

# import of modules:
from module import *

# import of txt file:
numpy.loadtxt('gillespie.txt', unpack=False)

The script is called through blender's own SDK like so:

filename = "C:/Users/Henry/PycharmProjects/project_name/runner.py"
exec(compile(open(filename).read(), filename, 'exec'))

Text files are stored in the same directory as python scripts, so importing them when running the script through python is no problem. Only when it's run through blender does it throw an error.

$\endgroup$
  • $\begingroup$ What error is thrown? Could you add the imports and directory structure you use? $\endgroup$ – stacker Jul 8 '15 at 13:04
  • $\begingroup$ @stacker I hope the information provided in the edit suffices? $\endgroup$ – Henry Ehlers Jul 8 '15 at 13:18
6
$\begingroup$

When you execute a python script inside Blender, the current working directory is not the base directory of your .blend file. You can easily confirm this by importing os and printing os.getcwd(), and that will explain why blender isn't finding these modules you're trying to import.

you'll get the current .blend filepath using bpy.data.filepath, from which you should be able to construct relative paths, or switch pythons working directory. if that's more convenient (remember to switch it back )

$\endgroup$
  • $\begingroup$ my bpy.data.filepath is empty, as I haven't created any file. os.getcwd() returns the path of my modules. Yet, I get the error. $\endgroup$ – McLawrence Sep 4 '17 at 13:06
  • $\begingroup$ feel free to ask a proper new question, that will give you room to describe your scenario and you might get a more pointed answer. $\endgroup$ – zeffii Sep 5 '17 at 11:38
8
$\begingroup$

A more detailed example of the technique zeffi outlined is illustrated by the following example from http://web.purplefrog.com/~thoth/blender/python-cookbook/import-python.html

import bpy
import sys
import os

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

import cityFunctions

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

# this is optional and allows you to call the functions without specifying the package name
from cityFunctions import *
$\endgroup$
  • $\begingroup$ This solution works for me. However, it is not clear to me why: printing dir gives me an empty string (len(dir)=0) and dir is not where my modules are, which are at the current working directory. $\endgroup$ – McLawrence Sep 4 '17 at 13:16
  • $\begingroup$ maybe having an empty string in your path is equivalent to having . in your path. $\endgroup$ – Mutant Bob Sep 5 '17 at 14:25

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.