Msmat
Msmat

Reputation: 27

How to translate command-line into python code

I am currently working with python and I have some real trouble with the syntax. I wanted to include a Cura command-line into my script but I do not know how to actually parse the line so that the command will be executed out of my Blender Script. My code is the following and I really do not know if this is even valid:

def main(context):
    blend_file_path = bpy.data.filepath
    directory = os.path.dirname(blend_file_path)
    target_file = os.path.join(directory,bpy.path.basename(bpy.context.blend_data.filepath) +'.stl')
    bpy.ops.export_mesh.stl(filepath=target_file)   

cevar = 'CuraEngine slice -j "C:\\Programme\\Ultimaker Cura 4.5\\resources\\definitions\\fdmprinter.def.json"' +' -l ' + directory + bpy.path.basename(bpy.context.blend_data.filepath) +'.stl +o '+ directory + bpy.path.basename(bpy.context.blend_data.filepath) +'.gcode'
os.system(cevar)

Upvotes: 1

Views: 280

Answers (1)

pedramcode
pedramcode

Reputation: 357

Use this code

import subprocess

cmd_args = [
    "CuraEngine",
    "slice",
    "-j",
    "C:\\Programme\\Ultimaker Cura 4.5\\resources\\definitions\\fdmprinter.def.json",
    "-l",
    f"{os.path.join(directory,bpy.path.basename(bpy.context.blend_data.filepath))}.stl",
    "+o",
    f"{os.path.join(directory, bpy.path.basename(bpy.context.blend_data.filepath))}.gcode",
]

output = subprocess.Popen(cmd_args, stdout=subprocess.PIPE).communicate()[0]

Upvotes: 3

Related Questions