Here is the example, it works well either way with os.system or subprocess. Only one problem I found was that Blender would stuck as long as the external program is open. This command os.system/subprocess.call won’t finish until the external program is closed, so this will make Blender stuck because it won’t do anything else until the script is finished.
If the external program won’t remain open too long (say about 1 to 5 seconds) you might call directly the external program otherwise you will need to use threading. I don’t know what errors multi threading will bring but at least it will solve the stuck.
import bpy
import threading
def run_program():
# import os
# os.system("notepad.exe")
import subprocess
subprocess.call("notepad.exe")
class RunExternalProgramOperator(bpy.types.Operator):
bl_idname = "wm.run_ext_program"
bl_label = "Run External Program Operator"
def execute(self, context):
t = threading.Thread(target=run_program)
t.start()
return {'FINISHED'}
def register():
bpy.utils.register_class(RunExternalProgramOperator)
def unregister():
bpy.utils.unregister_class(RunExternalProgramOperator)
if __name__ == "__main__":
register()
bpy.ops.wm.run_ext_program()
Thank you!
This work well for the installed accessories programms in windows > system32!
eg.: SnippingTool.exe / mspaint.exe / StikyNot.exe / xpsrchvw.exe / psr.exe / etc.
Is there a way to start portal programms on a relativ path?
If you want to see from where the script is loaded you can do this:
print(__file__)
and see how you can manage this filepath.
I tried this also and seemed like a good alternative, the deal is that you will create an environment variable and use that instead to get a proper path.
You could try this approach:
put the program I want to call in same folder as plugin, and then locate it using the dirname of file.
make a list containing the abspath to the program as first entry, and any other parameters as next entries
from os import path
#fetch absolute path to the folder where this script, and the program, are
working_dir = path.dirname(__file__)
program_path = path.join(working_dir, 'myprogram.exe')
params = [program_path, 'any_parameter_you_want', 'as many as you like']
proc = subprocess.Popen(args=args , cwd=self.working_dir)
#if you want to wait till process has finished, call proc.wait() here