0
$\begingroup$

I am trying to create an operator that restarts Blender. This script has worked for me in the past:

import bpy
import subprocess

blender_exe = bpy.app.binary_path
subprocess.Popen([blender_exe, "-con", "--python-expr", "import bpy; bpy.ops.wm.recover_last_session()"])
bpy.ops.wm.quit_blender()

I'm now using Blender 3.5 on Windows 11 Pro Version 22H2. Blender quits, but never reopens. If I just run the subprocess, it will open the last session. Blender or Windows is quitting both sessions. Is there a way to only quit the old session?

Thank you!

$\endgroup$

2 Answers 2

1
$\begingroup$

This worked for me [huge thank you to Ray Molenkamp (LazyDodo) on blender.chat]:

import bpy,os,subprocess

blender_exe = bpy.app.binary_path
head, tail = os.path.split(blender_exe)
blender_launcher = os.path.join(head,"blender-launcher.exe")
subprocess.run([blender_launcher, "-con", "--python-expr", "import bpy; bpy.ops.wm.recover_last_session()"])
bpy.ops.wm.quit_blender()
$\endgroup$
0
$\begingroup$

Popen accepts a startinfo argument, which provides for passing flags to WINDOWS CreateProcess().

The flag DETACHED_PROCESS may be what you need;

import bpy
import subprocess

blender_exe = bpy.app.binary_path

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.DETACHED_PROCESS
subprocess.Popen([blender_exe, "-con", "--python-expr", "import bpy; bpy.ops.wm.recover_last_session()"], startupinfo=si)

bpy.ops.wm.quit_blender()
$\endgroup$
3
  • $\begingroup$ Thank you very much for the response and code. Unfortunately this doesn't work for me. I've also tried terminating the Blender app by the process ID instead of bpy.ops.wm.quit_blender(). $\endgroup$ Dec 28, 2022 at 19:29
  • $\begingroup$ You're welcome @dr-pontchartrain, sorry my initial guess didn't solve it for you. I am not able to test this on a Windows system, but perhaps the flag you need is one of subprocess.CREATE_NEW_PROCESS_GROUP or subprocess.CREATE_BREAKAWAY_FROM_JOB. If you try either and it works I'll edit my answer. $\endgroup$ Dec 28, 2022 at 20:03
  • $\begingroup$ Thanks again for the suggestions. Those flags didn't work for me. I'm not sure if this issue is one particular to my environment or not, but hopefully the blender-launcher & subprocess.run route will work for other Windows users experiencing this. $\endgroup$ Dec 28, 2022 at 20:08

You must log in to answer this question.

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