I would like to know how to execute code in a blend file / project using Python.
-
1$\begingroup$ Go to the Scripting workspace, there you can create a new text data-block, paste and run the code: (blender.stackexchange.com/a/194425/107598). If you want to use this code interactively in Blender's Python console have a look here: Easy way to run python script in blender python console? $\endgroup$– BlunderJun 19, 2022 at 22:19
-
$\begingroup$ Hi. I've edited my script. It now fully runs a imported script in a new blend. $\endgroup$– RPaladinAug 1, 2022 at 19:32
1 Answer
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 toText 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