Files
split-flap-generator/generator.py
Dennis Gunia ebd90083e7 initial commit
2025-09-11 21:57:16 +02:00

177 lines
5.5 KiB
Python
Executable File

#!/bin/python3
import yaml
from pathlib import Path
import os
import subprocess
import sys
config_file = sys.argv[1]
scad_exec_path = ""
scad_out_path = ""
scad_input_path = ""
# prepare config
config = {}
with open(config_file) as conf_stream:
try:
config = yaml.safe_load(conf_stream)
except yaml.YAMLError as exc:
print(exc)
# count flaps
flap_count = len(config['flaps'])
print(f"Found {flap_count} flaps:")
# fill in default values
for flap in config['flaps']:
for default_key in config['defaults'].keys():
if (default_key not in flap.keys()):
flap[default_key] = config['defaults'][default_key]
print(flap)
def generate_mesh():
# check if openscad is installed
scad_exec = Path(config['scad_path'])
try:
scad_exec_path = scad_exec.resolve(strict=True)
except FileNotFoundError as exc:
print(exc)
else:
print(f"Using openscad from {scad_exec_path}")
# check if output directory exists
scad_out = Path(config['output_path'])
try:
scad_out_path = scad_out.resolve(strict=True)
except FileNotFoundError as exc:
print(exc)
else:
print(f"Save flaps to {scad_out_path}")
# clear out path
print("Clear output directory")
[f.unlink() for f in scad_out.glob("*") if f.is_file()]
# check if input file exists
scad_input = Path(config['scad_file'])
try:
scad_input_path = scad_input.resolve(strict=True)
except FileNotFoundError as exc:
print(exc)
else:
print(f"Use scad file {scad_input_path}")
## now start generating flaps
for ix, flap in enumerate(config['flaps']):
this_flap = flap
prev_flap = config['flaps'][(ix -1)%flap_count]
print (f"Generate flap {ix}")
print(this_flap)
print(prev_flap)
cmd_1 = [f"{scad_exec}"]
cmd_2 = [f"{scad_exec}"]
cmd_vals = []
cmd_vals.append(f"char_top=\"{prev_flap['string']}\"")
cmd_vals.append(f"char_bottom=\"{this_flap['string']}\"")
cmd_vals.append(f"svg_top=\"{prev_flap['svg']}\"")
cmd_vals.append(f"svg_bottom=\"{this_flap['svg']}\"")
cmd_vals.append(f"font_top=\"{prev_flap['font']}\"")
cmd_vals.append(f"font_top_size={prev_flap['font_size']}")
cmd_vals.append(f"font_top_y_scale={prev_flap['font_y_scale']}")
cmd_vals.append(f"svg_top_scale={prev_flap['svg_scale']}")
cmd_vals.append(f"font_bottom=\"{this_flap['font']}\"")
cmd_vals.append(f"font_bottom_size={this_flap['font_size']}")
cmd_vals.append(f"font_bottom_y_scale={this_flap['font_y_scale']}")
cmd_vals.append(f"svg_bottom_scale={this_flap['svg_scale']}")
# pass globals
for global_var in config['globals'].keys():
cmd_vals.append(f"{global_var}={config['globals'][global_var]}")
outfile_a = os.path.join(scad_out, f"flap_{ix}_a.stl")
outfile_b = os.path.join(scad_out, f"flap_{ix}_b.stl")
cmd_1.append("-o")
cmd_1.append(outfile_a)
cmd_2.append("-o")
cmd_2.append(outfile_b)
# add to cmd
for cmd_val in cmd_vals:
cmd_1.append("-D")
cmd_1.append(f"{cmd_val}")
cmd_2.append("-D")
cmd_2.append(f"{cmd_val}")
cmd_1.append("-D")
cmd_1.append("exp_step=0")
cmd_1.append(str(scad_input_path))
cmd_2.append("-D")
cmd_2.append("exp_step=1")
cmd_2.append(str(scad_input_path))
# print out cli command
print (cmd_1)
# generate both parts
subprocess.run(cmd_1)
subprocess.run(cmd_2)
def run_combine(): # not yet implemented
# combine stls into 3mf
if config['combine']['enable']:
orca_exec = Path(config['combine']['orca_slicer_exec'])
try:
orca_exec_path = orca_exec.resolve(strict=True)
except FileNotFoundError as exc:
print(exc)
else:
print(f"Using orca slicer from {orca_exec_path}")
# check if output directory exists
scad_out = Path(config['output_path'])
try:
scad_out_path = scad_out.resolve(strict=True)
except FileNotFoundError as exc:
print(exc)
else:
print(f"Use meshes from {scad_out_path}")
outfile_final = os.path.join(scad_out, f"flap_combine.3mf")
cmd_final = [
str(orca_exec_path),
"--export-3mf", f"{outfile_final}",
"--debug", "1",
"--allow-multicolor-oneplate"
]
for ix, flap in enumerate(config['flaps']):
outfile_a = os.path.join(scad_out, f"flap_{ix}_a.stl")
outfile_b = os.path.join(scad_out, f"flap_{ix}_b.stl")
outfile = os.path.join(scad_out, f"flap_{ix}_multi.3mf")
cmd_final.append(outfile)
cmd = [
str(orca_exec_path),
"--arrange", "1",
"--export-3mf", f"{outfile}",
"--debug", "1",
"--assemble",
"--ensure-on-bed",
"--allow-multicolor-oneplate",
"--load-filaments", f"{config['combine']['fillament']['background']};{config['combine']['fillament']['foreground']}",
"--load-filament-ids", "1,2",
outfile_a, outfile_b
]
print(' '.join(cmd))
subprocess.run(cmd)
# combine into final file
print(' '.join(cmd_final))
subprocess.run(cmd_final)
#generate_mesh()
run_combine()