2

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.

JakeD
  • 8,467
  • 2
  • 30
  • 73
datasmith
  • 31
  • 3

2 Answers2

1

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).

xeruf
  • 393
  • 3
  • 14
-2

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.

Doyousketch2
  • 1,220
  • 7
  • 12
  • 3
    They already stated their code works the first time. It is running the code automatically they have issue with. – Ray Mairlot Mar 20 '17 at 12:28
  • "Run" doesn't mean it gets past that point. I stand by my answer. – Doyousketch2 Mar 20 '17 at 16:00
  • Operating system is Xubuntu (Unix) rather than Windows, in this case the path is correct. Also consider, Blender comes with Python 3+. – brockmann Mar 20 '17 at 18:44
  • What does 3.x have anything to do with it? I mentioned TRAILING SLASHES, and still haven't gotten a reply on that. You say something is wrong, but haven't posted any error, so that's my best guess. – Doyousketch2 Mar 20 '17 at 18:48
  • 2
    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 – brockmann Mar 20 '17 at 19:33
  • 2.x or 3.x both use escape characters, if you knew that you wouldn't question it. Again, I point to the fact that if you are going to use a file in a directory, it isn't '/Path/To/My/Blender/ProjectFilename' ...it's... '/Path/To/My/Blender/Project/Filename' so where does the slash between the dir and the filename come from, unless you finish your dir with a TRAILING SLASH??? – Doyousketch2 Mar 20 '17 at 19:54
  • 2
    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. – datasmith Mar 21 '17 at 15:38
  • 2
    The normal practice in python is to use os.path.join to construct file paths. No os specific logic required. – datasmith Mar 21 '17 at 19:11