2
$\begingroup$

I am an experienced python programmer, but just getting into the Blender API. I am stuck on trying to get a script to execute when the Blender file is loaded. I want to do all sorts of setup in this script, but to start with, I'm just trying to change the python path to add my working directory, so that .py files can be imported from there.

So, I opened a text editor window in a fresh project, then created a new text block, into which I typed the following 2 lines:

import sys
sys.path.append('/Path/To/My/Blender/Project')

When I use the "run script" button, and check sys.path in the console, my path has indeed been added.

So then, I checked the register toggle for the text block, and set the user preferences to auto run python scripts. I saved the .blend file, exited Blender, and re-started by double clicking the .blend file.

When I check sys.path, it is the normal blender path, but if I then run the script that should have auto run, sys.path changes as it should. So it would seem that my script is not running on load.

I am on Xubuntu 14.04 (Trusty) and my Blender version is 2.78. I must be making some really simple mistake, but I can't seem to find it. Thanks for any suggestions.

$\endgroup$
2
  • 3
    $\begingroup$ Does this answer help? $\endgroup$
    – sambler
    Commented Mar 20, 2017 at 13:40
  • $\begingroup$ I think I'm already doing all the things mentioned in that answer, but I still get no autorun. $\endgroup$
    – datasmith
    Commented Mar 21, 2017 at 15:35

2 Answers 2

1
$\begingroup$

I had the same problem and got my answer here: https://blender.stackexchange.com/a/84392/38217

Note that this executes on ANY Blender File you open, but maybe it will suffice.

import bpy
import sys

@bpy.app.handlers.persistent
def setup_path(*args):
    sys.path.append('/Path/To/My/Blender/Project')
bpy.app.handlers.load_post.append(setup_path)
setup_path()

Put this code into a Python file and place it into ~/.config/blender/2.78/scrips/startup/setup-path.py (replace ~/config/blender with %APPDATA%/Blender Foundation/Blender on Windows, and something along those lines on MacOS).

$\endgroup$
-2
$\begingroup$

If you're on windows, use double backslashes instead:

sys.path.append('\\Path\\To\\My\\Blender\\Project\\')

in Python, backslashes have a special use,
so you need two to literally indicate that symbol:
https://docs.python.org/2.0/ref/strings.html

oh, and if memory serves correct, you need the trailing slash(es) as well.

...

I've run into situations where trailing slashes are needed:

cwd  = os .getcwd()

if cwd[0] is '/':   ##  linux
  inpu  = '/png/'
  outpu  = '/obj/'
else:                 ##  win
  inpu  = '\\png\\'
  outpu  = '\\obj\\'

Re  = png .Reader(cwd + inpu + img + filetype)
...
with open(cwd + outpu + img + '.mtl', 'w') as mtl:
...
''.join(cwd, filename)  

None of the last 3 commands would function without it, so when I see a path written without it, alarm bells ring, because I know it can cause issues with scripts.

$\endgroup$
8
  • 3
    $\begingroup$ They already stated their code works the first time. It is running the code automatically they have issue with. $\endgroup$ Commented Mar 20, 2017 at 12:28
  • $\begingroup$ "Run" doesn't mean it gets past that point. I stand by my answer. $\endgroup$ Commented Mar 20, 2017 at 16:00
  • 2
    $\begingroup$ Your reference link is pointing to python 2 instead of 3. Not the downvoter and honestly I don't care, leave it as is... Just wanted to point out that your answer has nothing to do with this question - OP's operating system is Linux (no ugly slash-hacks required) @Doyousketch2 $\endgroup$
    – brockmann
    Commented Mar 20, 2017 at 19:33
  • 2
    $\begingroup$ The trailing slash is not needed. When you import a python module, the interpreter automatically inserts the separator appropriate to the OS between the path and the file name. In my case, there is no exception raised, the script simply fails to execute. $\endgroup$
    – datasmith
    Commented Mar 21, 2017 at 15:38
  • 2
    $\begingroup$ The normal practice in python is to use os.path.join to construct file paths. No os specific logic required. $\endgroup$
    – datasmith
    Commented Mar 21, 2017 at 19:11

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