If the script runs for too long or you accidentally enter an infinite loop, Ctrl-C in the terminal (Ctrl-Break on Windows) will quit the script early.,code.interact can be added at any line in the script and will pause the script to launch an interactive interpreter in the terminal, when you’re done you can quit the interpreter and the script will continue execution.,The error traceback is printed in full to the terminal which won’t always generate an report message in Blender’s user interface (depending on how the script is executed).,Once the script is running properly in background mode, you’ll want to check the output of the script, this depends completely on the task at hand, however, here are some suggestions:
filename = "/full/path/to/myscript.py"
exec(compile(open(filename).read(), filename, 'exec'))
import bpy
import os
filename = os.path.join(os.path.dirname(bpy.data.filepath), "myscript.py")
exec(compile(open(filename).read(), filename, 'exec'))
import myscript
import importlib
importlib.reload(myscript)
myscript.main()
import sys
import os
import bpy
blend_dir = os.path.dirname(bpy.data.filepath)
if blend_dir not in sys.path:
sys.path.append(blend_dir)
import myscript
import importlib
importlib.reload(myscript)
myscript.main()
blender--background--python myscript.py
blender myscene.blend--background--python myscript.py
You should probably know how to run Python scripts if you are familiar with Python.,Why should you read this article? And another chance that you don’t know how to run Python scripts as you are not familiar with them. It’s definitely for you. Is this only for you? No, both who are familiar and who are not with Python can get something new in this article. Without further ado let’s jump into the article.,In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.,But what if you already have bash scripts that you want to run using Python?
In contrast to my thoughts, one has to create a python script which is executed by blender in background mode.
. / blender--background--python myscript.py
Open a text editor and create a Python script to use throughout this tutorial. You can use the following script of adding two numbers.
a, b = list(map(int, input().split())) print(a + b)
Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead
>>> FirstName = "kalebu" >>>
LastName = "Jordan" >>>
FirstName, LastName = LastName, FirstName >>>
print(FirstName, LastName)
('Jordan', 'kalebu')
If you want to run the script form python console, you use following,
filename = "/full/path/to/script.py"
exec(compile(open(filename).read(), filename, 'exec'))
To run a script by another script or from console, you can use followings,
import bpy
script = bpy.data.texts["script_name.py"]
exec(script.as_string())
To run the script from terminal,
$ blender filename.blend--python script.py