I'd make an addon that would use several external command line programs to process output images. Is this possible?
$\begingroup$
$\endgroup$
9
-
$\begingroup$ @HarryMcKenzie so the answer is "yes"..... Executing Shell commands on any system could pose serious security problem, esp when the users do not know what those commands really do behind the curtain. This is just my guess, but I worry about it. $\endgroup$– Blender LakeCommented Aug 17, 2023 at 1:23
-
1$\begingroup$ @BlenderLake, what exactly is the "serious security problem"? And why is it relevant if the users know what those commands do? Any third party Blender add-on is already a serious security risk anyway if you care about that. But how does using subprocess in an add-on change anything about security? $\endgroup$– Martynas ŽiemysCommented Aug 17, 2023 at 6:36
-
$\begingroup$ @MartynasŽiemys From this post, what I learnt is that I should buy or use any Blender addon only if it is absolutely necessary, otherwise my computer may be hacked very soon. $\endgroup$– Blender LakeCommented Aug 17, 2023 at 6:41
-
$\begingroup$ Add-ons run on the machine, so from security point of view, they are pretty much as much risk, as you get. I have never seen any malicious Blender add-ons though. My point is that your comment about executing shell commands and security makes no sense at all. I formed it as a question, because maybe you know something I don't. Do you? What difference does it make if a Python script already running on your system executes other commands? How is it a security risk? The way I see it, that's simply incorrect. $\endgroup$– Martynas ŽiemysCommented Aug 17, 2023 at 6:49
-
$\begingroup$ @MartynasŽiemys In almost all cases, I (as the user) do not fully understand what those commands do. Therefore, if the Python script is written by the Blender Foundation, then I trust its safety. If it is written by other third parties, then I worry about the safety of my computer. For example, an addon named "Auto-Rig Pro" may be suspicious. On the following page, if you click the link " Mike (free)", then my FireFox stop downloading the ZIP file by citing "potential security risk": blendermarket.com/products/auto-rig-pro?num=4&src=popular $\endgroup$– Blender LakeCommented Aug 17, 2023 at 9:34
|
Show 4 more comments
1 Answer
$\begingroup$
$\endgroup$
Yes. If you are running Blender on Linux, you can execute shell commands in your Blender addon using the subprocess
module. In this example I pipe the commands sed
and awk
to replace the string Hello World
with Yes? Hello Blender!
:
import bpy
import subprocess
command = 'echo \'Hello World!\' | sed \'s/World/Blender/\' | awk \'{print "Yes? ", $0}\''
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
rc = result.returncode
print("return code:", rc)
output = result.stdout.strip() if rc == 0 else result.stderr.strip()
bpy.context.area.type = 'CONSOLE'
bpy.ops.console.scrollback_append(text=output)
If you are running Blender on Windows, you can try this script:
import bpy
import subprocess
command = [
"powershell.exe",
"-Command",
"$output = 'Hello World!' -replace 'World', 'Blender'; Write-Output $output"
]
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
rc = result.returncode
print("return code:", rc)
output = result.stdout.strip() if rc == 0 else result.stderr.strip()
bpy.context.area.type = 'CONSOLE'
bpy.ops.console.scrollback_append(text=output)