-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmithforge.py
More file actions
1322 lines (1090 loc) · 55.3 KB
/
smithforge.py
File metadata and controls
1322 lines (1090 loc) · 55.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#⠀⠀⠀⠀⠀⠀⢰⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⡄⠀⠀⠀⠀⠀
#⠀⠹⣿⣿⣿⣿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⣄⡀⠀⠀
#⠀⠀⠙⢿⣿⣿⡇⢸⣿⣿⣿ SMITHFORGE ⣿⣿⣿⣿⢸⣿⣿⡶⠀
#⠀⠀⠀⠀⠉⠛⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠸⠟⠋⠀⠀
#⠀⠀⠀⠀⠀⠀⠀⠀⠸⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠇⠀⠀⠀⠀⠀
#⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣶⣶⣶⣶⣶⣶⣶⣶⡀⠀⠀⠀⠀⠀⠀⠀⠀
#⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿ by ⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⣠⣿⣿⣿ S1N4X ⣿⣿⣿⣄⠀⠀⠀⠀⠀⠀⠀
#⠀⠀⠀⠀⠀⠀⣀⣀⣈⣉⣉⣉⣉⣉⣉⣉⣉⣉⣉⣉⣉⣉⣉⣁⣀⣀⠀⠀⠀⠀
#⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⠀
#
# GPL-3.0-only License
import trimesh
from trimesh.exchange import load
from trimesh import transformations as tf
import shapely.geometry
import argparse
import zipfile
import xml.etree.ElementTree as ET
import json
import os
import tempfile
import shutil
# Import repair module
try:
from repair import auto_repair_mesh, RepairReport
except ImportError:
# If running from parent directory
try:
from smithforge.repair import auto_repair_mesh, RepairReport
except ImportError:
print("⚠️ Warning: Could not import repair module. Auto-repair will be disabled.")
auto_repair_mesh = None
RepairReport = None
# Import text layer parser module
try:
from text_layer_parser import parse_swap_instructions, validate_layer_heights
except ImportError:
# If running from parent directory
try:
from smithforge.text_layer_parser import parse_swap_instructions, validate_layer_heights
except ImportError:
print("⚠️ Warning: Could not import text_layer_parser module. Text injection will be disabled.")
parse_swap_instructions = None
validate_layer_heights = None
# Configuration constants
DEFAULT_EMBEDDING_OVERLAP_MM = 0.1 # Default Z-axis overlap for proper model union
def extract_main_mesh(scene):
if isinstance(scene, trimesh.Scene):
return trimesh.util.concatenate(scene.dump())
elif isinstance(scene, trimesh.Trimesh):
return scene
else:
raise ValueError("Unsupported 3MF content.")
def extract_color_layers(hueforge_3mf_path):
"""
Extract color layer information from a Hueforge 3MF file.
Returns:
dict with 'layers' (list of dicts with top_z, extruder, color),
'filament_colours' (list of hex color strings), and optionally
'layer_config_ranges_xml' (raw XML string), or None if no color data found.
"""
try:
with zipfile.ZipFile(hueforge_3mf_path, 'r') as zf:
# Extract color layers from custom_gcode_per_layer.xml
layers = []
try:
with zf.open('Metadata/custom_gcode_per_layer.xml') as f:
tree = ET.parse(f)
root = tree.getroot()
# Find all layer elements
for layer_elem in root.findall('.//layer[@type="2"]'):
top_z = layer_elem.get('top_z')
extruder = layer_elem.get('extruder')
color = layer_elem.get('color')
if top_z and extruder and color:
layers.append({
'top_z': float(top_z),
'extruder': extruder,
'color': color
})
except KeyError:
print("ℹ️ No custom_gcode_per_layer.xml found in Hueforge 3MF")
return None
# Extract filament colors from project_settings.config
filament_colours = []
try:
with zf.open('Metadata/project_settings.config') as f:
config_str = f.read().decode('utf-8')
# Parse as JSON (it's a JSON file)
config = json.loads(config_str)
filament_colours = config.get('filament_colour', [])
except (KeyError, json.JSONDecodeError) as e:
print(f"ℹ️ Could not extract filament colors: {e}")
# Extract layer_config_ranges.xml if it exists
layer_config_ranges_xml = None
try:
with zf.open('Metadata/layer_config_ranges.xml') as f:
layer_config_ranges_xml = f.read().decode('utf-8')
print("✅ Extracted layer_config_ranges.xml")
except KeyError:
print("ℹ️ No layer_config_ranges.xml found in Hueforge 3MF")
if layers:
print(f"✅ Extracted {len(layers)} color layer transitions")
result = {
'layers': layers,
'filament_colours': filament_colours
}
if layer_config_ranges_xml:
result['layer_config_ranges_xml'] = layer_config_ranges_xml
return result
else:
return None
except Exception as e:
print(f"⚠️ Error extracting color layers: {e}")
return None
def detect_object_id(temp_dir):
"""
Detect the object ID from the 3D model structure.
Looks for object_X.model files in 3D/Objects/ directory and returns
the first object ID found, or defaults to 1.
Args:
temp_dir: Path to extracted 3MF temporary directory
Returns:
int: Object ID (typically 1 for single-object models)
"""
try:
# Check 3D/Objects/ directory for object files
objects_dir = os.path.join(temp_dir, '3D', 'Objects')
if os.path.exists(objects_dir):
# Find object_X.model files
for filename in os.listdir(objects_dir):
if filename.startswith('object_') and filename.endswith('.model'):
# Extract ID from filename (e.g., "object_1.model" -> 1)
try:
obj_id = int(filename.replace('object_', '').replace('.model', ''))
return obj_id
except ValueError:
continue
# If no object files found, check 3dmodel.model for object definitions
model_path = os.path.join(temp_dir, '3D', '3dmodel.model')
if os.path.exists(model_path):
tree = ET.parse(model_path)
root = tree.getroot()
# Look for object elements with id attribute
ns = {'': 'http://schemas.microsoft.com/3dmanufacturing/core/2015/02'}
objects = root.findall('.//object[@id]', ns)
if not objects:
objects = root.findall('.//object[@id]')
if objects:
# Return the first object ID found
return int(objects[0].get('id', 1))
except Exception as e:
print(f"⚠️ Error detecting object ID: {e}")
# Default to 1 if detection fails
return 1
def inject_color_metadata(output_3mf_path, color_data, z_offset):
"""
Inject color layer metadata into an exported 3MF file using height range modifiers.
This approach defines extruder assignments for Z-height ranges on the object,
which is the preferred method for un-sliced models in Bambu Studio.
Args:
output_3mf_path: Path to the 3MF file to modify
color_data: Dict with 'layers' and 'filament_colours'
z_offset: Z offset to add to all layer heights
"""
try:
# Create a temporary directory to work with the 3MF contents
with tempfile.TemporaryDirectory() as temp_dir:
# Extract the 3MF
with zipfile.ZipFile(output_3mf_path, 'r') as zf:
zf.extractall(temp_dir)
# Create Metadata directory if it doesn't exist
metadata_dir = os.path.join(temp_dir, 'Metadata')
os.makedirs(metadata_dir, exist_ok=True)
# Detect object ID from 3D model structure
object_id = detect_object_id(temp_dir)
print(f"📍 Detected object ID: {object_id}")
# Create layer_config_ranges.xml with height range modifiers
# This approach defines extruder assignments for Z-height ranges on the object
# This is the preferred method for un-sliced models in Bambu Studio
# IMPORTANT: Only define ranges for color SWAPS (extruders 2+)
# Base color (extruder 1) is implicit and should NOT be included
ranges_xml = ET.Element('objects')
obj = ET.SubElement(ranges_xml, 'object', id=str(object_id))
# Calculate height ranges from color layer data
# Each range defines where a color swap occurs (extruders 2, 3, 4, etc.)
# Range 1: first swap (extruder 2) from 0.0 to first swap Z
# Range 2: second swap (extruder 3) from first swap Z to second swap Z
# Range 3: third swap (extruder 4) from second swap Z to third swap Z
# etc.
# Add ranges for each color swap (extruders 2+)
for i, layer_info in enumerate(color_data['layers']):
adjusted_z = layer_info['top_z'] + z_offset
# Determine min_z for this range
if i == 0:
min_z = 0.0 # First swap starts at bottom
else:
min_z = color_data['layers'][i - 1]['top_z'] + z_offset
# Determine max_z for this range
if i + 1 < len(color_data['layers']):
# Not the last layer: range goes to next swap Z
max_z = color_data['layers'][i + 1]['top_z'] + z_offset
else:
# Last layer: range goes to infinity (large value)
max_z = adjusted_z + 1000.0
range_elem = ET.SubElement(obj, 'range',
min_z=f"{min_z:.17g}",
max_z=f"{max_z:.17g}")
ET.SubElement(range_elem, 'option', opt_key='extruder').text = layer_info['extruder']
ET.SubElement(range_elem, 'option', opt_key='layer_height').text = '0.08'
# Write the XML file
tree = ET.ElementTree(ranges_xml)
ET.indent(tree, space=' ')
ranges_path = os.path.join(metadata_dir, 'layer_config_ranges.xml')
tree.write(ranges_path, encoding='utf-8', xml_declaration=True)
print(f"✅ Created layer_config_ranges.xml with {len(color_data['layers'])} color swap ranges (extruders 2+)")
# Update or create project_settings.config with filament colors
project_settings_path = os.path.join(metadata_dir, 'project_settings.config')
if os.path.exists(project_settings_path):
# Read existing config and update
with open(project_settings_path, 'r') as f:
config = json.load(f)
else:
# Create minimal config
config = {}
# Inject filament colors
if color_data['filament_colours']:
num_filaments = len(color_data['filament_colours'])
config['filament_colour'] = color_data['filament_colours']
config['filament_type'] = ['PLA'] * num_filaments
config['default_filament_colour'] = [''] * num_filaments # Match array length for Bambu Studio
# Add complete filament properties required by Bambu Studio
config['filament_ids'] = ['GFA00'] * num_filaments
config['filament_settings_id'] = ['Bambu PLA Basic @BBL X1C'] * num_filaments
config['filament_cost'] = ['24.99'] * num_filaments
config['filament_density'] = ['1.26'] * num_filaments
# CRITICAL: Enable multi-material printing mode
# Without these flags, Bambu Studio won't show color layer changes
config['enable_prime_tower'] = '1'
config['single_extruder_multi_material'] = '1'
# Write back
with open(project_settings_path, 'w') as f:
json.dump(config, f, indent=4)
# CRITICAL: We MUST overwrite model_settings.config because Bambu CLI creates
# an EMPTY <assemble> section, causing "assemble objects, size 0" error
print("📝 Creating model_settings.config with proper assemble section...")
model_settings_xml = create_model_settings_config(temp_dir)
model_settings_path = os.path.join(metadata_dir, 'model_settings.config')
with open(model_settings_path, 'w', encoding='utf-8') as f:
f.write(model_settings_xml)
print("✅ model_settings.config created")
# Create Metadata/_rels/model_settings.config.rels (required for assembly)
metadata_rels_dir = os.path.join(metadata_dir, '_rels')
os.makedirs(metadata_rels_dir, exist_ok=True)
# This .rels file is required for Bambu Studio to properly load objects for assembly
rels_content = '''<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
</Relationships>
'''
rels_path = os.path.join(metadata_rels_dir, 'model_settings.config.rels')
with open(rels_path, 'w', encoding='utf-8') as f:
f.write(rels_content)
print("✅ model_settings.config.rels created")
# NOTE: We do NOT create slice_info.config here because Bambu Studio CLI
# already creates it with the correct version. Overwriting it would
# downgrade the version and potentially break compatibility.
# NOTE: We use layer_config_ranges.xml for color definition via height range
# modifiers. This is the preferred approach for un-sliced models in Bambu Studio
# as it allows the slicer to apply extruder assignments per Z-height range.
# This is more flexible than custom_gcode_per_layer.xml (gcode tool changes).
# Repack the 3MF
temp_output = output_3mf_path + '.tmp'
with zipfile.ZipFile(temp_output, 'w', zipfile.ZIP_DEFLATED) as zf_out:
for root, dirs, files in os.walk(temp_dir):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, temp_dir)
zf_out.write(file_path, arcname)
# Replace original with modified
shutil.move(temp_output, output_3mf_path)
print(f"✅ Injected color metadata with Z-offset {z_offset:.3f} mm")
except Exception as e:
print(f"⚠️ Error injecting color metadata: {e}")
def create_model_settings_config(temp_dir):
"""
Create model_settings.config XML file for Bambu Studio.
This file is required for proper color layer preview in Bambu Studio.
It defines the object/plate/assemble relationship.
Args:
temp_dir: Path to extracted 3MF temporary directory
Returns:
XML string content for model_settings.config
"""
try:
# Extract transform from 3dmodel.model build item
model_path = os.path.join(temp_dir, '3D', '3dmodel.model')
if not os.path.exists(model_path):
print("⚠️ Warning: 3D/3dmodel.model not found, using default transform")
transform = "1 0 0 0 1 0 0 0 1 128 128 0"
else:
tree = ET.parse(model_path)
root = tree.getroot()
# Find build item - try with namespace first, then without
ns = {'': 'http://schemas.microsoft.com/3dmanufacturing/core/2015/02'}
build_items = root.findall('.//build/item', ns)
if not build_items:
build_items = root.findall('.//build/item')
if build_items:
transform = build_items[0].get('transform', '1 0 0 0 1 0 0 0 1 128 128 0')
else:
print("⚠️ Warning: Build item not found, using default transform")
transform = "1 0 0 0 1 0 0 0 1 128 128 0"
# Extract face count from object_1.model
object_path = os.path.join(temp_dir, '3D', 'Objects', 'object_1.model')
face_count = 0
if os.path.exists(object_path):
obj_tree = ET.parse(object_path)
obj_root = obj_tree.getroot()
# Find triangles element
triangles = obj_root.findall('.//{http://schemas.microsoft.com/3dmanufacturing/core/2015/02}triangles')
if not triangles:
triangles = obj_root.findall('.//triangles')
if triangles:
# Count triangle elements
for tri_elem in triangles:
face_count += len(tri_elem.findall('.//{http://schemas.microsoft.com/3dmanufacturing/core/2015/02}triangle'))
if face_count == 0: # Try without namespace
face_count += len(tri_elem.findall('.//triangle'))
# Build the XML structure
config = ET.Element('config')
# Object section
obj = ET.SubElement(config, 'object', id='2')
ET.SubElement(obj, 'metadata', key='name', value='SmithForge')
ET.SubElement(obj, 'metadata', key='extruder', value='1')
if face_count > 0:
ET.SubElement(obj, 'metadata', face_count=str(face_count))
part = ET.SubElement(obj, 'part', id='1', subtype='normal_part')
ET.SubElement(part, 'metadata', key='name', value='SmithForge')
ET.SubElement(part, 'metadata', key='matrix', value='1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1')
ET.SubElement(part, 'metadata', key='source_file', value='SmithForge/combined_model.3mf')
ET.SubElement(part, 'metadata', key='source_object_id', value='0')
ET.SubElement(part, 'metadata', key='source_volume_id', value='0')
ET.SubElement(part, 'metadata', key='source_offset_x', value='0')
ET.SubElement(part, 'metadata', key='source_offset_y', value='0')
ET.SubElement(part, 'metadata', key='source_offset_z', value='0')
if face_count > 0:
ET.SubElement(part, 'mesh_stat',
face_count=str(face_count),
edges_fixed='0',
degenerate_facets='0',
facets_removed='0',
facets_reversed='0',
backwards_edges='0')
# Plate section
plate = ET.SubElement(config, 'plate')
ET.SubElement(plate, 'metadata', key='plater_id', value='1')
ET.SubElement(plate, 'metadata', key='plater_name', value='')
ET.SubElement(plate, 'metadata', key='locked', value='false')
model_instance = ET.SubElement(plate, 'model_instance')
ET.SubElement(model_instance, 'metadata', key='object_id', value='2')
ET.SubElement(model_instance, 'metadata', key='instance_id', value='0')
ET.SubElement(model_instance, 'metadata', key='identify_id', value='2')
# Assemble section
assemble = ET.SubElement(config, 'assemble')
ET.SubElement(assemble, 'assemble_item',
object_id='2',
instance_id='0',
transform=transform,
offset='0 0 0')
# Convert to string
tree = ET.ElementTree(config)
ET.indent(tree, space=' ')
# Return as string
import io
output = io.BytesIO()
tree.write(output, encoding='utf-8', xml_declaration=True)
return output.getvalue().decode('utf-8')
except Exception as e:
print(f"⚠️ Error creating model_settings.config: {e}")
import traceback
traceback.print_exc()
# Return minimal valid config as fallback
return '''<?xml version='1.0' encoding='utf-8'?>
<config>
<object id="2">
<metadata key="extruder" value="1"/>
<part id="1" subtype="normal_part">
<metadata key="matrix" value="1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1"/>
<metadata key="source_object_id" value="0"/>
<metadata key="source_volume_id" value="0"/>
<metadata key="source_offset_x" value="0"/>
<metadata key="source_offset_y" value="0"/>
<metadata key="source_offset_z" value="0"/>
</part>
</object>
<plate>
<metadata key="plater_id" value="1"/>
<model_instance>
<metadata key="object_id" value="2"/>
<metadata key="instance_id" value="0"/>
<metadata key="identify_id" value="2"/>
</model_instance>
</plate>
<assemble>
<assemble_item object_id="2" instance_id="0" transform="1 0 0 0 1 0 0 0 1 128 128 0" offset="0 0 0"/>
</assemble>
</config>
'''
def fix_build_plate_transform(output_3mf_path):
"""
Fix the build plate transform in a Bambu 3MF to center the object.
After Bambu CLI export, the build transform may place objects off the build plate.
This function corrects it to center the object on a 256x256mm build plate.
Args:
output_3mf_path: Path to the 3MF file to modify
"""
try:
import xml.etree.ElementTree as ET
# Create a temporary directory to work with the 3MF contents
with tempfile.TemporaryDirectory() as temp_dir:
# Extract the 3MF
with zipfile.ZipFile(output_3mf_path, 'r') as zf:
zf.extractall(temp_dir)
# Parse object model to get vertex bounds
object_model_path = os.path.join(temp_dir, '3D', 'Objects', 'object_1.model')
if not os.path.exists(object_model_path):
print("⚠️ Warning: Could not find 3D/Objects/object_1.model, skipping transform fix")
return
# Parse the object XML to find vertex Z bounds
tree = ET.parse(object_model_path)
root = tree.getroot()
# Find all vertices
ns = {'': 'http://schemas.microsoft.com/3dmanufacturing/core/2015/02'}
vertices = root.findall('.//vertices/vertex', ns)
if not vertices:
# Try without namespace
vertices = root.findall('.//vertices/vertex')
if not vertices:
print("⚠️ Warning: Could not find vertices in object model")
return
# Calculate Z bounds
z_values = []
for vertex in vertices:
z = float(vertex.get('z', 0))
z_values.append(z)
z_min = min(z_values)
print(f"📐 Object Z bounds: min={z_min:.3f}")
# Parse main 3D model to update build transform
model_path = os.path.join(temp_dir, '3D', '3dmodel.model')
if not os.path.exists(model_path):
print("⚠️ Warning: Could not find 3D/3dmodel.model, skipping transform fix")
return
tree = ET.parse(model_path)
root = tree.getroot()
# Find build item element
build_items = root.findall('.//build/item', ns)
if not build_items:
# Try without namespace
build_items = root.findall('.//build/item')
if not build_items:
print("⚠️ Warning: Could not find build item in 3dmodel.model")
return
# Update transform for the first item (should be only one)
item = build_items[0]
# Calculate correct transform for 256x256mm build plate
# Center at (128, 128), bottom at Z=0
z_offset = -z_min # Move bottom to Z=0
new_transform = f"1 0 0 0 1 0 0 0 1 128 128 {z_offset}"
item.set('transform', new_transform)
print(f"🎯 Fixed build plate transform: (128, 128, {z_offset:.3f})")
# Write back the modified XML
tree.write(model_path, encoding='utf-8', xml_declaration=True)
# Repack the 3MF
temp_output = output_3mf_path + '.tmp'
with zipfile.ZipFile(temp_output, 'w', zipfile.ZIP_DEFLATED) as zf_out:
for root_dir, dirs, files in os.walk(temp_dir):
for file in files:
file_path = os.path.join(root_dir, file)
arcname = os.path.relpath(file_path, temp_dir)
zf_out.write(file_path, arcname)
# Replace original with modified
shutil.move(temp_output, output_3mf_path)
print(f"✅ Build plate transform fixed")
except Exception as e:
print(f"⚠️ Error fixing build plate transform: {e}")
import traceback
traceback.print_exc()
def fix_namespace_declarations(output_3mf_path):
"""
Fix namespace declaration mismatch in Bambu CLI exported 3MF files.
Problem: Bambu CLI creates files with:
- xmlns:ns1="http://schemas.microsoft.com/3dmanufacturing/production/2015/06"
- Uses ns1:path, ns1:UUID, etc.
- But declares requiredextensions="p" (without xmlns:p declaration)
This causes Bambu Studio to reject the file as invalid.
Solution: Replace ns1: prefix with p: and add proper xmlns:p declaration.
Args:
output_3mf_path: Path to the 3MF file to modify
"""
try:
import xml.etree.ElementTree as ET
import re
print("🔧 Fixing namespace declarations...")
# Create a temporary directory to work with the 3MF contents
with tempfile.TemporaryDirectory() as temp_dir:
# Extract the 3MF
with zipfile.ZipFile(output_3mf_path, 'r') as zf:
zf.extractall(temp_dir)
# Fix 3D/3dmodel.model
model_path = os.path.join(temp_dir, '3D', '3dmodel.model')
if not os.path.exists(model_path):
print("⚠️ Warning: Could not find 3D/3dmodel.model, skipping namespace fix")
return
# Read the XML file as text for easier namespace replacement
with open(model_path, 'r', encoding='utf-8') as f:
xml_content = f.read()
# Check if we have the ns1 namespace issue
if 'xmlns:ns1=' in xml_content and 'requiredextensions="p"' in xml_content:
# Replace xmlns:ns1 with xmlns:p
xml_content = xml_content.replace(
'xmlns:ns1="http://schemas.microsoft.com/3dmanufacturing/production/2015/06"',
'xmlns:p="http://schemas.microsoft.com/3dmanufacturing/production/2015/06"'
)
# Replace all ns1: attribute prefixes with p:
xml_content = re.sub(r'\bns1:', 'p:', xml_content)
print("✅ Fixed namespace: ns1: → p:")
# CRITICAL: Add BambuStudio namespace to prevent "other vendor" detection
if 'xmlns:BambuStudio=' not in xml_content:
# Find the model root element and add BambuStudio namespace
xml_content = xml_content.replace(
'requiredextensions="p"',
'requiredextensions="p" xmlns:BambuStudio="http://schemas.bambulab.com/package/2021"'
)
print("✅ Added BambuStudio namespace")
# CRITICAL FIX: Replace ns0: prefix with default namespace (no prefix)
# Bambu Studio expects <model xmlns=...> not <ns0:model xmlns:ns0=...>
if 'xmlns:ns0=' in xml_content and '<ns0:model' in xml_content:
# Replace prefixed namespace with default namespace
xml_content = xml_content.replace(
'xmlns:ns0="http://schemas.microsoft.com/3dmanufacturing/core/2015/02"',
'xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02"'
)
# Remove all ns0: prefixes from elements
xml_content = re.sub(r'<ns0:', '<', xml_content)
xml_content = re.sub(r'</ns0:', '</', xml_content)
print("✅ Converted ns0: prefix to default namespace")
# Write back the fixed XML
with open(model_path, 'w', encoding='utf-8') as f:
f.write(xml_content)
# Repack the 3MF
temp_output = output_3mf_path + '.tmp'
with zipfile.ZipFile(temp_output, 'w', zipfile.ZIP_DEFLATED) as zf_out:
for root_dir, dirs, files in os.walk(temp_dir):
for file in files:
file_path = os.path.join(root_dir, file)
arcname = os.path.relpath(file_path, temp_dir)
zf_out.write(file_path, arcname)
# Replace original with modified
shutil.move(temp_output, output_3mf_path)
print("✅ Namespace declarations fixed")
except Exception as e:
print(f"⚠️ Error fixing namespace declarations: {e}")
import traceback
traceback.print_exc()
def export_with_bambustudio_cli(mesh, output_path):
"""
Export a mesh to Bambu Studio format using the Bambu Studio CLI.
This creates a proper Bambu Studio 3MF structure that is compatible with
color layer metadata injection.
Args:
mesh: trimesh.Trimesh object to export
output_path: Path where the final 3MF should be saved
Returns:
bool: True if export succeeded, False otherwise
Raises:
RuntimeError: If bambu-studio CLI is not available
"""
import subprocess
import shutil
# Check if bambu-studio command exists
if not shutil.which('bambu-studio'):
raise RuntimeError(
"bambu-studio command not found. "
"Bambu Studio CLI is required for Bambu format exports. "
"Please ensure Bambu Studio is installed in the container."
)
# Create temporary input file (standard trimesh export)
temp_input = None
try:
import tempfile
with tempfile.NamedTemporaryFile(suffix='.3mf', delete=False) as tmp:
temp_input = tmp.name
print(f"📄 Creating temporary 3MF for Bambu Studio conversion: {temp_input}")
mesh.export(temp_input)
# Run Bambu Studio CLI to convert to proper Bambu format
# Just export without slicing to preserve geometry
cmd = [
'bambu-studio',
'--export-3mf', output_path,
temp_input
]
print(f"🚀 Running Bambu Studio CLI: {' '.join(cmd)}")
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=120 # 2 minute timeout
)
if result.returncode != 0:
print(f"❌ Bambu Studio CLI failed with return code {result.returncode}")
if result.stdout:
print(f" stdout: {result.stdout}")
if result.stderr:
print(f" stderr: {result.stderr}")
return False
print("✅ Bambu Studio CLI export successful")
if result.stdout:
print(f" Output: {result.stdout}")
return True
except subprocess.TimeoutExpired:
print("❌ Bambu Studio CLI timed out after 120 seconds")
return False
except Exception as e:
print(f"❌ Error during Bambu Studio CLI export: {e}")
return False
finally:
# Clean up temporary file
if temp_input and os.path.exists(temp_input):
try:
os.remove(temp_input)
print(f"🗑️ Cleaned up temporary file: {temp_input}")
except Exception as e:
print(f"⚠️ Could not remove temporary file {temp_input}: {e}")
def sample_perimeter_height(mesh, num_samples=40):
"""
Sample Z-heights along the perimeter of a mesh to detect the background height.
Uses boundary edge detection to find perimeter vertices and samples their heights,
which typically represents the Hueforge background layer.
Args:
mesh: trimesh.Trimesh object
num_samples: Number of points to sample along the perimeter (default 40)
Returns:
float: The most common (mode) Z-height value from perimeter samples
"""
import numpy as np
# Method 1: Try to find boundary edges (edges that appear only once)
try:
# Get all edges
edges = mesh.edges_unique
# Count how many faces each edge belongs to
edge_face_count = np.bincount(mesh.edges_unique_inverse)
# Boundary edges appear in only one face
boundary_mask = edge_face_count == 1
boundary_edges = edges[boundary_mask]
if len(boundary_edges) > 0:
# Get unique vertices from boundary edges
boundary_vertices = np.unique(boundary_edges.flatten())
z_heights = mesh.vertices[boundary_vertices, 2]
print(f"📏 Found {len(boundary_vertices)} boundary vertices")
else:
# Fallback to Method 2
raise ValueError("No boundary edges found")
except Exception as e:
# Method 2: Use 2D convex hull to find perimeter vertices
print("ℹ️ Using 2D projection method for perimeter detection")
try:
import shapely.geometry
# Project vertices to 2D
points_2d = mesh.vertices[:, :2]
# Create convex hull
hull = shapely.geometry.MultiPoint(points_2d).convex_hull
# Find vertices that are on or near the hull boundary
z_heights = []
tolerance = 0.5 # mm tolerance for being "on" the boundary
for i, vertex_2d in enumerate(points_2d):
point = shapely.geometry.Point(vertex_2d)
if hull.boundary.distance(point) < tolerance:
z_heights.append(mesh.vertices[i, 2])
z_heights = np.array(z_heights)
print(f"📏 Found {len(z_heights)} perimeter vertices using 2D hull")
except Exception as e2:
# Final fallback: sample from top layer
print(f"⚠️ Warning: Could not detect perimeter, using top layer sampling")
# Get vertices in the top 10% of Z range
z_min, z_max = mesh.vertices[:, 2].min(), mesh.vertices[:, 2].max()
z_threshold = z_max - 0.1 * (z_max - z_min)
top_vertices = mesh.vertices[mesh.vertices[:, 2] > z_threshold]
z_heights = top_vertices[:, 2]
if len(z_heights) == 0:
print("⚠️ Warning: No perimeter points found, using mesh maximum Z")
return mesh.bounds[1][2]
# Limit to num_samples if we have too many
if len(z_heights) > num_samples:
indices = np.linspace(0, len(z_heights) - 1, num_samples, dtype=int)
z_heights = z_heights[indices]
# Find the mode using histogram binning
# Use fewer bins for more stable mode detection
hist, bins = np.histogram(z_heights, bins=min(10, len(z_heights)//2))
mode_bin_idx = np.argmax(hist)
# Return the center of the most common bin
background_height = (bins[mode_bin_idx] + bins[mode_bin_idx + 1]) / 2.0
print(f"📏 Sampled {len(z_heights)} Z-heights from perimeter")
print(f"📏 Detected background height: {background_height:.3f} mm")
print(f"📏 Height range: {z_heights.min():.3f} to {z_heights.max():.3f} mm")
return background_height
def create_fill_geometry(base_mesh, hueforge_mesh, fill_height, base_top_z):
"""
Create fill geometry to fill gaps between a scaled-down Hueforge overlay and base boundaries.
Args:
base_mesh: The base mesh (defines outer boundary)
hueforge_mesh: The Hueforge overlay mesh (defines inner boundary)
fill_height: The Z-height at which to create the fill (typically Hueforge background height)
base_top_z: The Z coordinate of the top of the base mesh
Returns:
trimesh.Trimesh: Fill mesh, or None if no gap exists
"""
import numpy as np
# Get 2D projections (XY plane)
base_verts_2d = [(v[0], v[1]) for v in base_mesh.vertices]
hf_verts_2d = [(v[0], v[1]) for v in hueforge_mesh.vertices]
# Create convex hulls for both shapes
base_hull = shapely.geometry.MultiPoint(base_verts_2d).convex_hull
hf_hull = shapely.geometry.MultiPoint(hf_verts_2d).convex_hull
# Check if there's actually a gap to fill
if hf_hull.contains(base_hull) or hf_hull.equals(base_hull):
print("ℹ️ Hueforge covers entire base area - no gap filling needed")
return None
# Compute the difference region (gap area between base and Hueforge)
gap_region = base_hull.difference(hf_hull)
if gap_region.is_empty or gap_region.area < 1e-6:
print("ℹ️ Gap area is negligible - no fill geometry created")
return None
print(f"📐 Gap area detected: {gap_region.area:.2f} mm²")
# Calculate the height of the fill extrusion
# Fill should extend from base_top_z to the detected fill_height
# The fill_height is the detected background height of the Hueforge
fill_thickness = max(fill_height - base_top_z, 0.2) # Ensure minimum thickness
print(f"📐 Creating fill geometry: thickness = {fill_thickness:.3f} mm")
print(f" Base top: {base_top_z:.3f} mm, Fill top: {fill_height:.3f} mm")
# Extrude the gap region to create fill mesh
# Handle both Polygon and MultiPolygon cases
try:
fill_meshes = []
# Check if it's a MultiPolygon or single Polygon
from shapely.geometry import Polygon, MultiPolygon
if isinstance(gap_region, MultiPolygon):
print(f"📐 Gap region has {len(gap_region.geoms)} separate areas")
# Handle each polygon separately
for i, polygon in enumerate(gap_region.geoms):
if polygon.area > 1e-6: # Skip tiny fragments
try:
mesh = trimesh.creation.extrude_polygon(polygon, height=fill_thickness)
mesh.apply_translation([0, 0, base_top_z])
fill_meshes.append(mesh)
print(f" Created fill mesh {i+1}: {len(mesh.vertices)} vertices")
except Exception as e:
print(f" ⚠️ Failed to create fill for polygon {i+1}: {e}")
elif isinstance(gap_region, Polygon):
# Single polygon case
mesh = trimesh.creation.extrude_polygon(gap_region, height=fill_thickness)
mesh.apply_translation([0, 0, base_top_z])
fill_meshes.append(mesh)
else:
print(f"⚠️ Unexpected gap region type: {type(gap_region)}")
return None
# Combine all fill meshes if there are multiple
if len(fill_meshes) == 0:
print("⚠️ No fill meshes could be created")
return None
elif len(fill_meshes) == 1:
fill_mesh = fill_meshes[0]
else:
# Combine multiple meshes
fill_mesh = trimesh.util.concatenate(fill_meshes)
print(f"📐 Combined {len(fill_meshes)} fill meshes")
print(f"✅ Fill geometry created: {len(fill_mesh.vertices)} vertices, {len(fill_mesh.faces)} faces")
return fill_mesh
except Exception as e:
print(f"⚠️ Failed to create fill geometry: {e}")
import traceback
traceback.print_exc()
return None
def modify_3mf(hueforge_path, base_path, output_path,
scaledown, rotate_base,
xshift, yshift, zshift,
force_scale=None,
preserve_colors=False,
auto_repair=False,
fill_gaps=False,
inject_colors_text=None,
output_format="standard"):
"""
1) Rotate the base around Z by --rotatebase degrees (if nonzero).
2) Compute scale so Hueforge fully occupies at least one dimension => scale = max(scale_x, scale_y).
3) If scale < 1 and not --nominimum, clamp scale to 1.
4) Center Hueforge on the base in (x, y).
5) Embed Hueforge in Z for real overlap (see DEFAULT_EMBEDDING_OVERLAP_MM constant).
6) Apply user-specified shifts: --xshift, --yshift, --zshift
7) Build a 2D convex hull from base's XY, extrude => 'cutter'.
8) Intersect Hueforge with that cutter => clip outside base shape.
9) If fill_gaps=True, detect background height and create fill geometry for gaps between overlay and base.
10) Union clipped Hueforge (+ fill if enabled) + base => single manifold => export.
11) If preserve_colors=True, extract color layers from Hueforge and inject into output with adjusted Z heights.
12) If inject_colors_text is provided, parse text and inject color layers (mutually exclusive with preserve_colors).
13) If auto_repair=True, automatically validate and repair mesh issues before processing.
14) If output_format='bambu', use lib3mf to generate Bambu Studio compatible 3MF with Production Extension structure.
"""
# Validate mutually exclusive options
if preserve_colors and inject_colors_text:
print("❌ Error: --preserve-colors and --inject-colors-text are mutually exclusive")
print(" Choose one: preserve existing layers OR inject from text")
return
# Extract or parse color layer information if requested
color_data = None
if preserve_colors:
print("🎨 Extracting color layer information from Hueforge...")
color_data = extract_color_layers(hueforge_path)
if color_data is None:
print("ℹ️ No color layer data found, proceeding without color preservation")
elif inject_colors_text:
if parse_swap_instructions is None:
print("❌ Error: Text layer parser not available")
return
print("🎨 Parsing color layer information from text...")
# Read the text file content
try:
with open(inject_colors_text, 'r') as f: