Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
Last active February 14, 2019 04:41
Show Gist options
  • Save SeijiEmery/cdb7356a296da4b30c18b6b049ffcc63 to your computer and use it in GitHub Desktop.
Save SeijiEmery/cdb7356a296da4b30c18b6b049ffcc63 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# bpy-run-from-blender fix.
# Assumes that commandline arguments are
# sys.argv[0] = path-to-blender
# sys.argv[1] = '-b'
# sys.argv[2] = '-P'
# sys.argv[3] = path-to-this-script
import sys
import os
if sys.argv[0].split('.exe')[0].endswith('blender') and sys.argv[1] == '-b' and sys.argv[2] == '-P':
origin_script_working_directory = os.path.split(sys.argv[3])[0]
print("setting working directory for imports: '%s'"%origin_script_working_directory)
sys.path.insert(0, origin_script_working_directory)
# re-run self from within blender if bpy not available.
# lets you run this script directly, while really running it from within blender's python install.
# note: the code above is to fix imports (re-add the local working directory to the python search path)
# when this happens
from run_bpy import run_self
run_self()
# End header...
import bpy
@SeijiEmery
Copy link
Author

Use this as a header for when writing self-hoisting scripts to use w/ blender and bpy.

See https://github.com/SeijiEmery/shape-net-utils/blob/master/run_bpy.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment