Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
1385 lines (1309 loc) · 69.7 KB
/
Copy pathindex.html
File metadata and controls
1385 lines (1309 loc) · 69.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Boolean Exact Volume (boolean-exact-volume) — Examples — Blender Developer Tools</title>
<meta name="description" content="The Boolean modifier on the EXACT solver: union, difference and intersection of a cube and a slab whose top face is coplanar with the cube's, read back through the depsgraph and measured by the divergence theorem." />
<link rel="canonical" href="https://tmhsdigital.github.io/Blender-Developer-Tools/gallery/boolean-exact-volume/" />
<meta property="og:url" content="https://tmhsdigital.github.io/Blender-Developer-Tools/gallery/boolean-exact-volume/" />
<link rel="icon" href="../../assets/favicon.svg" type="image/svg+xml" />
<meta name="theme-color" content="#1a1b1e" />
<meta name="color-scheme" content="dark" />
<meta property="og:type" content="article" />
<meta property="og:title" content="Boolean Exact Volume (boolean-exact-volume) — Examples — Blender Developer Tools" />
<meta property="og:description" content="The Boolean modifier on the EXACT solver: union, difference and intersection of a cube and a slab whose top face is coplanar with the cube's, read back through the depsgraph and measured by the divergence theorem." />
<meta property="og:image" content="https://tmhsdigital.github.io/Blender-Developer-Tools/gallery/assets/boolean-exact-volume-hero.webp" />
<meta property="og:image:width" content="1280" />
<meta property="og:image:height" content="720" />
<meta property="og:image:alt" content="Three results on a walnut plinth behind brass UNION, DIFFERENCE and INTERSECT plaques: a teal union, a bronze cube notched under amber ghost glass, and an orange cube inside frosted glass." />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="https://tmhsdigital.github.io/Blender-Developer-Tools/gallery/assets/boolean-exact-volume-hero.webp" />
<meta name="twitter:image:alt" content="Three results on a walnut plinth behind brass UNION, DIFFERENCE and INTERSECT plaques: a teal union, a bronze cube notched under amber ghost glass, and an orange cube inside frosted glass." />
<meta property="og:site_name" content="Blender Developer Tools" />
<meta name="twitter:title" content="Boolean Exact Volume (boolean-exact-volume) — Examples — Blender Developer Tools" />
<meta name="twitter:description" content="The Boolean modifier on the EXACT solver: union, difference and intersection of a cube and a slab whose top face is coplanar with the cube's, read back through the depsgraph and measured by the divergence theorem." />
<link rel="preload" href="../../fonts/inter-regular.woff2" as="font" type="font/woff2" crossorigin />
<link rel="stylesheet" href="../gallery.css?v=27ce4298f2" />
<script>document.documentElement.classList.add('js');</script>
</head>
<body>
<a class="skip" href="#main">Skip to content</a>
<header class="site-header">
<div class="site-header-inner">
<a class="site-brand" href="../../"><span class="site-brand-dot" aria-hidden="true"></span>Blender Developer Tools</a>
<button class="site-menu-btn" type="button" aria-expanded="false" aria-controls="site-nav"><span class="site-menu-icon" aria-hidden="true"></span>Menu</button>
<nav class="site-nav" id="site-nav" aria-label="Primary">
<ul><li><a href="../../gallery/#k=examples">Examples</a></li><li><a href="../../gallery/#k=showcase">Showcase</a></li><li><a href="../../#new">What's new</a></li><li><a href="../../#skills">Skills</a></li><li><a href="../../#rules">Rules</a></li><li><a href="../../#snippets">Snippets</a></li><li><a href="../../#install">Install</a></li><li class="gh"><a href="https://github.com/TMHSDigital/Blender-Developer-Tools">GitHub</a></li></ul>
</nav>
</div>
</header>
<header class="hero">
<nav class="crumbs" aria-label="Breadcrumb"><ol><li><a href="../" id="crumbGallery">Gallery</a></li><li><a href="../#k=examples">Examples</a></li><li aria-current="page">boolean-exact-volume</li></ol></nav>
<h1>Boolean Exact Volume</h1>
<p class="slug">examples/boolean-exact-volume/</p>
<p>The Boolean modifier on the EXACT solver: union, difference and intersection of a cube and a slab whose top face is coplanar with the cube's, read back through the depsgraph and measured by the divergence theorem.</p>
</header>
<main id="main">
<nav class="pager" aria-label="Examples"><a rel="prev" href="../solidify-even-thickness/" aria-label="Previous example: solidify-even-thickness"><span aria-hidden="true">←</span> solidify-even-thickness</a><span class="pager-pos hud">56 of 56 examples<span class="pager-keys"> · <kbd>←</kbd><kbd>→</kbd></span></span><span></span></nav>
<button class="detail-hero" id="heroZoom" type="button" aria-label="View full size: Three results on a walnut plinth behind brass UNION, DIFFERENCE and INTERSECT plaques: a teal union, a bronze cube notched under amber ghost glass, and an orange cube inside frosted glass.">
<img src="../assets/boolean-exact-volume-hero.webp" alt="Three results on a walnut plinth behind brass UNION, DIFFERENCE and INTERSECT plaques: a teal union, a bronze cube notched under amber ghost glass, and an orange cube inside frosted glass." width="1280" height="720" fetchpriority="high" />
</button>
<p class="zoom-hint">Rendered headless by the example itself. Select it to enlarge.</p>
<div class="callout"><span class="tag">witnesses</span> EXACT booleans are closed-form even on coplanar faces: through translated, 18°-turned operands the union, difference and intersection measure 9, 7 and 1 m³ (8.999999 / 6.999999 / 1.000000, float32) and are closed 2-manifolds with no loose geometry; the floating-point solver ('FAST' on 4.5, 'FLOAT' on 5.x) gives a 7.708 m³ union and --float-solver exits 3.</div>
<p class="taglist"><span class="tag">tags</span> <a href="../#tag=mesh">mesh</a> <a href="../#tag=modifiers">modifiers</a> <a href="../#tag=depsgraph">depsgraph</a></p>
<div class="runline">
<pre id="runCmd">blender --background --python examples/boolean-exact-volume/boolean_exact_volume.py --</pre>
<button class="copy-btn" id="copyRun" type="button">Copy</button>
</div>
<section class="detail-section md">
<p>A runnable example that cuts a 2 m cube with a 2 × 1 × 1 m slab using the <strong>Boolean modifier on the EXACT solver</strong>, in all three operations (UNION, DIFFERENCE, INTERSECT), then proves that each result has the <strong>closed-form volume</strong> and is a <strong>closed 2-manifold</strong>. The hard part is deliberate: the slab's top face lies in the cube's top plane, the coplanar case where floating-point booleans go wrong. The results are read through the depsgraph lifetime contract from <a href="https://github.com/TMHSDigital/Blender-Developer-Tools/tree/main/skills/depsgraph-and-evaluated-data/SKILL.md"><code>depsgraph-and-evaluated-data</code></a> (<code>evaluated_get</code> → <code>to_mesh</code> → <code>to_mesh_clear</code>), and the operands are built with bmesh and freed in <code>try</code>/<code>finally</code> as <a href="https://github.com/TMHSDigital/Blender-Developer-Tools/tree/main/skills/mesh-editing-and-bmesh/SKILL.md"><code>mesh-editing-and-bmesh</code></a> requires.</p>
<p><strong>What it witnesses:</strong> with A = [0,2]³ and B = [1,3] × [0.5,1.5] × [1,2], the overlap is A ∩ B = [1,2] × [0.5,1.5] × [1,2], so the closed forms are V(A ∪ B) = 8 + 2 − 1 = 9, V(A − B) = 8 − 1 = 7 and V(A ∩ B) = 1 cubic metre. Each operand pair sits in an object matrix that is translated and turned 18° about Z, so the modifier has to carry B into A's space. For every result the check:</p>
<ul><li>sums the signed volume over the evaluated mesh's own triangles (divergence theorem, world space) and requires the closed form to 1e-6 relative (measured 8.999999, 6.999999 and 1.000000 — float32 coordinates through a rotation). A positive volume also means the normals point outward;</li><li>requires a closed 2-manifold: every edge borders exactly two faces, with no loose verts or edges;</li><li>requires that the result's volume differs from V(A) by at least 0.5 m³, so the modifier really acted and the witness cannot pass vacuously.</li></ul>
<p><strong>The trap it exposes:</strong> the floating-point solver is faster and is right in general position, but it mishandles coplanar faces. <code>--float-solver</code> runs the same three operations on it and gets a union of 7.708 m³ against the closed form 9, so check 3 fails with the measured error. That solver was <code>'FAST'</code> through 4.5 LTS and is <code>'FLOAT'</code> from 5.0, so a script that hard-codes either name breaks on the other series (<code>fast_solver_id()</code> branches on <code>bpy.app.version</code>). <code>'EXACT'</code> is the same identifier on 4.5, 5.1 and 5.2.</p>
<p>The still shows the three operations side by side on a walnut plinth, each named by a brass plaque in front of it. On the left, the union in teal glaze, with the slab's free end grown out of the cube. In the middle, the difference in machined bronze, with the notch cut down through the top face and the removed slab drawn as frosted amber glass. On the right, the intersection: the 1 m³ overlap cube in orange glaze, inside frosted ghosts of both operands. Thin rods trace the operands' edges (steel for the cube, amber for the coplanar slab).</p>
<p>Everything shown is render-only and is built after the check has run. The results on display are frozen copies of the checked meshes with a 12 mm chamfer; the measured modifier objects and the cutter objects never render. The ghosts are grown 2 mm and 4 mm so none of their faces is coplanar with a result or with each other.</p>
<h2 id="run">Run<a class="anchor" href="#run" aria-label="Link to this section">#</a></h2>
<pre><code># Cheap correctness check (no render) — the CI check:
blender --background --python boolean_exact_volume.py --
# Falsifier: the floating-point solver (FLOAT / FAST) on the coplanar case. Must exit 3.
blender --background --python boolean_exact_volume.py -- --float-solver
# Also render a still (EEVEE on a GPU host; use --engine cycles on GPU-less hosts):
blender --background --python boolean_exact_volume.py -- --output boolean.png --engine cycles</code></pre>
<h2 id="version-notes">Version notes<a class="anchor" href="#version-notes" aria-label="Link to this section">#</a></h2>
<p><code>BooleanModifier.solver</code> lists <code>'FAST'</code>, <code>'EXACT'</code>, <code>'MANIFOLD'</code> on 4.5 LTS and <code>'FLOAT'</code>, <code>'EXACT'</code>, <code>'MANIFOLD'</code> on 5.1 and 5.2 LTS: the floating-point solver was renamed in 5.0. Measured identically on 4.5.11, 5.1.2 and 5.2.1: EXACT union 8.999999 (12 faces), difference 6.999999 (10 faces), intersection 1.000000 (6 faces), all closed 2-manifolds; <code>--float-solver</code> union 7.708379.</p>
<h2 id="exit-codes">Exit codes<a class="anchor" href="#exit-codes" aria-label="Link to this section">#</a></h2>
<div class="table-wrap"><table><thead><tr><th scope="col">Code</th><th scope="col">Meaning</th></tr></thead><tbody><tr><td>0</td><td>Success</td></tr><tr><td>1</td><td>Uncaught exception (FATAL wrapper)</td></tr><tr><td>2</td><td>argparse / usage</td></tr><tr><td>3</td><td>A result's volume is off its closed form (<code>--float-solver</code> lands here)</td></tr><tr><td>4</td><td>A result is not a closed 2-manifold (an edge without exactly two faces, or loose geometry)</td></tr><tr><td>5</td><td>A result's volume is within 0.5 m³ of operand A's (the witness would pass vacuously)</td></tr><tr><td>6</td><td><code>--output</code> produced no file</td></tr><tr><td>10</td><td><code>--output</code> framing violation (Layer 1 fill / margin gate, <code>gallery_framing</code>)</td></tr></tbody></table></div>
<p>The <code>blender-smoke</code> workflow runs the check on Blender 5.2 LTS and 4.5 LTS (5.1 on the weekly cron, the <code>needs-5.1</code> PR label, or manual dispatch). Smoke does not pass <code>--output</code> or <code>--float-solver</code>.</p>
</section>
<section class="detail-section src" id="source">
<h2>Source</h2>
<div class="src-meta">
<code>examples/boolean-exact-volume/boolean_exact_volume.py</code>
<span>537 lines · <a href="https://github.com/TMHSDigital/Blender-Developer-Tools/tree/main/examples/boolean-exact-volume/boolean_exact_volume.py">View on GitHub →</a></span>
</div>
<div class="code" data-lines="537">
<pre class="gutter" aria-hidden="true">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</pre><pre class="code-lines"><span class="s">"""EXACT Boolean volumes are closed-form and manifold, even on coplanar faces — a runnable example.
Witnesses the Boolean modifier contract with ``solver = 'EXACT'``. Operand A
is a 2 m cube and operand B a 2 x 1 x 1 m slab that overlaps A by a 1 m cube
and whose top face lies in A's top plane: the coplanar case. Both operands
sit in object matrices that are translated and turned about Z, so the
modifier has to bring B into A's space. For each of UNION, DIFFERENCE and
INTERSECT the evaluated mesh (``evaluated_get`` + ``to_mesh`` /
``to_mesh_clear``) is measured in world space:
- 3: its signed volume, by the divergence theorem over its own triangles,
matches the closed form (V(A) + V(B) - V(A n B) = 9, V(A) - V(A n B) = 7,
V(A n B) = 1 cubic metres) to 1e-6 relative; positive volume also means
the normals point outward;
- 4: it is a closed 2-manifold: every edge borders exactly two faces, and
there are no loose verts or edges;
- 5: it is not vacuous: each result's volume differs from V(A) by at least
0.5 m^3, so the modifier really acted.
``--float-solver`` runs the same three operations on the floating-point
solver (``'FLOAT'`` on Blender 5.x, ``'FAST'`` on 4.5 LTS, the same solver
under its older name). On the coplanar top face it returns a wrong union
(7.708 m^3 against 9, measured identically on 4.5.11, 5.1.2 and 5.2.1), so
check 3 fails with the measured error. That is the falsifier, and the documented failure case
the EXACT solver exists to handle.
By default it runs only the correctness check (no render) — the CI smoke
check. Pass --output to also render a still:
blender --background --python boolean_exact_volume.py -- # check only
blender --background --python boolean_exact_volume.py -- --float-solver # must fail
blender --background --python boolean_exact_volume.py -- --output b.png # + render
"""</span>
<span class="k">import</span> bpy, bmesh, sys, os, math, argparse
<span class="k">from</span> mathutils <span class="k">import</span> Vector, Matrix
<span class="c"># Shared Layer 1 framing measurement (render path only) — see gallery_framing.py</span>
sys.path.insert(<span class="n">0</span>, os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir))
sys.dont_write_bytecode = <span class="k">True</span> <span class="c"># keep examples/__pycache__ out of the repo tree</span>
<span class="k">import</span> gallery_framing
<span class="c"># Operands in their shared local frame (metres). B's top face (z = 2) is</span>
<span class="c"># coplanar with A's top face; the overlap A n B is [1,2] x [0.5,1.5] x [1,2].</span>
A_LO, A_HI = (<span class="n">0.0</span>, <span class="n">0.0</span>, <span class="n">0.0</span>), (<span class="n">2.0</span>, <span class="n">2.0</span>, <span class="n">2.0</span>)
B_LO, B_HI = (<span class="n">1.0</span>, <span class="n">0.5</span>, <span class="n">1.0</span>), (<span class="n">3.0</span>, <span class="n">1.5</span>, <span class="n">2.0</span>)
OPS = (<span class="s">'UNION'</span>, <span class="s">'DIFFERENCE'</span>, <span class="s">'INTERSECT'</span>)
SPACING = <span class="n">4.6</span> <span class="c"># world X between the three operation sets</span>
TURN = math.radians(-<span class="n">18.0</span>) <span class="c"># every set's frame turns about Z</span>
<span class="k">def</span> box_volume(lo, hi):
<span class="k">return</span> (hi[<span class="n">0</span>] - lo[<span class="n">0</span>]) * (hi[<span class="n">1</span>] - lo[<span class="n">1</span>]) * (hi[<span class="n">2</span>] - lo[<span class="n">2</span>])
<span class="k">def</span> overlap(lo1, hi1, lo2, hi2):
lo = tuple(max(lo1[k], lo2[k]) <span class="k">for</span> k <span class="k">in</span> range(<span class="n">3</span>))
hi = tuple(min(hi1[k], hi2[k]) <span class="k">for</span> k <span class="k">in</span> range(<span class="n">3</span>))
<span class="k">return</span> lo, hi
V_A = box_volume(A_LO, A_HI)
V_B = box_volume(B_LO, B_HI)
V_AB = box_volume(*overlap(A_LO, A_HI, B_LO, B_HI))
EXPECTED = {<span class="s">'UNION'</span>: V_A + V_B - V_AB, <span class="s">'DIFFERENCE'</span>: V_A - V_AB, <span class="s">'INTERSECT'</span>: V_AB}
REL_TOL = <span class="n">1e-6</span>
MIN_CHANGE = <span class="n">0.5</span> <span class="c"># m^3; check 5's floor against V(A)</span>
<span class="k">def</span> fast_solver_id():
<span class="s">"""The floating-point solver: 'FAST' through 4.5 LTS, renamed 'FLOAT' in 5.0."""</span>
<span class="k">return</span> <span class="s">'FLOAT'</span> <span class="k">if</span> bpy.app.version >= (<span class="n">5</span>, <span class="n">0</span>, <span class="n">0</span>) <span class="k">else</span> <span class="s">'FAST'</span>
<span class="k">def</span> box_mesh(name, lo, hi):
me = bpy.data.meshes.new(name)
bm = bmesh.new()
<span class="k">try</span>:
res = bmesh.ops.create_cube(bm, size=<span class="n">1.0</span>)
<span class="k">for</span> vert <span class="k">in</span> res[<span class="s">"verts"</span>]:
vert.co = Vector(tuple(lo[k] + (vert.co[k] + <span class="n">0.5</span>) * (hi[k] - lo[k]) <span class="k">for</span> k <span class="k">in</span> range(<span class="n">3</span>)))
bm.to_mesh(me)
<span class="k">finally</span>:
bm.free()
<span class="k">return</span> me
<span class="k">def</span> frame(i):
<span class="s">"""World matrix of operation set i: translated along X, turned about Z,
with the operands' local frame centred on A's footprint."""</span>
centre = Matrix.Translation((-(A_HI[<span class="n">0</span>] - A_LO[<span class="n">0</span>]) / <span class="n">2</span> - <span class="n">0.5</span>, -(A_HI[<span class="n">1</span>] - A_LO[<span class="n">1</span>]) / <span class="n">2</span>, <span class="n">0.0</span>))
<span class="k">return</span> (Matrix.Translation(((i - <span class="n">1</span>) * SPACING, <span class="n">0.0</span>, <span class="n">0.0</span>))
@ Matrix.Rotation(TURN, <span class="n">4</span>, <span class="s">'Z'</span>) @ centre)
<span class="k">def</span> build_scene(float_solver=<span class="k">False</span>):
bpy.ops.wm.read_factory_settings(use_empty=<span class="k">True</span>)
scene = bpy.context.scene
solver = fast_solver_id() <span class="k">if</span> float_solver <span class="k">else</span> <span class="s">'EXACT'</span>
results = {}
cutters = {}
<span class="k">for</span> i, op <span class="k">in</span> enumerate(OPS):
a = bpy.data.objects.new(<span class="s">f"</span>{op.title()}<span class="s">Result</span><span class="s">"</span>, box_mesh(<span class="s">f"</span>{op.title()}<span class="s">Result</span><span class="s">"</span>, A_LO, A_HI))
b = bpy.data.objects.new(<span class="s">f"</span>{op.title()}<span class="s">Cutter</span><span class="s">"</span>, box_mesh(<span class="s">f"</span>{op.title()}<span class="s">Cutter</span><span class="s">"</span>, B_LO, B_HI))
a.matrix_world = frame(i)
b.matrix_world = frame(i)
b.hide_render = <span class="k">True</span>
b.display_type = <span class="s">'WIRE'</span>
scene.collection.objects.link(a)
scene.collection.objects.link(b)
mod = a.modifiers.new(<span class="s">"Boolean"</span>, <span class="s">'BOOLEAN'</span>)
mod.operation = op
mod.solver = solver
mod.object = b
results[op] = a
cutters[op] = b
<span class="k">return</span> results, cutters, solver
<span class="k">def</span> measure(obj):
<span class="s">"""World-space signed volume (divergence theorem over fan triangles of
each polygon) plus manifold counts, from the evaluated mesh."""</span>
deps = bpy.context.evaluated_depsgraph_get()
ev = obj.evaluated_get(deps)
me = ev.to_mesh()
<span class="k">try</span>:
mw = ev.matrix_world
co = [mw @ v.co <span class="k">for</span> v <span class="k">in</span> me.vertices]
vol = <span class="n">0.0</span>
<span class="k">for</span> p <span class="k">in</span> me.polygons:
idx = list(p.vertices)
v0 = co[idx[<span class="n">0</span>]]
<span class="k">for</span> j <span class="k">in</span> range(<span class="n">1</span>, len(idx) - <span class="n">1</span>):
vol += v0.dot(co[idx[j]].cross(co[idx[j + <span class="n">1</span>]])) / <span class="n">6.0</span>
bm = bmesh.new()
<span class="k">try</span>:
bm.from_mesh(me)
bad_edges = sum(<span class="n">1</span> <span class="k">for</span> e <span class="k">in</span> bm.edges <span class="k">if</span> len(e.link_faces) != <span class="n">2</span>)
loose_verts = sum(<span class="n">1</span> <span class="k">for</span> v <span class="k">in</span> bm.verts <span class="k">if</span> <span class="k">not</span> v.link_faces)
loose_edges = sum(<span class="n">1</span> <span class="k">for</span> e <span class="k">in</span> bm.edges <span class="k">if</span> <span class="k">not</span> e.link_faces)
<span class="k">finally</span>:
bm.free()
<span class="k">return</span> vol, bad_edges, loose_verts, loose_edges, len(me.polygons)
<span class="k">finally</span>:
ev.to_mesh_clear()
<span class="k">def</span> check(results, solver):
bpy.context.view_layer.update()
m = {op: measure(results[op]) <span class="k">for</span> op <span class="k">in</span> OPS}
<span class="c"># 3: volumes on the closed forms (positive => outward normals)</span>
<span class="k">for</span> op <span class="k">in</span> OPS:
vol, exp = m[op][<span class="n">0</span>], EXPECTED[op]
<span class="k">if</span> abs(vol - exp) > REL_TOL * exp:
print(<span class="s">f"</span><span class="s">ERROR: </span>{op}<span class="s"> on solver </span>{solver!r}<span class="s">: volume </span>{vol:<span class="s">.6f</span>}<span class="s"> m^3, closed form </span><span class="s">"</span>
<span class="s">f"</span>{exp:<span class="s">.6f</span>}<span class="s"> (error </span>{vol - exp:<span class="s">+.6f</span>}<span class="s">)</span><span class="s">"</span>, file=sys.stderr)
<span class="k">return</span> <span class="n">3</span>
<span class="c"># 4: closed 2-manifold, no loose geometry</span>
<span class="k">for</span> op <span class="k">in</span> OPS:
_, bad, lv, le, _ = m[op]
<span class="k">if</span> bad <span class="k">or</span> lv <span class="k">or</span> le:
print(<span class="s">f"</span><span class="s">ERROR: </span>{op}<span class="s"> on solver </span>{solver!r}<span class="s"> is not a closed manifold: </span>{bad}<span class="s"> edges </span><span class="s">"</span>
<span class="s">f"</span><span class="s">without exactly two faces, </span>{lv}<span class="s"> loose verts, </span>{le}<span class="s"> loose edges</span><span class="s">"</span>,
file=sys.stderr)
<span class="k">return</span> <span class="n">4</span>
<span class="c"># 5: every result really differs from operand A</span>
<span class="k">for</span> op <span class="k">in</span> OPS:
<span class="k">if</span> abs(m[op][<span class="n">0</span>] - V_A) < MIN_CHANGE:
print(<span class="s">f"</span><span class="s">ERROR: </span>{op}<span class="s"> volume </span>{m[op][<span class="n">0</span>]:<span class="s">.4f</span>}<span class="s"> within </span>{MIN_CHANGE}<span class="s"> of V(A) = </span>{V_A}<span class="s">"</span>,
file=sys.stderr)
<span class="k">return</span> <span class="n">5</span>
parts = <span class="s">", "</span>.join(<span class="s">f"</span>{op}<span class="s"> </span>{m[op][<span class="n">0</span>]:<span class="s">.6f</span>}<span class="s"> (closed form </span>{EXPECTED[op]:<span class="s">g</span>}<span class="s">, </span>{m[op][<span class="n">4</span>]}<span class="s"> faces)</span><span class="s">"</span>
<span class="k">for</span> op <span class="k">in</span> OPS)
print(<span class="s">f"</span><span class="s">solver </span>{solver!r}<span class="s">, coplanar top face: </span>{parts}<span class="s">; all closed 2-manifold, </span><span class="s">"</span>
<span class="s">f"</span><span class="s">0 loose verts/edges</span><span class="s">"</span>)
<span class="k">return</span> <span class="n">0</span>
<span class="k">def</span> eevee_engine_id():
<span class="k">return</span> <span class="s">'BLENDER_EEVEE'</span> <span class="k">if</span> bpy.app.version >= (<span class="n">5</span>, <span class="n">0</span>, <span class="n">0</span>) <span class="k">else</span> <span class="s">'BLENDER_EEVEE_NEXT'</span>
<span class="c"># ---------------------------------------------------------------------------</span>
<span class="c"># Render staging only (runs after the check; never part of it)</span>
<span class="c"># ---------------------------------------------------------------------------</span>
<span class="k">def</span> principled(name, base, rough, metal=<span class="n">0.0</span>, coat=<span class="n">0.0</span>, coat_rough=<span class="n">0.1</span>, rough_var=<span class="n">0.0</span>):
<span class="s">"""A designed Principled material. *rough_var* adds a low-frequency
roughness drift (no fine noise: on metal that reads as glitter)."""</span>
mat = bpy.data.materials.new(name)
mat.use_nodes = <span class="k">True</span>
nt = mat.node_tree
b = nt.nodes[<span class="s">"Principled BSDF"</span>]
b.inputs[<span class="s">"Base Color"</span>].default_value = (*base, <span class="n">1.0</span>)
b.inputs[<span class="s">"Roughness"</span>].default_value = rough
b.inputs[<span class="s">"Metallic"</span>].default_value = metal
<span class="k">if</span> coat:
b.inputs[<span class="s">"Coat Weight"</span>].default_value = coat
b.inputs[<span class="s">"Coat Roughness"</span>].default_value = coat_rough
<span class="k">if</span> rough_var:
tex = nt.nodes.new(<span class="s">"ShaderNodeTexNoise"</span>)
tex.inputs[<span class="s">"Scale"</span>].default_value = <span class="n">3.0</span>
tex.inputs[<span class="s">"Detail"</span>].default_value = <span class="n">2.0</span>
mr = nt.nodes.new(<span class="s">"ShaderNodeMapRange"</span>)
mr.inputs[<span class="s">"To Min"</span>].default_value = max(rough - rough_var, <span class="n">0.0</span>)
mr.inputs[<span class="s">"To Max"</span>].default_value = rough + rough_var
nt.links.new(tex.outputs[<span class="s">"Fac"</span>], mr.inputs[<span class="s">"Value"</span>])
nt.links.new(mr.outputs[<span class="s">"Result"</span>], b.inputs[<span class="s">"Roughness"</span>])
<span class="k">return</span> mat
<span class="k">def</span> ghost_glass(name, tint):
<span class="s">"""Frosted, faintly tinted glass for an operand volume the result lost."""</span>
mat = bpy.data.materials.new(name)
mat.use_nodes = <span class="k">True</span>
b = mat.node_tree.nodes[<span class="s">"Principled BSDF"</span>]
b.inputs[<span class="s">"Base Color"</span>].default_value = (*tint, <span class="n">1.0</span>)
b.inputs[<span class="s">"Roughness"</span>].default_value = <span class="n">0.22</span>
b.inputs[<span class="s">"IOR"</span>].default_value = <span class="n">1.45</span>
b.inputs[<span class="s">"Transmission Weight"</span>].default_value = <span class="n">1.0</span>
<span class="k">return</span> mat
<span class="k">def</span> walnut_material():
<span class="s">"""Walnut with grain running along the plinth (world X): distorted wave
bands mixed between two walnut tones, a soft blotch layer, a light bump,
and a satin coat that does not mirror the scene."""</span>
mat = bpy.data.materials.new(<span class="s">"Walnut"</span>)
mat.use_nodes = <span class="k">True</span>
nt = mat.node_tree
b = nt.nodes[<span class="s">"Principled BSDF"</span>]
coord = nt.nodes.new(<span class="s">"ShaderNodeTexCoord"</span>)
mapn = nt.nodes.new(<span class="s">"ShaderNodeMapping"</span>)
mapn.inputs[<span class="s">"Scale"</span>].default_value = (<span class="n">0.35</span>, <span class="n">2.4</span>, <span class="n">1.0</span>)
nt.links.new(coord.outputs[<span class="s">"Object"</span>], mapn.inputs[<span class="s">"Vector"</span>])
wave = nt.nodes.new(<span class="s">"ShaderNodeTexWave"</span>)
wave.wave_type = <span class="s">'BANDS'</span>
wave.bands_direction = <span class="s">'Y'</span>
wave.inputs[<span class="s">"Scale"</span>].default_value = <span class="n">2.2</span>
wave.inputs[<span class="s">"Distortion"</span>].default_value = <span class="n">7.0</span>
wave.inputs[<span class="s">"Detail"</span>].default_value = <span class="n">3.0</span>
wave.inputs[<span class="s">"Detail Scale"</span>].default_value = <span class="n">1.4</span>
nt.links.new(mapn.outputs[<span class="s">"Vector"</span>], wave.inputs[<span class="s">"Vector"</span>])
ramp = nt.nodes.new(<span class="s">"ShaderNodeValToRGB"</span>)
ramp.color_ramp.elements[<span class="n">0</span>].position = <span class="n">0.25</span>
ramp.color_ramp.elements[<span class="n">0</span>].color = (<span class="n">0.085</span>, <span class="n">0.040</span>, <span class="n">0.020</span>, <span class="n">1.0</span>)
ramp.color_ramp.elements[<span class="n">1</span>].position = <span class="n">0.85</span>
ramp.color_ramp.elements[<span class="n">1</span>].color = (<span class="n">0.19</span>, <span class="n">0.095</span>, <span class="n">0.045</span>, <span class="n">1.0</span>)
nt.links.new(wave.outputs[<span class="s">"Fac"</span>], ramp.inputs[<span class="s">"Fac"</span>])
blot = nt.nodes.new(<span class="s">"ShaderNodeTexNoise"</span>)
blot.inputs[<span class="s">"Scale"</span>].default_value = <span class="n">1.6</span>
blot.inputs[<span class="s">"Detail"</span>].default_value = <span class="n">3.0</span>
nt.links.new(coord.outputs[<span class="s">"Object"</span>], blot.inputs[<span class="s">"Vector"</span>])
mix = nt.nodes.new(<span class="s">"ShaderNodeMix"</span>)
mix.data_type = <span class="s">'RGBA'</span>
mix.blend_type = <span class="s">'MULTIPLY'</span>
mix.inputs[<span class="s">"Factor"</span>].default_value = <span class="n">0.35</span>
nt.links.new(ramp.outputs[<span class="s">"Color"</span>], mix.inputs[<span class="s">"A"</span>])
nt.links.new(blot.outputs[<span class="s">"Color"</span>], mix.inputs[<span class="s">"B"</span>])
nt.links.new(mix.outputs[<span class="s">"Result"</span>], b.inputs[<span class="s">"Base Color"</span>])
bump = nt.nodes.new(<span class="s">"ShaderNodeBump"</span>)
bump.inputs[<span class="s">"Strength"</span>].default_value = <span class="n">0.04</span>
nt.links.new(wave.outputs[<span class="s">"Fac"</span>], bump.inputs[<span class="s">"Height"</span>])
nt.links.new(bump.outputs[<span class="s">"Normal"</span>], b.inputs[<span class="s">"Normal"</span>])
b.inputs[<span class="s">"Roughness"</span>].default_value = <span class="n">0.48</span>
b.inputs[<span class="s">"Coat Weight"</span>].default_value = <span class="n">0.2</span>
b.inputs[<span class="s">"Coat Roughness"</span>].default_value = <span class="n">0.32</span>
<span class="k">return</span> mat
<span class="k">def</span> tube_mesh(name, segments, radius, sides=<span class="n">10</span>):
<span class="s">"""Render-only rods along line segments (the operand outlines)."""</span>
me = bpy.data.meshes.new(name)
bm = bmesh.new()
<span class="k">try</span>:
<span class="k">for</span> a, b <span class="k">in</span> segments:
axis = b - a
res = bmesh.ops.create_cone(bm, cap_ends=<span class="k">True</span>, segments=sides,
radius1=radius, radius2=radius, depth=axis.length)
rot = Vector((<span class="n">0</span>, <span class="n">0</span>, <span class="n">1</span>)).rotation_difference(axis.normalized()).to_matrix()
mid = (a + b) / <span class="n">2</span>
<span class="k">for</span> vert <span class="k">in</span> res[<span class="s">"verts"</span>]:
vert.co = rot @ vert.co + mid
bmesh.ops.recalc_face_normals(bm, faces=bm.faces)
bm.to_mesh(me)
<span class="k">finally</span>:
bm.free()
<span class="k">for</span> p <span class="k">in</span> me.polygons:
p.use_smooth = <span class="k">True</span>
<span class="k">return</span> me
<span class="k">def</span> box_edges(lo, hi, mw):
c = {(i, j, k): mw @ Vector((hi[<span class="n">0</span>] <span class="k">if</span> i <span class="k">else</span> lo[<span class="n">0</span>], hi[<span class="n">1</span>] <span class="k">if</span> j <span class="k">else</span> lo[<span class="n">1</span>], hi[<span class="n">2</span>] <span class="k">if</span> k <span class="k">else</span> lo[<span class="n">2</span>]))
<span class="k">for</span> i <span class="k">in</span> (<span class="n">0</span>, <span class="n">1</span>) <span class="k">for</span> j <span class="k">in</span> (<span class="n">0</span>, <span class="n">1</span>) <span class="k">for</span> k <span class="k">in</span> (<span class="n">0</span>, <span class="n">1</span>)}
edges = []
<span class="k">for</span> i <span class="k">in</span> (<span class="n">0</span>, <span class="n">1</span>):
<span class="k">for</span> j <span class="k">in</span> (<span class="n">0</span>, <span class="n">1</span>):
edges.append((c[(i, j, <span class="n">0</span>)], c[(i, j, <span class="n">1</span>)]))
edges.append((c[(i, <span class="n">0</span>, j)], c[(i, <span class="n">1</span>, j)]))
edges.append((c[(<span class="n">0</span>, i, j)], c[(<span class="n">1</span>, i, j)]))
<span class="k">return</span> edges
<span class="k">def</span> grown(lo, hi, pad):
<span class="k">return</span> tuple(v - pad <span class="k">for</span> v <span class="k">in</span> lo), tuple(v + pad <span class="k">for</span> v <span class="k">in</span> hi)
<span class="c"># Which operand volumes each result no longer contains: those are drawn as</span>
<span class="c"># frosted ghosts so the eye can see what the solver removed.</span>
GHOSTS = {<span class="s">'UNION'</span>: (), <span class="s">'DIFFERENCE'</span>: (<span class="s">'B'</span>,), <span class="s">'INTERSECT'</span>: (<span class="s">'A'</span>, <span class="s">'B'</span>)}
<span class="c"># Outline rods: B everywhere (the coplanar cutter); A only where A is a ghost.</span>
OUTLINES = {<span class="s">'UNION'</span>: (<span class="s">'B'</span>,), <span class="s">'DIFFERENCE'</span>: (<span class="s">'B'</span>,), <span class="s">'INTERSECT'</span>: (<span class="s">'A'</span>, <span class="s">'B'</span>)}
LABELS = {<span class="s">'UNION'</span>: <span class="s">"UNION"</span>, <span class="s">'DIFFERENCE'</span>: <span class="s">"DIFFERENCE"</span>, <span class="s">'INTERSECT'</span>: <span class="s">"INTERSECT"</span>}
<span class="k">def</span> render_still(results, cutters, path, engine):
scene = bpy.context.scene
finishes = {
<span class="c"># glazed ceramic, machined bronze, glazed ceramic: three readable hues</span>
<span class="s">'UNION'</span>: principled(<span class="s">"UnionGlaze"</span>, (<span class="n">0.05</span>, <span class="n">0.29</span>, <span class="n">0.32</span>), <span class="n">0.32</span>, coat=<span class="n">0.6</span>, coat_rough=<span class="n">0.08</span>,
rough_var=<span class="n">0.06</span>),
<span class="s">'DIFFERENCE'</span>: principled(<span class="s">"DifferenceBronze"</span>, (<span class="n">0.78</span>, <span class="n">0.52</span>, <span class="n">0.27</span>), <span class="n">0.30</span>, metal=<span class="n">1.0</span>,
rough_var=<span class="n">0.07</span>),
<span class="s">'INTERSECT'</span>: principled(<span class="s">"IntersectGlaze"</span>, (<span class="n">0.90</span>, <span class="n">0.31</span>, <span class="n">0.05</span>), <span class="n">0.32</span>, coat=<span class="n">0.6</span>,
coat_rough=<span class="n">0.08</span>, rough_var=<span class="n">0.06</span>),
}
steel = principled(<span class="s">"OperandASteel"</span>, (<span class="n">0.70</span>, <span class="n">0.72</span>, <span class="n">0.75</span>), <span class="n">0.24</span>, metal=<span class="n">1.0</span>)
amber = principled(<span class="s">"OperandBAmber"</span>, (<span class="n">0.95</span>, <span class="n">0.47</span>, <span class="n">0.10</span>), <span class="n">0.30</span>, metal=<span class="n">0.6</span>)
ghost = {<span class="s">'A'</span>: ghost_glass(<span class="s">"GhostA"</span>, (<span class="n">0.86</span>, <span class="n">0.90</span>, <span class="n">0.95</span>)),
<span class="s">'B'</span>: ghost_glass(<span class="s">"GhostB"</span>, (<span class="n">1.0</span>, <span class="n">0.80</span>, <span class="n">0.55</span>))}
brass = principled(<span class="s">"PlaqueBrass"</span>, (<span class="n">0.84</span>, <span class="n">0.62</span>, <span class="n">0.32</span>), <span class="n">0.40</span>, metal=<span class="n">1.0</span>, rough_var=<span class="n">0.05</span>)
ink = principled(<span class="s">"PlaqueInk"</span>, (<span class="n">0.012</span>, <span class="n">0.010</span>, <span class="n">0.008</span>), <span class="n">0.6</span>)
walnut = walnut_material()
parts = []
<span class="k">def</span> add(name, me, mat, mw=<span class="k">None</span>):
me.materials.append(mat)
ob = bpy.data.objects.new(name, me)
<span class="k">if</span> mw <span class="k">is</span> <span class="k">not</span> <span class="k">None</span>:
ob.matrix_world = mw
scene.collection.objects.link(ob)
parts.append(ob)
<span class="k">return</span> ob
<span class="c"># Display copies: the checked results, frozen, with a 12 mm machined</span>
<span class="c"># chamfer. The modifier objects themselves (already measured) are hidden</span>
<span class="c"># from the render, so the chamfer never touches the volume check.</span>
deps = bpy.context.evaluated_depsgraph_get()
<span class="k">for</span> i, op <span class="k">in</span> enumerate(OPS):
res = results[op]
shown = bpy.data.meshes.new_from_object(res.evaluated_get(deps))
<span class="c"># the boolean output carries the operands' (empty) material slots;</span>
<span class="c"># start clean so every face takes the finish below</span>
shown.materials.clear()
<span class="k">for</span> poly <span class="k">in</span> shown.polygons:
poly.material_index = <span class="n">0</span>
ob = add(<span class="s">f"</span>{op.title()}<span class="s">Shown</span><span class="s">"</span>, shown, finishes[op], res.matrix_world.copy())
bev = ob.modifiers.new(<span class="s">"MachinedChamfer"</span>, <span class="s">'BEVEL'</span>)
bev.width = <span class="n">0.012</span>
bev.segments = <span class="n">3</span>
bev.limit_method = <span class="s">'ANGLE'</span>
bev.harden_normals = <span class="k">True</span>
res.hide_render = <span class="k">True</span>
mw = frame(i)
boxes = {<span class="s">'A'</span>: (A_LO, A_HI), <span class="s">'B'</span>: (B_LO, B_HI)}
<span class="k">for</span> key <span class="k">in</span> GHOSTS[op]:
<span class="c"># grown 2 / 4 mm so no ghost face is coplanar with the result or</span>
<span class="c"># with the other ghost (their top faces share z = 2)</span>
lo, hi = grown(*boxes[key], <span class="n">0.002</span> <span class="k">if</span> key == <span class="s">'A'</span> <span class="k">else</span> <span class="n">0.004</span>)
add(<span class="s">f"</span>{op.title()}<span class="s">Ghost</span>{key}<span class="s">"</span>, box_mesh(<span class="s">f"</span>{op.title()}<span class="s">Ghost</span>{key}<span class="s">"</span>, lo, hi),
ghost[key], mw)
<span class="k">for</span> key <span class="k">in</span> OUTLINES[op]: