0
$\begingroup$

In my code I am trying to open a new blender subprocess with this code

I am trying to do some action like this how to run a give code = ''' import bpy''' like in code below, in that new blender subprocess how to run like a script in that new sub process that opens Example Script : 'C:/Users/Desktop/my_script.py'

import bpy
import subprocess

blenpath = 'C:/Program Files/Blender Foundation/Blender 2.93/blender.exe'

code = '''
import bpy
for obj in bpy.data.objects:
    obj.name = 'test_name'
'''

# how to run the code above in that new blender subprocess
# how to run like a script in that new sub process that opens 
# Example Script : 'C:/Users/Desktop/my_script.py'

cmd = [blenpath]
subprocess.Popen(cmd)
$\endgroup$

1 Answer 1

1
$\begingroup$

See all the python options here https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html#python-options

import bpy
import subprocess

temp_blend_path = 'C:/Users/naman/Desktop/test.blend'

filepath = 'C:/Users/Desktop/my_script.py'

process = subprocess.Popen([bpy.app.binary_path, "--factory-startup", "-b", temp_blend_path, "--python", filepath], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, encoding='utf-8')
process.wait()
$\endgroup$
6
  • $\begingroup$ Can I know how, to open a subprocess, instead of an existing file? like another instance of blender and run the code in it. $\endgroup$
    – NamanDeep
    May 23, 2023 at 4:02
  • $\begingroup$ Also u wrote a code in the expression, What if I want to get a code from a script I written in a directory, 'C:/Users/Desktop/my_script.py' , like this $\endgroup$
    – NamanDeep
    May 23, 2023 at 4:02
  • $\begingroup$ learn more docs.blender.org/manual/en/latest/advanced/command_line/… $\endgroup$
    – Karan
    May 23, 2023 at 4:12
  • $\begingroup$ argument order docs.blender.org/manual/en/latest/advanced/command_line/… $\endgroup$
    – Karan
    May 23, 2023 at 4:18
  • 1
    $\begingroup$ remove the "--python-expr", expression and use "--python", <filepath> $\endgroup$
    – Karan
    May 23, 2023 at 17:28

You must log in to answer this question.

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