18
$\begingroup$

I use an external to Blender text editor (vim). I understand I can write in it and then reload file in Blender's text editor. Is there a way to run a script from external editor without this additional step?

$\endgroup$
0

4 Answers 4

16
$\begingroup$

For short or non-operator-based scripts, that's as efficient as you'll get. Currently there is no faster way than AltR-AltP to reload a plain script modified in external editor.

For longer operator-based scripts, you can use the following initial steps:

  1. Convert the script to an add-on, by adding proper bl_info dictionary and turning your main code into operators (can be made faster by using a template).

  2. Create a symbolic link of the script or its development path to Blender's alternate script path (set through User Preferences > File > Scripts). That way, it stays registered on Blender upgrade, and your development folder stays organized.

    If your OS or filesystem doesn't support symbolic link, move the script to the alternate script path and develop from there.

  3. Register the add-on.

After that, the script can be updated just by pressing F8 Reload Scripts in Blender. Longer preparation, faster in the long run.

$\endgroup$
0
14
$\begingroup$

You can run an external file directly from the text editor without having to continuously reload by using a stub, This is nothing special or Blender spesific.

Blender comes with a template for a stub.

From the text editor select:

Templates -> Python -> Script Stub

# This stub runs a python script relative to the currently open
# blend file, useful when editing scripts externally.

import bpy
import os

# Use your own script name here:
filename = "my_script.py"

filepath = os.path.join(os.path.dirname(bpy.data.filepath), filename)
global_namespace = {"__file__": filepath, "__name__": "__main__"}
with open(filepath, 'rb') as file:
    exec(compile(file.read(), filepath, 'exec'), global_namespace)

In this example filepath is set relative to the currently open blend-file. However you could set filepath to an absolute location.


Note that you can also load external scripts by importing them as modules, however this means the scripts need to be in Python's module search path (sys.path), in some cases its nicer to be able to run a script from any location on your file-system.

$\endgroup$
12
$\begingroup$

You can also set up a wrapper script that reload the external Python file you are editing:

Text file inside Blender:

import importlib
import myModule

importlib.reload(myModule)
myModule.runMyCode()

This way, every time you press Alt+P to run this script, it will automatically reload the latest version of the code from MyModule.

$\endgroup$
7
$\begingroup$

I recently wrote an addon for this task. https://github.com/s-a/ToBlenderenter image description here

$\endgroup$

You must log in to answer this question.

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