5
$\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$
  • $\begingroup$ Where are you running this from? $\endgroup$ – iKlsR Apr 9 '14 at 16:23
  • $\begingroup$ From command line : $ blender.exe --python script-to-automate-things.py $\endgroup$ – Lebrun Apr 9 '14 at 17:54
  • $\begingroup$ My configuration : Blender version 2.69, OS Cygwin on Windows7 $\endgroup$ – Lebrun Apr 9 '14 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 '14 at 19:25
7
$\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
| improve this answer | |
$\endgroup$
  • 1
    $\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 '14 at 7:47
5
$\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!

| improve this answer | |
$\endgroup$
2
$\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)
| improve this answer | |
$\endgroup$
  • $\begingroup$ this even works when the script being exec'd is importing local datablock .py files as modules. nice. $\endgroup$ – zeffii Jun 16 '15 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 '15 at 13:27
0
$\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.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.