Skip to content

Instantly share code, notes, and snippets.

@samwiseg0
Last active November 8, 2024 21:38
Show Gist options
  • Save samwiseg0/3d4cb07742db83343bf7ef381c7c86b3 to your computer and use it in GitHub Desktop.
Save samwiseg0/3d4cb07742db83343bf7ef381c7c86b3 to your computer and use it in GitHub Desktop.
Convert SuperSlicer/PrusaSlicer filament profiles to OrcaSlicer
# -*- coding: utf-8 -*-
import argparse
import configparser
import json
import os
def sanitize_value(value):
# Remove leading and trailing whitespaces, including newline characters
value = value.strip()
# Remove any extra quotes or escape characters
value = value.strip("\"'")
value = value.replace("\\", "")
return value
def ini_to_json(ini_file_path, json_output_directory):
# Read the entire INI file as a string
with open(ini_file_path) as f:
ini_content = f.read()
# Insert the default section header for settings without section headers
ini_content = "[printer_settings]\n" + ini_content
# Parse the INI file as a configparser
config = configparser.ConfigParser(interpolation=None)
config.read_string(ini_content, source=ini_file_path)
# Create the updated key mapping
key_mapping = {
# Exact matches
"chamber_temperature": "chamber_temperature",
"filament_cost": "filament_cost",
"filament_density": "filament_density",
"filament_diameter": "filament_diameter",
"filament_max_volumetric_speed": "filament_max_volumetric_speed",
"filament_minimal_purge_on_wipe_tower": "filament_minimal_purge_on_wipe_tower",
"filament_retract_before_wipe": "filament_retract_before_wipe",
"filament_retract_lift_above": "filament_retract_lift_above",
"filament_retract_lift_below": "filament_retract_lift_below",
"filament_retract_restart_extra": "filament_retract_restart_extra",
"filament_shrink": "filament_shrink",
"filament_soluble": "filament_soluble",
"filament_type": "filament_type",
"filament_vendor": "filament_vendor",
"filament_wipe": "filament_wipe",
"full_fan_speed_layer": "full_fan_speed_layer",
# Translated
"bed_temperature": "hot_plate_temp",
"bridge_fan_speed": "overhang_fan_speed",
"disable_fan_first_layers": "close_fan_the_first_x_layers",
"end_filament_gcode": "filament_end_gcode",
"extrusion_multiplier": "filament_flow_ratio",
"fan_always_on": "reduce_fan_stop_start_freq",
"fan_below_layer_time": "fan_cooling_layer_time",
"filament_colour": "default_filament_colour",
"filament_deretract_speed": "filament_deretraction_speed",
"filament_retract_layer_change": "filament_retract_when_changing_layer",
"filament_retract_length": "filament_retraction_length",
"filament_retract_lift": "filament_z_hop",
"filament_retract_speed": "filament_retraction_speed",
"first_layer_bed_temperature": "hot_plate_temp_initial_layer",
"first_layer_temperature": "nozzle_temperature_initial_layer",
"max_fan_speed": "fan_max_speed",
"min_fan_speed": "fan_min_speed",
"min_print_speed": "slow_down_min_speed",
"slowdown_below_layer_time": "slow_down_layer_time",
"start_filament_gcode": "filament_start_gcode",
"temperature": "nozzle_temperature",
# New key for filament_type condition
"overhang_fan_threshold": "overhang_fan_threshold"
}
# Convert the data to a dictionary with values as arrays
data = {}
for key in key_mapping:
if key in config["printer_settings"]:
value = config["printer_settings"][key]
value = sanitize_value(value)
# Check if the key is "filament_max_volumetric_speed" and the value is 0
if key == "filament_max_volumetric_speed" and value == "0":
value = "15" # Set default value to 15
# Check if the key is "filament_type"
if key == "filament_type":
if value in ['ABS', 'ABS+', 'PCCF', 'ASA', 'PC']:
# Set "overhang_fan_threshold" to "25%" if filament_type is "ABS"
data[key_mapping["overhang_fan_threshold"]] = ["25%"]
else:
# Set "overhang_fan_threshold" to "50%" for any other filament_type
data[key_mapping["overhang_fan_threshold"]] = ["50%"]
# For other keys, use the normal key mapping
data[key_mapping[key]] = [value]
# Add the "name" key with the input file name
data["name"] = os.path.splitext(os.path.basename(ini_file_path))[0]
# Add the static keys
data["is_custom_defined"] = "1"
data["version"] = "1.5.1.2"
# Create the output JSON file path inside the json_output_directory
json_file_path = os.path.join(json_output_directory, f"{data['name']}.json")
# Write the data to a JSON file
with open(json_file_path, 'w') as json_file:
json.dump(data, json_file, indent=4)
def convert_all_ini_to_json(directory_path):
# Create the 'json' subfolder if it doesn't exist
json_output_directory = os.path.join(directory_path, "json")
os.makedirs(json_output_directory, exist_ok=True)
for filename in os.listdir(directory_path):
if filename.endswith(".ini"):
ini_file_path = os.path.join(directory_path, filename)
ini_to_json(ini_file_path, json_output_directory)
if __name__ == "__main__":
# Parse command-line arguments
parser = argparse.ArgumentParser(description="Convert SS/PS filament profile INI files to \
JSON files to use with OrcaSlicer.")
parser.add_argument("directory", nargs="?", default=os.getcwd(), help="Directory path \
containing SS/PS filament \
profile files")
args = parser.parse_args()
directory_path = args.directory
convert_all_ini_to_json(directory_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment