Created
July 4, 2025 08:01
-
-
Save sevenissimo/0e1c57be8a1f1c69e3ceb68067708420 to your computer and use it in GitHub Desktop.
OrcaSlicer Preset Helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import json | |
| import sys | |
| from time import time | |
| def bundle(path): | |
| def ls(path, sub): | |
| files = [] | |
| path = os.path.join(path, sub) | |
| if os.path.exists(path): | |
| for f in os.listdir(path): | |
| if os.path.isfile(os.path.join(path, f)): | |
| files.append(f"{sub}/{f}") | |
| return files | |
| printers = ls(path, "printer") | |
| preset = os.path.splitext(os.path.basename(printers[0]))[0] if printers else "Unknown" | |
| data = { | |
| "bundle_id": f"{preset}_{time():0.0f}", | |
| "bundle_type": "printer config bundle", | |
| "filament_config": ls(path, "filament"), | |
| "printer_config": printers, | |
| "printer_preset_name": preset, | |
| "process_config": ls(path, "process"), | |
| "version": "" | |
| } | |
| out = os.path.join(path, "bundle_structure.json") | |
| with open(out, "w") as f: | |
| json.dump(data, f, indent=2) | |
| print(f"File '{out}' creato.", file=sys.stderr) | |
| if __name__ == "__main__": | |
| if len(sys.argv) != 2: | |
| print("Uso: python script.py PATH", file=sys.stderr) | |
| sys.exit(1) | |
| bundle(sys.argv[1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import os | |
| import sys | |
| def merge_json(path: str) -> dict: | |
| """ | |
| Recursively merges JSON objects based on an 'inherits' key. | |
| """ | |
| d = os.path.dirname(path) # Get directory of current file | |
| with open(path, 'r') as f: | |
| obj = json.load(f) | |
| if 'inherits' in obj: | |
| # Reassign 'path' to the inherited file's full path | |
| path = os.path.join(d, obj.pop('inherits') + ".json") | |
| if os.path.exists(path): | |
| merge = merge_json(path).copy() # Recursive call with the new 'path' | |
| merge.update(obj) # Parent `obj` overrides `new` child for common keys | |
| return merge | |
| else: | |
| # If inherited file doesn't exist, return current JSON without 'inherits' | |
| return obj | |
| else: | |
| # If no 'inherits' key, return current JSON | |
| return obj | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 2: | |
| print(f"Usage: python {sys.argv[0]} FILE [OUTPUT]", file=sys.stderr) | |
| sys.exit(1) | |
| if not os.path.exists(sys.argv[1]): | |
| print(f"Error: File not found at '{sys.argv[1]}'", file=sys.stderr) | |
| sys.exit(1) | |
| out = None | |
| if len(sys.argv) > 2: | |
| if os.path.isdir(sys.argv[2]): | |
| # If the second argument is an existing directory, use basename of initial file | |
| out = os.path.join(sys.argv[2], os.path.basename(sys.argv[1])) | |
| else: | |
| # Otherwise, treat it as the full output file path | |
| out = sys.argv[2] | |
| try: | |
| res = merge_json(sys.argv[1]) | |
| if out: | |
| # Write output to the specified file | |
| with open(out, 'w') as f: | |
| json.dump(res, f, indent=2) | |
| print(f"Output written to '{out}'", file=sys.stderr) | |
| else: | |
| # Print output to stdout | |
| print(json.dumps(res, indent=2)) | |
| except json.JSONDecodeError as e: | |
| print(f"Error decoding JSON in file '{sys.argv[1]}': {e}", file=sys.stderr) | |
| sys.exit(1) | |
| except Exception as e: | |
| print(f"An unexpected error occurred: {e}", file=sys.stderr) | |
| sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment