Friday, December 7, 2012

Scripting Externally

There are a handful of reasons not to script using the internal script editor built into Blender.  One of the main reasons is undo-ing.  If you undo, something in your scene while testing, it will also undo the last set of edits you made to your script.  This may not be obvious and you'll find yourself re-typing scripts sections over and over again.

Helper Script
The suggested method is to edit your scripts externally, and use the following script to run it in your Blender scene.

import bpy
import os

filename = os.path.join(os.path.dirname(bpy.data.filepath), "my-script-name.py")

exec(compile(open(filename).read(), filename, 'exec'))



This script will load your "my-script-name.py" and run it whenever you press the RunScript button.   This script also uses the path of the Blender file to find your python script, so there are some requirements for this script to run correctly.
  1. You must save your .blend file first
  2. You must co-locate your script file with your .blend file
Once you finish developing your script, you can then move it wherever you want, and install it through the Blender add-on menu, like any other add-on.

Scripting Externally
In regards to scripting externally, there are a number of really good application designed for coding in python.  If you don't want to go through the hassle of finding, downloading and installing one, you can always open another instance of Blender and use that as your developing program, and still use the script above in your testing instance.

No comments:

Post a Comment