Created
January 16, 2026 09:23
-
-
Save Vaelatern/95a5347671d56d6b6bd2ab0cb91cad41 to your computer and use it in GitHub Desktop.
Little utility to add Extruder information for Orcaslicer, to 3mf files created by colorscad
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
| # /// script | |
| # requires-python = ">=3.14" | |
| # dependencies = [ | |
| # "lxml>=6.0.2", | |
| # ] | |
| # /// | |
| # Little utility to add Extruder information for Orcaslicer, to 3mf files created by colorscad | |
| import sys | |
| import zipfile | |
| import pathlib | |
| from lxml import etree | |
| MAPPINGS = { | |
| '[1, 1, 1, 1]': '1', | |
| '[0, 0.501961, 0.501961, 1]': '2', | |
| '[0.501961, 0, 0.501961, 1]': '3', | |
| '[0, 0, 0, 1]': '4', | |
| '[1, 0.843137, 0, 1]': '5', | |
| } | |
| def modxml(input): | |
| tree = etree.fromstring(input) | |
| for val, extruder in MAPPINGS.items(): | |
| for part in tree.xpath(f'//config/object/part[metadata[@key="name" and @value="{val}"]]'): | |
| part.insert(0, etree.Element('metadata', key="extruder", value=extruder)) | |
| return etree.tostring(tree, pretty_print=True, encoding='unicode') | |
| def modify(srcfile, dstfile): | |
| with zipfile.ZipFile(srcfile) as inzip, zipfile.ZipFile(dstfile, 'w') as outzip: | |
| # Iterate the input files | |
| for inzipinfo in inzip.infolist(): | |
| # Read input file | |
| with inzip.open(inzipinfo) as infile: | |
| if inzipinfo.filename == 'Metadata/model_settings.config': | |
| outzip.writestr(inzipinfo.filename, modxml(infile.read())) | |
| else: | |
| outzip.writestr(inzipinfo.filename, infile.read()) | |
| def usage(): | |
| print(f'Usage:') | |
| print(f' uv run {sys.argv[0]} input.3mf output.3mf') | |
| sys.exit(1) | |
| def main(): | |
| args = sys.argv[1:] | |
| # Usage | |
| if len(args) != 2: | |
| usage() | |
| input_path = pathlib.Path(args[0]) | |
| # First argument (input file) must exist | |
| if not input_path.is_file(): | |
| print(f'Error: Input file not found: {input_path}', file=sys.stderr) | |
| sys.exit(1) | |
| output_path = None | |
| # Output must not exist | |
| output_path = pathlib.Path(args[1]) | |
| if output_path.exists(): | |
| print(f'Error: Output file already exists: {output_path}', file=sys.stderr) | |
| print('Overriding this is a future feature.') | |
| sys.exit(1) | |
| return modify(input_path, output_path) | |
| if __name__ == '__main__': | |
| main() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
snippets shamelessly stolen from https://techoverflow.net/2020/11/11/how-to-modify-file-inside-a-zip-file-using-python/