7
$\begingroup$

With python API, is it possible to load a (new) python script in the Text Editor and then launch it (to automate things !) ?

I try this but it doesn't work :

bpy.ops.text.open(filepath="my-script.py")
bpy.ops.text.run_script()

Script name added in the text list but not visible in the Editor.

$\endgroup$
4
  • $\begingroup$ Where are you running this from? $\endgroup$
    – iKlsR
    Apr 9, 2014 at 16:23
  • $\begingroup$ From command line : $ blender.exe --python script-to-automate-things.py $\endgroup$
    – Lebrun
    Apr 9, 2014 at 17:54
  • $\begingroup$ My configuration : Blender version 2.69, OS Cygwin on Windows7 $\endgroup$
    – Lebrun
    Apr 9, 2014 at 18:05
  • $\begingroup$ I must add that i just try to simplify the task for my project. Everything loaded and executed and the script ready to be completed in the text editor. $\endgroup$
    – Lebrun
    Apr 9, 2014 at 19:25

4 Answers 4

10
$\begingroup$

Every Text Editor has its own active (visible) text datablock stored in .text.

To call the text.run_script() operator, use a context override and give it the right 'edit_text' member.

import bpy

text = bpy.data.texts.load(path_to_file)

for area in bpy.context.screen.areas:
    if area.type == 'TEXT_EDITOR':
        area.spaces[0].text = text # make loaded text file visible

        ctx = bpy.context.copy()
        ctx['edit_text'] = text # specify the text datablock to execute
        ctx['area'] = area # not actually needed...
        ctx['region'] = area.regions[-1] # ... just be nice

        bpy.ops.text.run_script(ctx)
        break
$\endgroup$
1
  • 2
    $\begingroup$ Just a sidenote: You can also use area.spaces.active.text = text since area.spaces[0] is the active space. $\endgroup$
    – satishgoda
    Apr 10, 2014 at 7:47
6
$\begingroup$

It seems it can be done in less code even, just the following code, no TextEditor windows open.

# text = bpy.data.texts.load(path_to_file)   # if from disk
text = bpy.data.texts['some_file_name.py']   # if exists in blend
ctx = bpy.context.copy()
ctx['edit_text'] = text
bpy.ops.text.run_script(ctx)

executed fine!

$\endgroup$
3
$\begingroup$

Another technique is illustrated at http://web.purplefrog.com/~thoth/blender/python-cookbook/exec-text-library.html

import bpy
bufferName = 'lib 1'
lib1 = bpy.data.texts[bufferName].as_string()
exec(lib1)
$\endgroup$
2
  • $\begingroup$ this even works when the script being exec'd is importing local datablock .py files as modules. nice. $\endgroup$
    – zeffii
    Jun 16, 2015 at 17:04
  • $\begingroup$ this can fail if the file you are trying to exec has a few imports not already present in globals/locals -- and this isn't being executed directly from the TextEditor. $\endgroup$
    – zeffii
    Nov 14, 2015 at 13:27
1
$\begingroup$

You don't necessary need to load it. You can use a browser to get the path p and then run exec(compile(open(p).read(), p, 'exec')). The script will be executed.

$\endgroup$

You must log in to answer this question.

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