forked from CGCookie/retopoflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
4192 lines (3360 loc) · 160 KB
/
__init__.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
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
'''
Copyright (C) 2014 CG Cookie
http://cgcookie.com
Created by Jonathan Denning, Jonathan Williamson, and Patrick Moore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
bl_info = {
"name": "RetopoFlow",
"description": "A suite of dedicated retopology tools for Blender",
"author": "Jonathan Denning, Jonathan Williamson, Patrick Moore",
"version": (1, 0, 1),
"blender": (2, 7, 5),
"location": "View 3D > Tool Shelf",
"warning": "", # used for warning icon and text in addons panel
"wiki_url": "http://cgcookiemarkets.com/blender/all-products/retopoflow/?view=docs",
"tracker_url": "https://github.com/CGCookie/retopoflow/issues",
"category": "3D View"
}
# System imports
import os
import sys
import copy
import math
import random
import time
from math import sqrt
from mathutils import Vector, Matrix, Quaternion
from mathutils.geometry import intersect_line_plane, intersect_point_line
import itertools
# Blender imports
import bgl
import blf
import bmesh
import bpy
from bpy.props import EnumProperty, StringProperty, BoolProperty, IntProperty, FloatVectorProperty, FloatProperty
from bpy.types import Operator, AddonPreferences
from bpy_extras.view3d_utils import location_3d_to_region_2d, region_2d_to_vector_3d, region_2d_to_location_3d
global bversion
bversion = '%03d.%03d.%03d' % (bpy.app.version[0],bpy.app.version[1],bpy.app.version[2])
if bversion > '002.074.004':
import bpy.utils.previews
# Common imports
from .lib import common_utilities
from .lib import common_drawing
from .lib.common_utilities import get_object_length_scale, dprint, profiler, frange, selection_mouse, showErrorMessage
from .lib.common_classes import SketchBrush, TextBox
from . import key_maps
# Polystrip imports
from . import polystrips_utilities
from .polystrips import PolyStrips
from .polystrips_draw import draw_gedge_info
# Contour imports
from . import contour_utilities
from .contour_classes import ContourCutLine, ExistingVertList, CutLineManipulatorWidget, ContourCutSeries, ContourStatePreserver
# Create a class that contains all location information for addons
AL = common_utilities.AddonLocator()
#a place to store stokes for later
global contour_cache
contour_cache = {}
contour_undo_cache = []
#store any temporary triangulated objects
#store the bmesh to prevent recalcing bmesh
#each time :-)
global contour_mesh_cache
contour_mesh_cache = {}
# Used to store undo snapshots
polystrips_undo_cache = []
class RetopoFlowPreferences(AddonPreferences):
bl_idname = __name__
def update_theme(self, context):
print('theme updated to ' + str(theme))
# Theme definitions
theme = EnumProperty(
items=[
('blue', 'Blue', 'Blue color scheme'),
('green', 'Green', 'Green color scheme'),
('orange', 'Orange', 'Orange color scheme'),
],
name='theme',
default='blue'
)
def rgba_to_float(r, g, b, a):
return (r/255.0, g/255.0, b/255.0, a/255.0)
theme_colors_active = {
'blue': rgba_to_float(78, 207, 81, 255),
'green': rgba_to_float(26, 111, 255, 255),
'orange': rgba_to_float(207, 135, 78, 255)
}
theme_colors_selection = {
'blue': rgba_to_float(78, 207, 81, 255),
'green': rgba_to_float(26, 111, 255, 255),
'orange': rgba_to_float(207, 135, 78, 255)
}
theme_colors_mesh = {
'blue': rgba_to_float(26, 111, 255, 255),
'green': rgba_to_float(78, 207, 81, 255),
'orange': rgba_to_float(26, 111, 255, 255)
}
theme_colors_frozen = {
'blue': rgba_to_float(255, 255, 255, 255),
'green': rgba_to_float(255, 255, 255, 255),
'orange': rgba_to_float(255, 255, 255, 255)
}
theme_colors_warning = {
'blue': rgba_to_float(182, 31, 0, 125),
'green': rgba_to_float(182, 31, 0, 125),
'orange': rgba_to_float(182, 31, 0, 125)
}
# User settings
show_help = BoolProperty(
name='Show Help Box',
description='A help text box will float on 3d view',
default=True
)
help_def = BoolProperty(
name='Show Help at Start',
description='Check to have help expanded when starting operator',
default=False
)
show_segment_count = BoolProperty(
name='Show Selected Segment Count',
description='Show segment count on selection',
default=True
)
use_pressure = BoolProperty(
name='Use Pressure Sensitivity',
description='Adjust size of Polystrip with pressure of tablet pen',
default=False
)
# Tool settings
contour_panel_settings = BoolProperty(
name="Show Contour Settings",
description = "Show the Contour settings",
default=False,
)
polystrips_panel_settings = BoolProperty(
name="Show Polystrips Settings",
description = "Show the Polystrips settings",
default=False,
)
# System settings
quad_prev_radius = IntProperty(
name="Pixel Brush Radius",
description="Pixel brush size",
default=15,
)
show_edges = BoolProperty(
name="Show Span Edges",
description = "Display the extracted mesh edges. Usually only turned off for debugging",
default=True,
)
show_ring_edges = BoolProperty(
name="Show Ring Edges",
description = "Display the extracted mesh edges. Usually only turned off for debugging",
default=True,
)
draw_widget = BoolProperty(
name="Draw Widget",
description = "Turn display of widget on or off",
default=True,
)
show_axes = BoolProperty(
name = "show_axes",
description = "Show Cut Axes",
default = False)
show_experimental = BoolProperty(
name="Enable Experimental",
description = "Enable experimental features and functions that are still in development, useful for experimenting and likely to crash",
default=False,
)
vert_size = IntProperty(
name="Vertex Size",
default=4,
min = 1,
max = 10,
)
edge_thick = IntProperty(
name="Edge Thickness",
default=1,
min=1,
max=10,
)
#TODO Theme this out nicely :-)
widget_color = FloatVectorProperty(name="Widget Color", description="Choose Widget color", min=0, max=1, default=(0,0,1), subtype="COLOR")
widget_color2 = FloatVectorProperty(name="Widget Color", description="Choose Widget color", min=0, max=1, default=(1,0,0), subtype="COLOR")
widget_color3 = FloatVectorProperty(name="Widget Color", description="Choose Widget color", min=0, max=1, default=(0,1,0), subtype="COLOR")
widget_color4 = FloatVectorProperty(name="Widget Color", description="Choose Widget color", min=0, max=1, default=(0,0.2,.8), subtype="COLOR")
widget_color5 = FloatVectorProperty(name="Widget Color", description="Choose Widget color", min=0, max=1, default=(.9,.1,0), subtype="COLOR")
handle_size = IntProperty(
name="Handle Vertex Size",
default=8,
min = 1,
max = 10,
)
line_thick = IntProperty(
name="Line Thickness",
default=1,
min = 1,
max = 10,
)
stroke_thick = IntProperty(
name="Stroke Thickness",
description = "Width of stroke lines drawn by user",
default=1,
min = 1,
max = 10,
)
auto_align = BoolProperty(
name="Automatically Align Vertices",
description = "Attempt to automatically align vertices in adjoining edgeloops. Improves outcome, but slows performance",
default=True,
)
live_update = BoolProperty(
name="Live Update",
description = "Will live update the mesh preview when transforming cut lines. Looks good, but can get slow on large meshes",
default=True,
)
use_x_ray = BoolProperty(
name="X-Ray",
description = 'Enable X-Ray on Retopo-mesh upon creation',
default=False,
)
use_perspective = BoolProperty(
name="Use Perspective",
description = 'Make non parallel cuts project from the same view to improve expected outcome',
default=True,
)
widget_radius = IntProperty(
name="Widget Radius",
description = "Size of cutline widget radius",
default=25,
min = 20,
max = 100,
)
widget_radius_inner = IntProperty(
name="Widget Inner Radius",
description = "Size of cutline widget inner radius",
default=10,
min = 5,
max = 30,
)
widget_thickness = IntProperty(
name="Widget Line Thickness",
description = "Width of lines used to draw widget",
default=2,
min = 1,
max = 10,
)
widget_thickness2 = IntProperty(
name="Widget 2nd Line Thick",
description = "Width of lines used to draw widget",
default=4,
min = 1,
max = 10,
)
arrow_size = IntProperty(
name="Arrow Size",
default=12,
min=5,
max=50,
)
arrow_size2 = IntProperty(
name="Translate Arrow Size",
default=10,
min=5,
max=50,
)
vertex_count = IntProperty(
name = "Vertex Count",
description = "The Number of Vertices Per Edge Ring",
default=10,
min = 3,
max = 250,
)
ring_count = IntProperty(
name="Ring Count",
description="The Number of Segments Per Guide Stroke",
default=10,
min=3,
max=100,
)
cyclic = BoolProperty(
name = "Cyclic",
description = "Make contour loops cyclic",
default = False)
recover = BoolProperty(
name = "Recover",
description = "Recover strokes from last session",
default = False)
recover_clip = IntProperty(
name = "Recover Clip",
description = "Number of cuts to leave out, usually set to 0 or 1",
default=1,
min = 0,
max = 10,
)
search_factor = FloatProperty(
name = "Search Factor",
description = "Factor of existing segment length to connect a new cut",
default=5,
min = 0,
max = 30,
)
intersect_threshold = FloatProperty(
name = "Intersect Factor",
description = "Stringence for connecting new strokes",
default=1.,
min = .000001,
max = 1,
)
merge_threshold = FloatProperty(
name = "Intersect Factor",
description = "Distance below which to snap strokes together",
default=1.,
min = .000001,
max = 1,
)
cull_factor = IntProperty(
name = "Cull Factor",
description = "Fraction of screen drawn points to throw away. Bigger = less detail",
default = 4,
min = 1,
max = 10,
)
smooth_factor = IntProperty(
name = "Smooth Factor",
description = "Number of iterations to smooth drawn strokes",
default = 5,
min = 1,
max = 10,
)
feature_factor = IntProperty(
name = "Smooth Factor",
description = "Fraction of sketch bounding box to be considered feature. Bigger = More Detail",
default = 4,
min = 1,
max = 20,
)
extend_radius = IntProperty(
name="Snap/Extend Radius",
default=20,
min=5,
max=100,
)
undo_depth = IntProperty(
name="Undo Depth",
description="Max number of undo steps",
min = 0,
max = 100,
default=15,
)
smooth_method = EnumProperty(
items=[
('ENDPOINT', 'ENDPOINT', 'Blend Between Endpoints'),
('CENTER_MASS', 'CENTER_MASS', 'Use Cut Locations to smooth'),
('PATH_NORMAL', 'PATH_NORMAL', 'Use Cut Orientation only'),
],
name='Smooth Method',
default='ENDPOINT'
)
## Debug Settings
show_debug = BoolProperty(
name="Show Debug Settings",
description = "Show the debug settings, useful for troubleshooting",
default=False,
)
debug = IntProperty(
name="Debug Level",
default=1,
min=0,
max=4,
)
raw_vert_size = IntProperty(
name="Raw Vertex Size",
default=1,
min = 1,
max = 10,
)
simple_vert_inds = BoolProperty(
name="Simple Inds",
default=False,
)
vert_inds = BoolProperty(
name="Vert Inds",
description = "Display indices of the raw contour verts",
default=False,
)
show_backbone = BoolProperty(
name = "show_backbone",
description = "Show Cut Series Backbone",
default = False)
show_nodes = BoolProperty(
name = "show_nodes",
description = "Show Cut Nodes",
default = False)
show_ring_inds = BoolProperty(
name = "show_ring_inds",
description = "Show Ring Indices",
default = False)
show_verts = BoolProperty(
name="Show Raw Verts",
description = "Display the raw contour verts",
default=False,
)
show_cut_indices = BoolProperty(
name="Show Cut Indices",
description = "Display the order the operator stores cuts. Usually only turned on for debugging",
default=False,
)
new_method = BoolProperty(
name="New Method",
description = "Use robust cutting, may be slower, more accurate on dense meshes",
default=True,
)
distraction_free = BoolProperty(
name = "distraction_free",
description = "Switch to distraction-free mode",
default = False,
)
symmetry_plane = EnumProperty(
items=[
('none', 'None', 'Disable symmetry plane'),
('x', 'X', 'Clip to X-axis (YZ plane)'),
# ('y', 'Y', 'Clip to Y-axis (XZ plane)'),
# ('z', 'Z', 'Clip to Z-axis (XY plane)'),
],
name='Symmetry Plane',
description = "Clamp and clip to symmetry plane",
default='none'
)
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.prop(self, "theme", "Theme")
row.prop(self,"show_help")
row.prop(self,"help_def")
## Polystrips
row = layout.row(align=True)
row.label("POLYSTRIPS SETTINGS:")
row = layout.row(align=True)
row.prop(self, "use_pressure")
row.prop(self, "show_segment_count")
##Contours
row = layout.row(align=True)
row.label("CONTOURS SETTINGS:")
# Interaction Settings
row = layout.row(align=True)
row.prop(self, "use_x_ray", "Enable X-Ray at Mesh Creation")
row.prop(self, "smooth_method", text="Smoothing Method")
# Widget Settings
row = layout.row()
row.prop(self,"draw_widget", text="Display Widget")
## Debug Settings
box = layout.box().column(align=False)
row = box.row()
row.label(text="Debug Settings")
row = box.row()
row.prop(self, "show_debug", text="Show Debug Settings")
if self.show_debug:
row = box.row()
row.prop(self, "new_method")
row.prop(self, "debug")
row = box.row()
row.prop(self, "vert_inds", text="Show Vertex Indices")
row.prop(self, "simple_vert_inds", text="Show Simple Indices")
row = box.row()
row.prop(self, "show_verts", text="Show Raw Vertices")
row.prop(self, "raw_vert_size")
row = box.row()
row.prop(self, "show_backbone", text="Show Backbone")
row.prop(self, "show_nodes", text="Show Cut Nodes")
row.prop(self, "show_ring_inds", text="Show Ring Indices")
class CGCOOKIE_OT_retopoflow_panel(bpy.types.Panel):
'''RetopoFlow Tools'''
bl_category = "Retopology"
bl_label = "RetopoFlow"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
@classmethod
def poll(cls, context):
mode = bpy.context.mode
obj = context.active_object
return (obj and obj.type == 'MESH' and mode in ('OBJECT', 'EDIT_MESH'))
def draw(self, context):
layout = self.layout
settings = common_utilities.get_settings()
col = layout.column(align=True)
if bversion > '002.074.004':
icons = icon_collections["main"]
contours_icon = icons.get("rf_contours_icon")
col.operator("cgcookie.contours", icon_value=contours_icon.icon_id)
else:
col.operator("cgcookie.contours", icon='IPO_LINEAR')
box = layout.box()
row = box.row()
row.prop(settings, "contour_panel_settings")
if settings.contour_panel_settings:
col = box.column()
col.prop(settings, "vertex_count")
col.label("Guide Mode:")
col.prop(settings, "ring_count")
col.label("Cache:")
col.prop(settings, "recover", text="Recover")
if settings.recover:
col.prop(settings, "recover_clip")
col.operator("cgcookie.contours_clear_cache", text = "Clear Cache", icon = 'CANCEL')
col = layout.column(align=True)
if bversion > '002.074.004':
polystrips_icon = icons.get("rf_polystrips_icon")
col.operator("cgcookie.polystrips", icon_value=polystrips_icon.icon_id)
else:
col.operator("cgcookie.polystrips", icon='IPO_BEZIER')
box = layout.box()
row = box.row()
row.prop(settings, "polystrips_panel_settings")
if settings.polystrips_panel_settings:
col = box.column()
col.prop(settings, "symmetry_plane", text ="Symmetry Plane")
class CGCOOKIE_OT_retopoflow_menu(bpy.types.Menu):
bl_label = "Retopology"
bl_space_type = 'VIEW_3D'
bl_idname = "object.retopology_menu"
def draw(self, context):
layout = self.layout
layout.operator_context = 'INVOKE_DEFAULT'
layout.operator("cgcookie.contours", icon="IPO_LINEAR")
layout.operator("cgcookie.polystrips", icon="IPO_BEZIER")
################### Contours ###################
def object_validation(ob):
me = ob.data
# get object data to act as a hash
counts = (len(me.vertices), len(me.edges), len(me.polygons), len(ob.modifiers))
bbox = (tuple(min(v.co for v in me.vertices)), tuple(max(v.co for v in me.vertices)))
vsum = tuple(sum((v.co for v in me.vertices), Vector((0,0,0))))
return (ob.name, counts, bbox, vsum)
def is_object_valid(ob):
global contour_mesh_cache
if 'valid' not in contour_mesh_cache: return False
return contour_mesh_cache['valid'] == object_validation(ob)
def write_mesh_cache(orig_ob,tmp_ob, bme):
print('writing mesh cache')
global contour_mesh_cache
contour_mesh_cache['valid'] = object_validation(orig_ob)
contour_mesh_cache['bme'] = bme
contour_mesh_cache['tmp'] = tmp_ob
def clear_mesh_cache():
print('clearing mesh cache')
global contour_mesh_cache
if 'valid' in contour_mesh_cache and contour_mesh_cache['valid']:
del contour_mesh_cache['valid']
if 'bme' in contour_mesh_cache and contour_mesh_cache['bme']:
bme_old = contour_mesh_cache['bme']
bme_old.free()
del contour_mesh_cache['bme']
if 'tmp' in contour_mesh_cache and contour_mesh_cache['tmp']:
old_obj = contour_mesh_cache['tmp']
#context.scene.objects.unlink(self.tmp_ob)
old_me = old_obj.data
old_obj.user_clear()
if old_obj and old_obj.name in bpy.data.objects:
bpy.data.objects.remove(old_obj)
if old_me and old_me.name in bpy.data.meshes:
bpy.data.meshes.remove(old_me)
del contour_mesh_cache['tmp']
class CGCOOKIE_OT_contours_cache_clear(bpy.types.Operator):
'''Removes the temporary object and mesh data from the cache. Do this if you have altered your original form in any way'''
bl_idname = "cgcookie.contours_clear_cache"
bl_label = "Clear Contour Cache"
def execute(self,context):
clear_mesh_cache()
return {'FINISHED'}
class CGCOOKIE_OT_contours(bpy.types.Operator):
'''Draw Strokes Perpindicular to Cylindrical Forms to Retopologize Them'''
bl_idname = "cgcookie.contours"
bl_label = "Contours"
@classmethod
def poll(cls,context):
if context.mode not in {'EDIT_MESH','OBJECT'}:
return False
if context.active_object:
if context.mode == 'EDIT_MESH':
if len(context.selected_objects) > 1:
return True
else:
return False
else:
return context.object.type == 'MESH'
else:
return False
#####drawing#######
def draw_callback(self,context):
settings = common_utilities.get_settings()
r3d = context.space_data.region_3d
if context.space_data.use_occlude_geometry:
new_matrix = [v for l in r3d.view_matrix for v in l]
if new_matrix != self.last_matrix:
for path in self.cut_paths:
path.update_visibility(context, self.original_form)
for cut_line in path.cuts:
cut_line.update_visibility(context, self.original_form)
self.post_update = False
self.last_matrix = new_matrix
for i, c_cut in enumerate(self.cut_lines):
if self.widget_interaction and self.drag_target == c_cut:
interact = True
else:
interact = False
c_cut.draw(context, settings)#,three_dimensional = self.navigating, interacting = interact)
if c_cut.verts_simple != [] and settings.show_cut_indices:
loc = location_3d_to_region_2d(context.region, context.space_data.region_3d, c_cut.verts_simple[0])
blf.position(0, loc[0], loc[1], 0)
blf.draw(0, str(i))
if self.cut_line_widget and settings.draw_widget:
self.cut_line_widget.draw(context)
if len(self.sketch):
common_drawing.draw_polyline_from_points(context, self.sketch, self.snap_color, 2, "GL_LINE_SMOOTH")
if len(self.cut_paths):
for path in self.cut_paths:
path.draw(context, path = False, nodes = settings.show_nodes, rings = True, follows = True, backbone = settings.show_backbone )
if len(self.snap_circle):
common_drawing.draw_polyline_from_points(context, self.snap_circle, self.snap_color, 2, "GL_LINE_SMOOTH")
if settings.show_help:
self.help_box.draw()
####Blender Mesh Data Management####
def new_destination_obj(self,context,name, mx):
'''
creates new object for mesh data to enter
'''
dest_me = bpy.data.meshes.new(name)
dest_ob = bpy.data.objects.new(name,dest_me) #this is an empty currently
dest_ob.matrix_world = mx
dest_ob.update_tag()
dest_bme = bmesh.new()
dest_bme.from_mesh(dest_me)
return dest_ob, dest_me, dest_bme
def tmp_obj_and_triangulate(self,context, bme, ngons, mx):
'''
ob - input object
bme - bmesh extracted from input object <- this will be modified by triangulation
ngons - list of bmesh faces that are ngons
'''
if len(ngons):
new_geom = bmesh.ops.triangulate(bme, faces = ngons, quad_method=0, ngon_method=1)
new_faces = new_geom['faces']
new_me = bpy.data.meshes.new('tmp_recontour_mesh')
bme.to_mesh(new_me)
new_me.update()
tmp_ob = bpy.data.objects.new('ContourTMP', new_me)
#ob must be linked to scene for ray casting?
context.scene.objects.link(tmp_ob)
tmp_ob.update_tag()
context.scene.update()
#however it can be unlinked to prevent user from seeing it?
context.scene.objects.unlink(tmp_ob)
tmp_ob.matrix_world = mx
return tmp_ob
def mesh_data_gather_object_mode(self,context):
'''
get references to object and object data
'''
self.sel_edge = None
self.sel_verts = None
self.existing_cut = None
ob = context.object
tmp_ob = None
name = ob.name + '_recontour'
self.dest_ob, self.dest_me, self.dest_bme = self.new_destination_obj(context, name, ob.matrix_world)
is_valid = is_object_valid(context.object)
has_tmp = 'ContourTMP' in bpy.data.objects and bpy.data.objects['ContourTMP'].data
if is_valid and has_tmp:
self.bme = contour_mesh_cache['bme']
tmp_ob = contour_mesh_cache['tmp']
else:
clear_mesh_cache()
me = ob.to_mesh(scene=context.scene, apply_modifiers=True, settings='PREVIEW')
me.update()
self.bme = bmesh.new()
self.bme.from_mesh(me)
ngons = [f for f in self.bme.faces if len(f.verts) > 4]
if len(ngons) or len(ob.modifiers) > 0:
tmp_ob= self.tmp_obj_and_triangulate(context, self.bme, ngons, ob.matrix_world)
if tmp_ob:
self.original_form = tmp_ob
else:
self.original_form = ob
if self.settings.recover and is_valid:
print('loading cache!')
self.undo_action()
return
else:
print('no recover or not valid or something')
global contour_undo_cache
contour_undo_cache = []
write_mesh_cache(ob,tmp_ob, self.bme)
def mesh_data_gather_edit_mode(self,context):
'''
get references to object and object data
'''
self.dest_ob = context.object
self.dest_me = self.dest_ob.data
self.dest_bme = bmesh.from_edit_mesh(self.dest_me)
ob = [obj for obj in context.selected_objects if obj.name != context.object.name][0]
is_valid = is_object_valid(ob)
tmp_ob = None
if is_valid:
self.bme = contour_mesh_cache['bme']
tmp_ob = contour_mesh_cache['tmp']
else:
clear_mesh_cache()
me = ob.to_mesh(scene=context.scene, apply_modifiers=True, settings='PREVIEW')
me.update()
self.bme = bmesh.new()
self.bme.from_mesh(me)
ngons = [f for f in self.bme.faces if len(f.verts) > 4]
if len(ngons) or len(ob.modifiers) > 0:
tmp_ob = self.tmp_obj_and_triangulate(context, self.bme, ngons, ob.matrix_world)
if tmp_ob:
print('Load form cache tmp obj, original form set')
self.original_form = tmp_ob
else:
print('Load new obj, original form set')
self.original_form = ob
self.tmp_ob = tmp_ob
if self.settings.recover and is_valid:
print('loading cache!')
self.undo_action()
return
else:
global contour_undo_cache
contour_undo_cache = []
#count and collect the selected edges if any
ed_inds = [ed.index for ed in self.dest_bme.edges if ed.select and len(ed.link_faces) < 2]
self.existing_loops = []
if len(ed_inds):
vert_loops = contour_utilities.edge_loops_from_bmedges(self.dest_bme, ed_inds)
if len(vert_loops) > 1:
self.report({'WARNING'}, 'Only one edge loop will be used for extension')
print('there are %i edge loops selected' % len(vert_loops))
#for loop in vert_loops:
#until multi loops are supported, do this
loop = vert_loops[0]
if loop[-1] != loop[0] and len(list(set(loop))) != len(loop):
self.report({'WARNING'},'Edge loop selection has extra parts! Excluding this loop')
else:
lverts = [self.dest_bme.verts[i] for i in loop]
existing_loop =ExistingVertList(context,
lverts,
loop,
self.dest_ob.matrix_world,
key_type = 'INDS')
#make a blank path with just an existing head
path = ContourCutSeries(context, [],
cull_factor = self.settings.cull_factor,
smooth_factor = self.settings.smooth_factor,
feature_factor = self.settings.feature_factor)
path.existing_head = existing_loop
path.seg_lock = False
path.ring_lock = True
path.ring_segments = len(existing_loop.verts_simple)
path.connect_cuts_to_make_mesh(ob)
path.update_visibility(context, ob)
#path.update_visibility(context, self.original_form)
self.cut_paths.append(path)
self.existing_loops.append(existing_loop)
write_mesh_cache(ob,tmp_ob, self.bme)
def finish_mesh(self, context):
back_to_edit = (context.mode == 'EDIT_MESH')
#This is where all the magic happens
print('pushing data into bmesh')
for path in self.cut_paths:
path.push_data_into_bmesh(context, self.dest_ob, self.dest_bme, self.original_form, self.dest_me)
if back_to_edit:
print('updating edit mesh')
bmesh.update_edit_mesh(self.dest_me, tessface=False, destructive=True)
else:
#write the data into the object
print('write data into the object')
self.dest_bme.to_mesh(self.dest_me)
#remember we created a new object
print('link destination object')
context.scene.objects.link(self.dest_ob)
print('select and make active')
self.dest_ob.select = True
context.scene.objects.active = self.dest_ob
if context.space_data.local_view:
view_loc = context.space_data.region_3d.view_location.copy()
view_rot = context.space_data.region_3d.view_rotation.copy()