-
Notifications
You must be signed in to change notification settings - Fork 55
/
operator.py
759 lines (672 loc) · 34.6 KB
/
operator.py
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
# -*- coding: utf-8 -*-
# operator.py: interaction with Blender and the user
from re import compile as re_compile
from math import pi, ceil
import bpy
import bl_operators
import bmesh
import mathutils
from .unfolder import Unfolder, UnfoldError, default_priority_effect
from .svg import Svg
from .pdf import Pdf
global_paper_sizes = [
('USER', "User defined", "User defined paper size"),
('A4', "A4", "International standard paper size"),
('A3', "A3", "International standard paper size"),
('US_LETTER', "Letter", "North American paper size"),
('US_LEGAL', "Legal", "North American paper size")
]
def first_letters(text):
"""Iterator over the first letter of each word"""
for match in first_letters.pattern.finditer(text):
yield text[match.start()]
first_letters.pattern = re_compile("((?<!\w)\w)|\d")
def init_exporter(self, properties):
self.page_size = mathutils.Vector((properties.output_size_x, properties.output_size_y))
self.style = properties.style
margin = properties.output_margin
self.margin = mathutils.Vector((margin, margin))
self.pure_net = (properties.texture_type == 'NONE')
self.do_create_stickers = properties.do_create_stickers
self.text_size = properties.sticker_width
self.angle_epsilon = properties.angle_epsilon
class Unfold(bpy.types.Operator):
"""Blender Operator: unfold the selected object."""
bl_idname = "mesh.unfold"
bl_label = "Unfold"
bl_description = "Mark seams so that the mesh can be exported as a paper model"
bl_options = {'REGISTER', 'UNDO'}
edit: bpy.props.BoolProperty(default=False, options={'HIDDEN'})
priority_effect_convex: bpy.props.FloatProperty(
name="Priority Convex", description="Priority effect for edges in convex angles",
default=default_priority_effect['CONVEX'], soft_min=-1, soft_max=10, subtype='FACTOR')
priority_effect_concave: bpy.props.FloatProperty(
name="Priority Concave", description="Priority effect for edges in concave angles",
default=default_priority_effect['CONCAVE'], soft_min=-1, soft_max=10, subtype='FACTOR')
priority_effect_length: bpy.props.FloatProperty(
name="Priority Length", description="Priority effect of edge length",
default=default_priority_effect['LENGTH'], soft_min=-10, soft_max=1, subtype='FACTOR')
do_create_uvmap: bpy.props.BoolProperty(
name="Create UVMap", description="Create a new UV Map showing the islands and page layout", default=False)
object = None
@classmethod
def poll(cls, context):
return context.active_object and context.active_object.type == "MESH"
def draw(self, context):
layout = self.layout
col = layout.column()
col.active = not self.object or len(self.object.data.uv_layers) < 8
col.prop(self.properties, "do_create_uvmap")
layout.label(text="Edge Cutting Factors:")
col = layout.column(align=True)
col.label(text="Face Angle:")
col.prop(self.properties, "priority_effect_convex", text="Convex")
col.prop(self.properties, "priority_effect_concave", text="Concave")
layout.prop(self.properties, "priority_effect_length", text="Edge Length")
def execute(self, context):
sce = bpy.context.scene
settings = sce.paper_model
recall_mode = context.object.mode
bpy.ops.object.mode_set(mode='EDIT')
self.object = context.object
cage_size = mathutils.Vector((settings.output_size_x, settings.output_size_y))
priority_effect = {
'CONVEX': self.priority_effect_convex,
'CONCAVE': self.priority_effect_concave,
'LENGTH': self.priority_effect_length}
try:
unfolder = Unfolder(self.object)
unfolder.do_create_uvmap = self.do_create_uvmap
scale = sce.unit_settings.scale_length / settings.scale
unfolder.prepare(cage_size, priority_effect, scale, settings.limit_by_page)
unfolder.mesh.mark_cuts()
except UnfoldError as error:
self.report(type={'ERROR_INVALID_INPUT'}, message=error.args[0])
error.mesh_select()
del unfolder
bpy.ops.object.mode_set(mode=recall_mode)
return {'CANCELLED'}
mesh = self.object.data
mesh.update()
if mesh.paper_island_list:
unfolder.copy_island_names(mesh.paper_island_list)
island_list = mesh.paper_island_list
attributes = {item.label: (item.abbreviation, item.auto_label, item.auto_abbrev) for item in island_list}
island_list.clear() # remove previously defined islands
for island in unfolder.mesh.islands:
# add islands to UI list and set default descriptions
list_item = island_list.add()
# add faces' IDs to the island
for face in island.faces:
lface = list_item.faces.add()
lface.id = face.index
list_item["label"] = island.label
list_item["abbreviation"], list_item["auto_label"], list_item["auto_abbrev"] = attributes.get(
island.label,
(island.abbreviation, True, True))
island_item_changed(list_item, context)
mesh.paper_island_index = -1
del unfolder
bpy.ops.object.mode_set(mode=recall_mode)
return {'FINISHED'}
class ClearAllSeams(bpy.types.Operator):
"""Blender Operator: clear all seams of the active Mesh and all its unfold data"""
bl_idname = "mesh.clear_all_seams"
bl_label = "Clear All Seams"
bl_description = "Clear all the seams and unfolded islands of the active object"
@classmethod
def poll(cls, context):
return context.active_object and context.active_object.type == 'MESH'
def execute(self, context):
ob = context.active_object
mesh = ob.data
for edge in mesh.edges:
edge.use_seam = False
mesh.paper_island_list.clear()
return {'FINISHED'}
def page_size_preset_changed(self, context):
"""Update the actual document size to correct values"""
if hasattr(self, "limit_by_page") and not self.limit_by_page:
return
if self.page_size_preset == 'A4':
self.output_size_x = 0.210
self.output_size_y = 0.297
elif self.page_size_preset == 'A3':
self.output_size_x = 0.297
self.output_size_y = 0.420
elif self.page_size_preset == 'US_LETTER':
self.output_size_x = 0.216
self.output_size_y = 0.279
elif self.page_size_preset == 'US_LEGAL':
self.output_size_x = 0.216
self.output_size_y = 0.356
class PaperModelStyle(bpy.types.PropertyGroup):
line_styles = [
('SOLID', "Solid (----)", "Solid line"),
('DOT', "Dots (. . .)", "Dotted line"),
('DASH', "Short Dashes (- - -)", "Solid line"),
('LONGDASH', "Long Dashes (-- --)", "Solid line"),
('DASHDOT', "Dash-dotted (-- .)", "Solid line")
]
outer_color: bpy.props.FloatVectorProperty(
name="Outer Lines", description="Color of net outline",
default=(0.0, 0.0, 0.0, 1.0), min=0, max=1, subtype='COLOR', size=4)
outer_style: bpy.props.EnumProperty(
name="Outer Lines Drawing Style", description="Drawing style of net outline",
default='SOLID', items=line_styles)
line_width: bpy.props.FloatProperty(
name="Base Lines Thickness", description="Base thickness of net lines, each actual value is a multiple of this length",
default=1e-4, min=0, soft_max=5e-3, precision=5, step=1e-2, subtype="UNSIGNED", unit="LENGTH")
outer_width: bpy.props.FloatProperty(
name="Outer Lines Thickness", description="Relative thickness of net outline",
default=3, min=0, soft_max=10, precision=1, step=10, subtype='FACTOR')
use_outbg: bpy.props.BoolProperty(
name="Highlight Outer Lines", description="Add another line below every line to improve contrast",
default=True)
outbg_color: bpy.props.FloatVectorProperty(
name="Outer Highlight", description="Color of the highlight for outer lines",
default=(1.0, 1.0, 1.0, 1.0), min=0, max=1, subtype='COLOR', size=4)
outbg_width: bpy.props.FloatProperty(
name="Outer Highlight Thickness", description="Relative thickness of the highlighting lines",
default=5, min=0, soft_max=10, precision=1, step=10, subtype='FACTOR')
convex_color: bpy.props.FloatVectorProperty(
name="Inner Convex Lines", description="Color of lines to be folded to a convex angle",
default=(0.0, 0.0, 0.0, 1.0), min=0, max=1, subtype='COLOR', size=4)
convex_style: bpy.props.EnumProperty(
name="Convex Lines Drawing Style", description="Drawing style of lines to be folded to a convex angle",
default='DASH', items=line_styles)
convex_width: bpy.props.FloatProperty(
name="Convex Lines Thickness", description="Relative thickness of concave lines",
default=2, min=0, soft_max=10, precision=1, step=10, subtype='FACTOR')
concave_color: bpy.props.FloatVectorProperty(
name="Inner Concave Lines", description="Color of lines to be folded to a concave angle",
default=(0.0, 0.0, 0.0, 1.0), min=0, max=1, subtype='COLOR', size=4)
concave_style: bpy.props.EnumProperty(
name="Concave Lines Drawing Style", description="Drawing style of lines to be folded to a concave angle",
default='DASHDOT', items=line_styles)
concave_width: bpy.props.FloatProperty(
name="Concave Lines Thickness", description="Relative thickness of concave lines",
default=2, min=0, soft_max=10, precision=1, step=10, subtype='FACTOR')
freestyle_color: bpy.props.FloatVectorProperty(
name="Freestyle Edges", description="Color of lines marked as Freestyle Edge",
default=(0.0, 0.0, 0.0, 1.0), min=0, max=1, subtype='COLOR', size=4)
freestyle_style: bpy.props.EnumProperty(
name="Freestyle Edges Drawing Style", description="Drawing style of Freestyle Edges",
default='SOLID', items=line_styles)
freestyle_width: bpy.props.FloatProperty(
name="Freestyle Edges Thickness", description="Relative thickness of Freestyle edges",
default=2, min=0, soft_max=10, precision=1, step=10, subtype='FACTOR')
use_inbg: bpy.props.BoolProperty(
name="Highlight Inner Lines", description="Add another line below every line to improve contrast",
default=True)
inbg_color: bpy.props.FloatVectorProperty(
name="Inner Highlight", description="Color of the highlight for inner lines",
default=(1.0, 1.0, 1.0, 1.0), min=0, max=1, subtype='COLOR', size=4)
inbg_width: bpy.props.FloatProperty(
name="Inner Highlight Thickness", description="Relative thickness of the highlighting lines",
default=2, min=0, soft_max=10, precision=1, step=10, subtype='FACTOR')
sticker_color: bpy.props.FloatVectorProperty(
name="Tabs Fill", description="Fill color of sticking tabs",
default=(0.9, 0.9, 0.9, 1.0), min=0, max=1, subtype='COLOR', size=4)
text_color: bpy.props.FloatVectorProperty(
name="Text Color", description="Color of all text used in the document",
default=(0.0, 0.0, 0.0, 1.0), min=0, max=1, subtype='COLOR', size=4)
bpy.utils.register_class(PaperModelStyle)
class ExportPaperModel(bpy.types.Operator):
"""Blender Operator: save the selected object's net and optionally bake its texture"""
bl_idname = "export_mesh.paper_model"
bl_label = "Export Paper Model"
bl_description = "Export the selected object's net and optionally bake its texture"
bl_options = {'PRESET'}
filepath: bpy.props.StringProperty(
name="File Path", description="Target file to save the SVG", options={'SKIP_SAVE'})
filename: bpy.props.StringProperty(
name="File Name", description="Name of the file", options={'SKIP_SAVE'})
directory: bpy.props.StringProperty(
name="Directory", description="Directory of the file", options={'SKIP_SAVE'})
page_size_preset: bpy.props.EnumProperty(
name="Page Size", description="Size of the exported document",
default='A4', update=page_size_preset_changed, items=global_paper_sizes)
output_size_x: bpy.props.FloatProperty(
name="Page Width", description="Width of the exported document",
default=0.210, soft_min=0.105, soft_max=0.841, subtype="UNSIGNED", unit="LENGTH")
output_size_y: bpy.props.FloatProperty(
name="Page Height", description="Height of the exported document",
default=0.297, soft_min=0.148, soft_max=1.189, subtype="UNSIGNED", unit="LENGTH")
output_margin: bpy.props.FloatProperty(
name="Page Margin", description="Distance from page borders to the printable area",
default=0.005, min=0, soft_max=0.1, step=0.1, subtype="UNSIGNED", unit="LENGTH")
texture_type: bpy.props.EnumProperty(
name="Textures", description="Source of a texture for the model",
default='NONE', items=[
('NONE', "No Texture", "Export the net only"),
('TEXTURE', "From Materials", "Render the diffuse color and all painted textures"),
('AMBIENT_OCCLUSION', "Ambient Occlusion", "Render the Ambient Occlusion pass"),
('RENDER', "Full Render", "Render the material in actual scene illumination"),
('SELECTED_TO_ACTIVE', "Selected to Active", "Render all selected surrounding objects as a texture")
])
output_layers: bpy.props.EnumProperty(
name="Layers", description="Separation of drawings by meaning",
default='ONESIDE', items=[
('ONESIDE', "Single Sided", "All layers exported on top of each other"),
('TWOSIDE', "Two Sided", "Texture exported to odd pages, drawing to even pages"),
('SEPARATE', "Separate", "Texture and drawing exported as two separate documents"),
])
do_create_stickers: bpy.props.BoolProperty(
name="Create Tabs", description="Create gluing tabs around the net (useful for paper)",
default=True)
do_create_numbers: bpy.props.BoolProperty(
name="Create Numbers", description="Enumerate edges to make it clear which edges should be sticked together",
default=True)
sticker_width: bpy.props.FloatProperty(
name="Tabs and Text Size", description="Width of gluing tabs and their numbers",
default=0.005, soft_min=0, soft_max=0.05, step=0.1, subtype="UNSIGNED", unit="LENGTH")
angle_epsilon: bpy.props.FloatProperty(
name="Hidden Edge Angle", description="Folds with angle below this limit will not be drawn",
default=pi/360, min=0, soft_max=pi/4, step=0.01, subtype="ANGLE", unit="ROTATION")
output_dpi: bpy.props.FloatProperty(
name="Resolution (DPI)", description="Resolution of images in pixels per inch",
default=90, min=1, soft_min=30, soft_max=600, subtype="UNSIGNED")
bake_samples: bpy.props.IntProperty(
name="Samples", description="Number of samples to render for each pixel",
default=64, min=1, subtype="UNSIGNED")
file_format: bpy.props.EnumProperty(
name="Document Format", description="File format of the exported net",
default='PDF', items=[
('PDF', "PDF", "Adobe Portable Document Format 1.4"),
('SVG', "SVG", "W3C Scalable Vector Graphics"),
])
image_packing: bpy.props.EnumProperty(
name="Image Packing Method", description="Method of attaching baked image(s) to the SVG",
default='ISLAND_EMBED', items=[
('PAGE_LINK', "Single Linked", "Bake one image per page of output and save it separately"),
('ISLAND_LINK', "Linked", "Bake images separately for each island and save them in a directory"),
('ISLAND_EMBED', "Embedded", "Bake images separately for each island and embed them into the SVG")
])
nesting_method: bpy.props.EnumProperty(
name="Nesting Method", description="How to pack islands onto a page",
default='BOX', items=[
('BOX', "Bounding Box", "Built-in Blender method with bounding boxes"),
('CCOR', "Raster", "Cross-correlation of rasterized shapes"),
])
scale: bpy.props.FloatProperty(
name="Scale", description="Divisor of all dimensions when exporting",
default=1, soft_min=1.0, soft_max=100.0, subtype='FACTOR', precision=1)
do_create_uvmap: bpy.props.BoolProperty(
name="Create UVMap",
description="Create a new UV Map showing the islands and page layout",
default=False, options={'SKIP_SAVE'})
ui_expanded_document: bpy.props.BoolProperty(
name="Show Document Settings Expanded",
description="Shows the box 'Document Settings' expanded in user interface",
default=True, options={'SKIP_SAVE'})
ui_expanded_style: bpy.props.BoolProperty(
name="Show Style Settings Expanded",
description="Shows the box 'Colors and Style' expanded in user interface",
default=False, options={'SKIP_SAVE'})
ui_expanded_development: bpy.props.BoolProperty(
name="Show Development Settings Expanded",
description="Shows the box 'Colors and Style' expanded in user interface",
default=False, options={'SKIP_SAVE'})
style: bpy.props.PointerProperty(type=PaperModelStyle)
unfolder = None
@classmethod
def poll(cls, context):
return context.active_object and context.active_object.type == 'MESH'
def prepare(self, context):
sce = context.scene
self.recall_mode = context.object.mode
bpy.ops.object.mode_set(mode='EDIT')
self.object = context.active_object
self.unfolder = Unfolder(self.object)
cage_size = mathutils.Vector((sce.paper_model.output_size_x, sce.paper_model.output_size_y))
unfolder_scale = sce.unit_settings.scale_length/self.scale
self.unfolder.prepare(cage_size, scale=unfolder_scale, limit_by_page=sce.paper_model.limit_by_page)
if sce.paper_model.use_auto_scale:
self.scale = ceil(self.get_scale_ratio(sce))
def recall(self):
self.unfolder = None
bpy.ops.object.mode_set(mode=self.recall_mode)
def invoke(self, context, event):
self.scale = context.scene.paper_model.scale
try:
self.prepare(context)
except UnfoldError as error:
self.report(type={'ERROR_INVALID_INPUT'}, message=error.args[0])
error.mesh_select()
self.recall()
return {'CANCELLED'}
wm = context.window_manager
self.filename = f"{self.object.name}"
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
try:
if not self.unfolder:
self.prepare(context)
self.unfolder.do_create_uvmap = self.do_create_uvmap
if self.object.data.paper_island_list:
self.unfolder.copy_island_names(self.object.data.paper_island_list)
exporter_class = Svg if self.properties.file_format == 'SVG' else Pdf
exporter = exporter_class(self.properties)
# TODO make the constructor meaningful enough so that init_exporter is not necessary
init_exporter(exporter, self.properties)
self.unfolder.save(self.properties, exporter)
self.report({'INFO'}, "Saved a {}-page document".format(len(self.unfolder.mesh.pages)))
return {'FINISHED'}
except UnfoldError as error:
self.report(type={'ERROR_INVALID_INPUT'}, message=error.args[0])
return {'CANCELLED'}
finally:
self.recall()
self.unfolder = None
def get_scale_ratio(self, sce):
margin = self.output_margin + self.sticker_width
if min(self.output_size_x, self.output_size_y) <= 2 * margin:
return False
output_inner_size = mathutils.Vector((self.output_size_x - 2*margin, self.output_size_y - 2*margin))
ratio = self.unfolder.mesh.largest_island_ratio(output_inner_size)
return ratio * sce.unit_settings.scale_length / self.scale
def draw(self, context):
layout = self.layout
layout.prop(self.properties, "do_create_uvmap")
layout.prop(self.properties, "scale", text="Scale: 1/")
scale_ratio = self.get_scale_ratio(context.scene)
if scale_ratio > 1:
layout.label(
text="An island is roughly {:.1f}x bigger than page".format(scale_ratio),
icon="ERROR")
elif scale_ratio > 0:
layout.label(text="Largest island is roughly 1/{:.1f} of page".format(1 / scale_ratio))
if context.scene.unit_settings.scale_length != 1:
layout.label(
text="Unit scale {:.1f} makes page size etc. not display correctly".format(
context.scene.unit_settings.scale_length), icon="ERROR")
box = layout.box()
row = box.row(align=True)
row.prop(
self.properties, "ui_expanded_document", text="",
icon=('TRIA_DOWN' if self.ui_expanded_document else 'TRIA_RIGHT'), emboss=False)
row.label(text="Document Settings")
if self.ui_expanded_document:
box.prop(self.properties, "file_format", text="Format")
box.prop(self.properties, "page_size_preset")
col = box.column(align=True)
col.active = self.page_size_preset == 'USER'
col.prop(self.properties, "output_size_x")
col.prop(self.properties, "output_size_y")
box.prop(self.properties, "output_margin")
col = box.column()
col.prop(self.properties, "do_create_stickers")
col.prop(self.properties, "do_create_numbers")
col = box.column()
col.active = self.do_create_stickers or self.do_create_numbers
col.prop(self.properties, "sticker_width")
box.prop(self.properties, "angle_epsilon")
box.prop(self.properties, "texture_type")
col = box.column()
col.active = (self.texture_type != 'NONE')
if len(self.object.data.uv_layers) >= 8:
col.label(text="No UV slots left, No Texture is the only option.", icon='ERROR')
elif context.scene.render.engine != 'CYCLES' and self.texture_type != 'NONE':
col.label(text="Cycles will be used for texture baking.", icon='ERROR')
row = col.row()
row.active = self.texture_type in ('AMBIENT_OCCLUSION', 'RENDER', 'SELECTED_TO_ACTIVE')
row.prop(self.properties, "bake_samples")
col.prop(self.properties, "output_dpi")
row = col.row()
row.active = self.file_format == 'SVG'
row.prop(self.properties, "image_packing", text="Images")
box = layout.box()
row = box.row(align=True)
row.prop(
self.properties, "ui_expanded_style", text="",
icon=('TRIA_DOWN' if self.ui_expanded_style else 'TRIA_RIGHT'), emboss=False)
row.label(text="Colors and Style")
if self.ui_expanded_style:
box.prop(self.style, "line_width", text="Default line width")
col = box.column()
col.prop(self.style, "outer_color")
col.prop(self.style, "outer_width", text="Relative width")
col.prop(self.style, "outer_style", text="Style")
col = box.column()
col.active = self.texture_type != 'NONE'
col.prop(self.style, "use_outbg", text="Outer Lines Highlight:")
sub = col.column()
sub.active = self.texture_type != 'NONE' and self.style.use_outbg
sub.prop(self.style, "outbg_color", text="")
sub.prop(self.style, "outbg_width", text="Relative width")
col = box.column()
col.prop(self.style, "convex_color")
col.prop(self.style, "convex_width", text="Relative width")
col.prop(self.style, "convex_style", text="Style")
col = box.column()
col.prop(self.style, "concave_color")
col.prop(self.style, "concave_width", text="Relative width")
col.prop(self.style, "concave_style", text="Style")
col = box.column()
col.prop(self.style, "freestyle_color")
col.prop(self.style, "freestyle_width", text="Relative width")
col.prop(self.style, "freestyle_style", text="Style")
col = box.column()
col.active = self.texture_type != 'NONE'
col.prop(self.style, "use_inbg", text="Inner Lines Highlight:")
sub = col.column()
sub.active = self.texture_type != 'NONE' and self.style.use_inbg
sub.prop(self.style, "inbg_color", text="")
sub.prop(self.style, "inbg_width", text="Relative width")
col = box.column()
col.active = self.do_create_stickers
col.prop(self.style, "sticker_color")
box.prop(self.style, "text_color")
if 0:
box = layout.box()
row = box.row(align=True)
row.prop(
self.properties, "ui_expanded_development", text="",
icon=('TRIA_DOWN' if self.ui_expanded_development else 'TRIA_RIGHT'), emboss=False)
row.label(text="Development")
if self.ui_expanded_development:
box.prop(self.properties, "nesting_method", text="Format")
def menu_func_export(self, context):
self.layout.operator("export_mesh.paper_model", text="Paper Model (.pdf/.svg)")
def menu_func_unfold(self, context):
self.layout.operator("mesh.unfold", text="Unfold")
class SelectIsland(bpy.types.Operator):
"""Blender Operator: select all faces of the active island"""
bl_idname = "mesh.select_paper_island"
bl_label = "Select Island"
bl_description = "Select an island of the paper model net"
operation: bpy.props.EnumProperty(
name="Operation", description="Operation with the current selection",
default='ADD', items=[
('ADD', "Add", "Add to current selection"),
('REMOVE', "Remove", "Remove from selection"),
('REPLACE', "Replace", "Select only the ")
])
@classmethod
def poll(cls, context):
return context.active_object and context.active_object.type == 'MESH' and context.mode == 'EDIT_MESH'
def execute(self, context):
ob = context.active_object
me = ob.data
bm = bmesh.from_edit_mesh(me)
island = me.paper_island_list[me.paper_island_index]
faces = {face.id for face in island.faces}
edges = set()
verts = set()
if self.operation == 'REPLACE':
for face in bm.faces:
selected = face.index in faces
face.select = selected
if selected:
edges.update(face.edges)
verts.update(face.verts)
for edge in bm.edges:
edge.select = edge in edges
for vert in bm.verts:
vert.select = vert in verts
else:
selected = (self.operation == 'ADD')
for index in faces:
face = bm.faces[index]
face.select = selected
edges.update(face.edges)
verts.update(face.verts)
for edge in edges:
edge.select = any(face.select for face in edge.link_faces)
for vert in verts:
vert.select = any(edge.select for edge in vert.link_edges)
bmesh.update_edit_mesh(me, loop_triangles=False, destructive=False)
return {'FINISHED'}
class VIEW3D_PT_paper_model_tools(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Paper'
bl_label = "Unfold"
def draw(self, context):
layout = self.layout
sce = context.scene
obj = context.active_object
mesh = obj.data if obj and obj.type == 'MESH' else None
layout.operator("mesh.unfold")
if context.mode == 'EDIT_MESH':
row = layout.row(align=True)
row.operator("mesh.mark_seam", text="Mark Seam").clear = False
row.operator("mesh.mark_seam", text="Clear Seam").clear = True
else:
layout.operator("mesh.clear_all_seams")
class VIEW3D_PT_paper_model_settings(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Paper'
bl_label = "Export"
def draw(self, context):
layout = self.layout
sce = context.scene
obj = context.active_object
mesh = obj.data if obj and obj.type == 'MESH' else None
layout.operator("export_mesh.paper_model")
props = sce.paper_model
layout.prop(props, "use_auto_scale")
sub = layout.row()
sub.active = not props.use_auto_scale
sub.prop(props, "scale", text="Model Scale: 1/")
layout.prop(props, "limit_by_page")
col = layout.column()
col.active = props.limit_by_page
col.prop(props, "page_size_preset")
sub = col.column(align=True)
sub.active = props.page_size_preset == 'USER'
sub.prop(props, "output_size_x")
sub.prop(props, "output_size_y")
class DATA_PT_paper_model_islands(bpy.types.Panel):
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"
bl_label = "Paper Model Islands"
COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_EEVEE', 'BLENDER_WORKBENCH'}
def draw(self, context):
layout = self.layout
sce = context.scene
obj = context.active_object
mesh = obj.data if obj and obj.type == 'MESH' else None
layout.operator("mesh.unfold", icon='FILE_REFRESH')
if mesh and mesh.paper_island_list:
layout.label(
text="1 island:" if len(mesh.paper_island_list) == 1 else
"{} islands:".format(len(mesh.paper_island_list)))
layout.template_list(
'UI_UL_list', 'paper_model_island_list', mesh,
'paper_island_list', mesh, 'paper_island_index', rows=1, maxrows=5)
sub = layout.split(align=True)
sub.operator("mesh.select_paper_island", text="Select").operation = 'ADD'
sub.operator("mesh.select_paper_island", text="Deselect").operation = 'REMOVE'
sub.prop(sce.paper_model, "sync_island", icon='UV_SYNC_SELECT', toggle=True)
if mesh.paper_island_index >= 0:
list_item = mesh.paper_island_list[mesh.paper_island_index]
sub = layout.column(align=True)
sub.prop(list_item, "auto_label")
sub.prop(list_item, "label")
sub.prop(list_item, "auto_abbrev")
row = sub.row()
row.active = not list_item.auto_abbrev
row.prop(list_item, "abbreviation")
else:
layout.box().label(text="Not unfolded")
def label_changed(self, context):
"""The label of an island was changed"""
# accessing properties via [..] to avoid a recursive call after the update
self["auto_label"] = not self.label or self.label.isspace()
island_item_changed(self, context)
def island_item_changed(self, context):
"""The labelling of an island was changed"""
def increment(abbrev, collisions):
letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
while abbrev in collisions:
if len(abbrev) < 3:
abbrev += letters[0]
else:
abbrev = abbrev.rstrip(letters[-1])
abbrev = abbrev[:-1] + letters[letters.find(abbrev[-1]) + 1]
return abbrev
# accessing properties via [..] to avoid a recursive call after the update
island_list = context.active_object.data.paper_island_list
if self.auto_label:
self["label"] = "" # avoid self-conflict
number = 1
while any(item.label == "Island {}".format(number) for item in island_list):
number += 1
self["label"] = "Island {}".format(number)
if self.auto_abbrev:
self["abbreviation"] = "" # avoid self-conflict
abbrev = "".join(first_letters(self.label))[:3].upper()
self["abbreviation"] = increment(abbrev, {item.abbreviation for item in island_list})
elif len(self.abbreviation) > 3:
self["abbreviation"] = self.abbreviation[:3]
self.name = "[{}] {} ({} {})".format(
self.abbreviation, self.label, len(self.faces), "faces" if len(self.faces) > 1 else "face")
def island_index_changed(self, context):
"""The active island was changed"""
if context.scene.paper_model.sync_island and SelectIsland.poll(context):
bpy.ops.mesh.select_paper_island(operation='REPLACE')
class FaceList(bpy.types.PropertyGroup):
id: bpy.props.IntProperty(name="Face ID")
class IslandList(bpy.types.PropertyGroup):
faces: bpy.props.CollectionProperty(
name="Faces", description="Faces belonging to this island", type=FaceList)
label: bpy.props.StringProperty(
name="Label", description="Label on this island",
default="", update=label_changed)
abbreviation: bpy.props.StringProperty(
name="Abbreviation", description="Three-letter label to use when there is not enough space",
default="", update=island_item_changed)
auto_label: bpy.props.BoolProperty(
name="Auto Label", description="Generate the label automatically",
default=True, update=island_item_changed)
auto_abbrev: bpy.props.BoolProperty(
name="Auto Abbreviation", description="Generate the abbreviation automatically",
default=True, update=island_item_changed)
class PaperModelSettings(bpy.types.PropertyGroup):
sync_island: bpy.props.BoolProperty(
name="Sync", description="Keep faces of the active island selected",
default=False, update=island_index_changed)
limit_by_page: bpy.props.BoolProperty(
name="Limit Island Size", description="Do not create islands larger than given dimensions",
default=False, update=page_size_preset_changed)
page_size_preset: bpy.props.EnumProperty(
name="Page Size", description="Maximal size of an island",
default='A4', update=page_size_preset_changed, items=global_paper_sizes)
output_size_x: bpy.props.FloatProperty(
name="Width", description="Maximal width of an island",
default=0.2, soft_min=0.105, soft_max=0.841, subtype="UNSIGNED", unit="LENGTH")
output_size_y: bpy.props.FloatProperty(
name="Height", description="Maximal height of an island",
default=0.29, soft_min=0.148, soft_max=1.189, subtype="UNSIGNED", unit="LENGTH")
use_auto_scale: bpy.props.BoolProperty(
name="Automatic Scale", description="Scale the net automatically to fit on paper",
default=True)
scale: bpy.props.FloatProperty(
name="Scale", description="Divisor of all dimensions when exporting",
default=1, soft_min=1.0, soft_max=100.0, subtype='FACTOR', precision=1,
update=lambda settings, _: settings.__setattr__('use_auto_scale', False))