0
$\begingroup$

I would like to know how to execute code in a blend file / project using Python.

$\endgroup$
2

1 Answer 1

3
$\begingroup$

Edit

Script now automatically runs in new blend.


Recap summary of what this script does:

  • Creates new blend file.

  • Imports stated script.

  • Replaces default 3D Viewport window to Text Editor window.

  • Assigns imported script to active Text Editor window.

  • Automatically runs imported script once Text Editor window is active.

import bpy

bpy.ops.wm.read_homefile(app_template="")
bpy.ops.text.open(filepath=str("D:\Blender\Scripts\external_script.py"))

for window in bpy.context.window_manager.windows: 
    for area in window.screen.areas:
        if area.type == "VIEW_3D":
            area.ui_type = "TEXT_EDITOR"
            area.spaces[0].text = bpy.data.texts["external_script.py"]
            with bpy.context.temp_override(window=window, area=area):
                bpy.ops.text.run_script()
            break
$\endgroup$

You must log in to answer this question.

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