1

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)
1
  • 1
    plumbum is a 3rd party module and doesn't ship with blender. However it's easy to install and IMO greatly simplifies calling external commands. Welcome to blender stack exchange. Look forward to your future questions as they become more blender specific. Commented Jun 18, 2020 at 5:20

1 Answer 1

3

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.