This repository has been archived by the owner on Dec 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
__init__.py
1721 lines (1398 loc) · 66.5 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": "Polystrips Retopology Tool",
"description": "A tool to retopologize complex forms with drawn strips of polygons.",
"author": "Jonathan Denning, Jonathan Williamson, Patrick Moore",
"version": (1, 0, 2),
"blender": (2, 7, 1),
"location": "View 3D > Tool Shelf",
"warning": "", # used for warning icon and text in addons panel
"wiki_url": "http://cgcookiemarkets.com/blender/all-products/polystrips-retopology-tool/?view=docs",
"tracker_url": "https://github.com/CGCookie/retopology-polystrips/issues",
"category": "3D View"
}
# Add the current __file__ path to the search path
import sys
import os
sys.path.append(os.path.dirname(__file__))
import math
import copy
import random
import time
from math import sqrt
import bpy
import bmesh
import blf
import bgl
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
from mathutils import Vector, Matrix, Quaternion
from mathutils.geometry import intersect_line_plane, intersect_point_line
from lib import common_utilities
from lib.common_utilities import get_object_length_scale, dprint, profiler, frange
from lib.common_classes import SketchBrush
from lib import common_drawing
from polystrips import *
import polystrips_utilities
from polystrips_draw import *
# Used to store keymaps for addon
polystrips_keymaps = []
# Used to store undo snapshots
polystrips_undo_cache = []
class PolystripsToolsAddonPreferences(AddonPreferences):
bl_idname = __name__
def update_theme(self, context):
print('theme updated to ' + str(theme))
debug = IntProperty(
name="Debug Level",
default=1,
min=0,
max=4,
)
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_selection = {
'blue': rgba_to_float(105, 246, 113, 255),
'green': rgba_to_float(102, 165, 240, 255),
'orange': rgba_to_float(102, 165, 240, 255)
}
theme_colors_mesh = {
'blue': rgba_to_float(102, 165, 240, 255),
'green': rgba_to_float(105, 246, 113, 255),
'orange': rgba_to_float(254, 145, 0, 255)
}
show_segment_count = BoolProperty(
name='Show Selected Segment Count',
description='Show segment count on selection',
default=True
)
quad_prev_radius = IntProperty(
name="Pixel Brush Radius",
description="Pixel brush size",
default=15,
)
undo_depth = IntProperty(
name="Undo Depth",
description="Max number of undo steps",
default=15,
)
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.prop(self, "theme", "Theme")
row = layout.row(align=True)
row.prop(self, "show_segment_count")
row = layout.row(align=True)
row.prop(self, "debug")
class CGCOOKIE_OT_retopo_polystrips_panel(bpy.types.Panel):
'''Retopologize Forms with polygon strips'''
bl_category = "Retopology"
bl_label = "Polystrips"
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
col = layout.column(align=True)
if 'EDIT' in context.mode and len(context.selected_objects) != 2:
col.label(text='No 2nd Object!')
col.operator("cgcookie.polystrips", icon="IPO_BEZIER")
class CGCOOKIE_OT_polystrips(bpy.types.Operator):
bl_idname = "cgcookie.polystrips"
bl_label = "Polystrips"
@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
def draw_callback(self, context):
return self.ui.draw_callback(context)
def modal(self, context, event):
ret = self.ui.modal(context, event)
if 'FINISHED' in ret or 'CANCELLED' in ret:
self.ui.cleanup(context)
common_utilities.callback_cleanup(self, context)
return ret
def invoke(self, context, event):
self.ui = PolystripsUI(context, event)
# Switch to modal
self._handle = bpy.types.SpaceView3D.draw_handler_add(self.draw_callback, (context, ), 'WINDOW', 'POST_PIXEL')
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def register():
bpy.utils.register_class(CGCOOKIE_OT_polystrips)
bpy.utils.register_class(CGCOOKIE_OT_retopo_polystrips_panel)
bpy.utils.register_class(PolystripsToolsAddonPreferences)
def unregister():
bpy.utils.unregister_class(PolystripsToolsAddonPreferences)
bpy.utils.unregister_class(CGCOOKIE_OT_retopo_polystrips_panel)
bpy.utils.unregister_class(CGCOOKIE_OT_polystrips)
class PolystripsUI:
def __init__(self, context, event):
settings = common_utilities.get_settings()
self.mode = 'main'
self.mode_pos = (0, 0)
self.cur_pos = (0, 0)
self.mode_radius = 0
self.action_center = (0, 0)
self.action_radius = 0
self.is_navigating = False
self.sketch_curpos = (0, 0)
self.sketch_pressure = 1
self.sketch = []
self.post_update = True
self.footer = ''
self.footer_last = ''
self.last_matrix = None
self._timer = context.window_manager.event_timer_add(0.1, context.window)
self.stroke_smoothing = 0.75 # 0: no smoothing. 1: no change
if context.mode == 'OBJECT':
self.obj_orig = context.object
# duplicate selected objected to temporary object but with modifiers applied
self.me = self.obj_orig.to_mesh(scene=context.scene, apply_modifiers=True, settings='PREVIEW')
self.me.update()
self.obj = bpy.data.objects.new('PolystripsTmp', self.me)
bpy.context.scene.objects.link(self.obj)
self.obj.hide = True
self.obj.matrix_world = self.obj_orig.matrix_world
self.me.update()
# HACK
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.object.mode_set(mode='OBJECT')
self.bme = bmesh.new()
self.bme.from_mesh(self.me)
self.to_obj = None
self.to_bme = None
self.snap_eds = []
self.snap_eds_vis = []
self.hover_ed = None
if context.mode == 'EDIT_MESH':
self.obj_orig = [ob for ob in context.selected_objects if ob != context.object][0]
self.me = self.obj_orig.to_mesh(scene=context.scene, apply_modifiers=True, settings='PREVIEW')
self.me.update()
self.bme = bmesh.new()
self.bme.from_mesh(self.me)
self.obj = bpy.data.objects.new('PolystripsTmp', self.me)
bpy.context.scene.objects.link(self.obj)
self.obj.hide = True
self.obj.matrix_world = self.obj_orig.matrix_world
self.me.update()
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
self.to_obj = context.object
self.to_bme = bmesh.from_edit_mesh(context.object.data)
self.snap_eds = [ed for ed in self.to_bme.edges if not ed.is_manifold]
region, r3d = context.region, context.space_data.region_3d
mx = self.to_obj.matrix_world
rv3d = context.space_data.region_3d
self.snap_eds_vis = [False not in common_utilities.ray_cast_visible([mx * ed.verts[0].co, mx * ed.verts[1].co], self.obj, rv3d) for ed in self.snap_eds]
self.hover_ed = None
self.scale = self.obj.scale[0]
self.length_scale = get_object_length_scale(self.obj)
# World stroke radius
self.stroke_radius = 0.01 * self.length_scale
self.stroke_radius_pressure = 0.01 * self.length_scale
# Screen_stroke_radius
self.screen_stroke_radius = 20 # TODO, hood to settings
self.sketch_brush = SketchBrush(context,
settings,
event.mouse_region_x, event.mouse_region_y,
15, # settings.quad_prev_radius,
self.obj)
self.sel_gedge = None # selected gedge
self.sel_gvert = None # selected gvert
self.act_gvert = None # active gvert (operated upon)
self.polystrips = PolyStrips(context, self.obj)
polystrips_undo_cache = [] # Clear the cache in case any is left over
if self.obj.grease_pencil:
self.create_polystrips_from_greasepencil()
elif 'BezierCurve' in bpy.data.objects:
self.create_polystrips_from_bezier(bpy.data.objects['BezierCurve'])
context.area.header_text_set('PolyStrips')
###############################
def create_undo_snapshot(self, action):
'''
unsure about all the _timers get deep copied
and if sel_gedges and verts get copied as references
or also duplicated, making them no longer valid.
'''
settings = common_utilities.get_settings()
repeated_actions = {'count', 'zip count'}
if action in repeated_actions and len(polystrips_undo_cache):
if action == polystrips_undo_cache[-1][1]:
print('repeatable...dont take snapshot')
return
p_data = copy.deepcopy(self.polystrips)
if self.sel_gedge:
sel_gedge = self.polystrips.gedges.index(self.sel_gedge)
else:
sel_gedge = None
if self.sel_gvert:
sel_gvert = self.polystrips.gverts.index(self.sel_gvert)
else:
sel_gvert = None
if self.act_gvert:
act_gvert = self.polystrips.gverts.index(self.sel_gvert)
else:
act_gvert = None
polystrips_undo_cache.append(([p_data, sel_gvert, sel_gedge, act_gvert], action))
if len(polystrips_undo_cache) > settings.undo_depth:
polystrips_undo_cache.pop(0)
def undo_action(self):
'''
'''
if len(polystrips_undo_cache) > 0:
data, action = polystrips_undo_cache.pop()
self.polystrips = data[0]
if data[1]:
self.sel_gvert = self.polystrips.gverts[data[1]]
else:
self.sel_gvert = None
if data[2]:
self.sel_gedge = self.polystrips.gedges[data[2]]
else:
self.sel_gedge = None
if data[3]:
self.act_gvert = self.polystrips.gverts[data[3]]
else:
self.act_gvert = None
def cleanup(self, context):
'''
remove temporary object
'''
dprint('cleaning up!')
tmpobj = self.obj # Not always, sometimes if duplicate remains...will be .001
meobj = tmpobj.data
# Delete object
context.scene.objects.unlink(tmpobj)
tmpobj.user_clear()
if tmpobj.name in bpy.data.objects:
bpy.data.objects.remove(tmpobj)
bpy.context.scene.update()
bpy.data.meshes.remove(meobj)
################################
# Draw functions
def draw_callback(self, context):
settings = common_utilities.get_settings()
region,r3d = context.region,context.space_data.region_3d
new_matrix = [v for l in r3d.view_matrix for v in l]
if self.post_update or self.last_matrix != new_matrix:
for gv in self.polystrips.gverts:
gv.update_visibility(r3d)
for ge in self.polystrips.gedges:
ge.update_visibility(r3d)
if self.sel_gedge:
for gv in [self.sel_gedge.gvert1, self.sel_gedge.gvert2]:
gv.update_visibility(r3d)
if self.sel_gvert:
for gv in self.sel_gvert.get_inner_gverts():
gv.update_visibility(r3d)
if len(self.snap_eds):
mx = self.obj.matrix_world
self.snap_eds_vis = [False not in common_utilities.ray_cast_visible([mx * ed.verts[0].co, mx * ed.verts[1].co], self.obj, r3d) for ed in self.snap_eds]
self.post_update = False
self.last_matrix = new_matrix
if settings.debug < 3:
self.draw_callback_themed(context)
else:
self.draw_callback_debug(context)
def draw_callback_themed(self, context):
settings = common_utilities.get_settings()
region,r3d = context.region,context.space_data.region_3d
# theme_number = int(settings.theme)
color_inactive = PolystripsToolsAddonPreferences.theme_colors_mesh[settings.theme]
color_selection = PolystripsToolsAddonPreferences.theme_colors_selection[settings.theme]
color_active = PolystripsToolsAddonPreferences.theme_colors_selection[settings.theme] # Not used at the moment
bgl.glEnable(bgl.GL_POINT_SMOOTH)
for i_ge,gedge in enumerate(self.polystrips.gedges):
if gedge == self.sel_gedge:
color_border = (color_selection[0], color_selection[1], color_selection[2], 1.00)
color_fill = (color_selection[0], color_selection[1], color_selection[2], 0.20)
else:
color_border = (color_inactive[0], color_inactive[1], color_inactive[2], 1.00)
color_fill = (color_inactive[0], color_inactive[1], color_inactive[2], 0.20)
for c0,c1,c2,c3 in gedge.iter_segments(only_visible=True):
common_drawing.draw_quads_from_3dpoints(context, [c0,c1,c2,c3], color_fill)
common_drawing.draw_polyline_from_3dpoints(context, [c0,c1,c2,c3,c0], color_border, 1, "GL_LINE_STIPPLE")
if settings.debug >= 2:
# draw bezier
p0,p1,p2,p3 = gedge.gvert0.snap_pos, gedge.gvert1.snap_pos, gedge.gvert2.snap_pos, gedge.gvert3.snap_pos
p3d = [cubic_bezier_blend_t(p0,p1,p2,p3,t/16.0) for t in range(17)]
common_drawing.draw_polyline_from_3dpoints(context, p3d, (1,1,1,0.5),1, "GL_LINE_STIPPLE")
for i_gv,gv in enumerate(self.polystrips.gverts):
if not gv.is_visible(): continue
p0,p1,p2,p3 = gv.get_corners()
if gv.is_unconnected(): continue
is_selected = False
is_selected |= gv == self.sel_gvert
is_selected |= self.sel_gedge!=None and (self.sel_gedge.gvert0 == gv or self.sel_gedge.gvert1 == gv)
is_selected |= self.sel_gedge!=None and (self.sel_gedge.gvert2 == gv or self.sel_gedge.gvert3 == gv)
if is_selected:
color_border = (color_selection[0], color_selection[1], color_selection[2], 1.00)
color_fill = (color_selection[0], color_selection[1], color_selection[2], 0.20)
else:
color_border = (color_inactive[0], color_inactive[1], color_inactive[2], 1.00)
color_fill = (color_inactive[0], color_inactive[1], color_inactive[2], 0.20)
p3d = [p0,p1,p2,p3,p0]
common_drawing.draw_quads_from_3dpoints(context, [p0,p1,p2,p3], color_fill)
common_drawing.draw_polyline_from_3dpoints(context, p3d, color_border, 1, "GL_LINE_STIPPLE")
p3d = [gvert.position for gvert in self.polystrips.gverts if not gvert.is_unconnected() and gvert.is_visible()]
color = (color_inactive[0], color_inactive[1], color_inactive[2], 1.00)
common_drawing.draw_3d_points(context, p3d, color, 4)
if self.sel_gvert:
color = (color_selection[0], color_selection[1], color_selection[2], 1.00)
gv = self.sel_gvert
p0 = gv.position
if gv.is_inner():
p1 = gv.gedge_inner.get_outer_gvert_at(gv).position
common_drawing.draw_3d_points(context, [p0], color, 8)
common_drawing.draw_polyline_from_3dpoints(context, [p0,p1], color, 2, "GL_LINE_SMOOTH")
else:
p3d = [ge.get_inner_gvert_at(gv).position for ge in gv.get_gedges_notnone() if not ge.is_zippered()]
common_drawing.draw_3d_points(context, [p0] + p3d, color, 8)
for p1 in p3d:
common_drawing.draw_polyline_from_3dpoints(context, [p0,p1], color, 2, "GL_LINE_SMOOTH")
if self.sel_gedge:
color = (color_selection[0], color_selection[1], color_selection[2], 1.00)
ge = self.sel_gedge
if self.sel_gedge.is_zippered():
p3d = [ge.gvert0.position, ge.gvert3.position]
common_drawing.draw_3d_points(context, p3d, color, 8)
else:
p3d = [gv.position for gv in ge.gverts()]
common_drawing.draw_3d_points(context, p3d, color, 8)
common_drawing.draw_polyline_from_3dpoints(context, [p3d[0], p3d[1]], color, 2, "GL_LINE_SMOOTH")
common_drawing.draw_polyline_from_3dpoints(context, [p3d[2], p3d[3]], color, 2, "GL_LINE_SMOOTH")
if settings.show_segment_count:
draw_gedge_info(self.sel_gedge, context)
if self.act_gvert:
color = (color_active[0], color_active[1], color_active[2], 1.00)
gv = self.act_gvert
p0 = gv.position
common_drawing.draw_3d_points(context, [p0], color, 8)
if self.mode == 'sketch':
# Draw smoothing line (end of sketch to current mouse position)
common_drawing.draw_polyline_from_points(context, [self.sketch_curpos, self.sketch[-1][0]], color_active, 1, "GL_LINE_SMOOTH")
# Draw sketching stroke
common_drawing.draw_polyline_from_points(context, [co[0] for co in self.sketch], color_selection, 2, "GL_LINE_STIPPLE")
# Report pressure reading
info = str(round(self.sketch_pressure,3))
txt_width, txt_height = blf.dimensions(0, info)
d = self.sketch_brush.pxl_rad
blf.position(0, self.sketch_curpos[0] - txt_width/2, self.sketch_curpos[1] + d + txt_height, 0)
blf.draw(0, info)
if self.mode in {'scale tool','rotate tool'}:
# Draw a scale/rotate line from tool origin to current mouse position
common_drawing.draw_polyline_from_points(context, [self.action_center, self.mode_pos], (0, 0, 0, 0.5), 1, "GL_LINE_STIPPLE")
bgl.glLineWidth(1)
if self.mode == 'brush scale tool':
# scaling brush size
self.sketch_brush.draw(context, color=(1, 1, 1, .5), linewidth=1, color_size=(1, 1, 1, 1))
elif self.mode not in {'grab tool','scale tool','rotate tool'} and not self.is_navigating:
# draw the brush oriented to surface
ray,hit = common_utilities.ray_cast_region2d(region, r3d, self.cur_pos, self.obj, settings)
hit_p3d,hit_norm,hit_idx = hit
if hit_idx != -1: # and not self.hover_ed:
mx = self.obj.matrix_world
mxnorm = mx.transposed().inverted().to_3x3()
hit_p3d = mx * hit_p3d
hit_norm = mxnorm * hit_norm
common_drawing.draw_circle(context, hit_p3d, hit_norm.normalized(), self.stroke_radius_pressure, (1,1,1,.5))
if self.mode == 'sketch':
ray,hit = common_utilities.ray_cast_region2d(region, r3d, self.sketch[0][0], self.obj, settings)
hit_p3d,hit_norm,hit_idx = hit
if hit_idx != -1:
mx = self.obj.matrix_world
mxnorm = mx.transposed().inverted().to_3x3()
hit_p3d = mx * hit_p3d
hit_norm = mxnorm * hit_norm
common_drawing.draw_circle(context, hit_p3d, hit_norm.normalized(), self.stroke_radius_pressure, (1,1,1,.5))
if self.hover_ed and False:
color = (color_selection[0], color_selection[1], color_selection[2], 1.00)
common_drawing.draw_bmedge(context, self.hover_ed, self.to_obj.matrix_world, 2, color)
def draw_callback_debug(self, context):
settings = common_utilities.get_settings()
region = context.region
r3d = context.space_data.region_3d
draw_original_strokes = False
draw_gedge_directions = True
draw_gvert_orientations = False
draw_unconnected_gverts = False
draw_gvert_unsnapped = False
draw_gedge_bezier = False
draw_gedge_index = False
draw_gedge_igverts = False
cols = [(1,.5,.5,.8),(.5,1,.5,.8),(.5,.5,1,.8),(1,1,.5,.8)]
color_selected = (.5,1,.5,.8)
color_gedge = (1,.5,.5,.8)
color_gedge_nocuts = (.5,.2,.2,.8)
color_gedge_zipped = (.5,.7,.7,.8)
color_gvert_unconnected = (.2,.2,.2,.8)
color_gvert_endpoint = (.2,.2,.5,.8)
color_gvert_endtoend = (.5,.5,1,.8)
color_gvert_ljunction = (1,.5,1,.8)
color_gvert_tjunction = (1,1,.5,.8)
color_gvert_cross = (1,1,1,.8)
color_gvert_midpoints = (.7,1,.7,.8)
t = time.time()
tf = t - int(t)
tb = tf*2 if tf < 0.5 else 2-(tf*2)
tb1 = 1-tb
sel_fn = lambda c: tuple(cv*tb+cs*tb1 for cv,cs in zip(c,color_selected))
if draw_original_strokes:
for stroke in self.strokes_original:
#p3d = [pt for pt,pr in stroke]
#common_drawing.draw_polyline_from_3dpoints(context, p3d, (.7,.7,.7,.8), 3, "GL_LINE_SMOOTH")
common_drawing.draw_circle(context, stroke[0][0], Vector((0,0,1)),0.003,(.2,.2,.2,.8))
common_drawing.draw_circle(context, stroke[-1][0], Vector((0,1,0)),0.003,(.5,.5,.5,.8))
for i_ge,gedge in enumerate(self.polystrips.gedges):
if draw_gedge_directions:
p0,p1,p2,p3 = gedge.gvert0.snap_pos, gedge.gvert1.snap_pos, gedge.gvert2.snap_pos, gedge.gvert3.snap_pos
n0,n1,n2,n3 = gedge.gvert0.snap_norm, gedge.gvert1.snap_norm, gedge.gvert2.snap_norm, gedge.gvert3.snap_norm
pm = cubic_bezier_blend_t(p0,p1,p2,p3,0.5)
px = cubic_bezier_derivative(p0,p1,p2,p3,0.5).normalized()
pn = (n0+n3).normalized()
py = pn.cross(px).normalized()
rs = (gedge.gvert0.radius+gedge.gvert3.radius) * 0.35
rl = rs * 0.75
p3d = [pm-px*rs,pm+px*rs,pm+px*(rs-rl)+py*rl,pm+px*rs,pm+px*(rs-rl)-py*rl]
common_drawing.draw_polyline_from_3dpoints(context, p3d, (0.8,0.8,0.8,0.8),1, "GL_LINE_SMOOTH")
if draw_gedge_bezier:
p0,p1,p2,p3 = gedge.gvert0.snap_pos, gedge.gvert1.snap_pos, gedge.gvert2.snap_pos, gedge.gvert3.snap_pos
p3d = [cubic_bezier_blend_t(p0,p1,p2,p3,t/16.0) for t in range(17)]
common_drawing.draw_polyline_from_3dpoints(context, p3d, (0.5,0.5,0.5,0.8),1, "GL_LINE_SMOOTH")
col = color_gedge if len(gedge.cache_igverts) else color_gedge_nocuts
if gedge.zip_to_gedge: col = color_gedge_zipped
if gedge == self.sel_gedge: col = sel_fn(col)
w = 2 if len(gedge.cache_igverts) else 5
for c0,c1,c2,c3 in gedge.iter_segments(only_visible=True):
common_drawing.draw_polyline_from_3dpoints(context, [c0,c1,c2,c3,c0], col, w, "GL_LINE_SMOOTH")
if draw_gedge_index:
draw_gedge_text(gedge, context, str(i_ge))
if draw_gedge_igverts:
rm = (gedge.gvert0.radius + gedge.gvert3.radius)*0.1
for igv in gedge.cache_igverts:
common_drawing.common_drawing.draw_circle(context, igv.position, igv.normal, rm, (1,1,1,.3))
for i_gv,gv in enumerate(self.polystrips.gverts):
if not gv.is_visible(): continue
p0,p1,p2,p3 = gv.get_corners()
if not draw_unconnected_gverts and gv.is_unconnected() and gv != self.sel_gvert: continue
col = color_gvert_unconnected
if gv.is_endpoint(): col = color_gvert_endpoint
elif gv.is_endtoend(): col = color_gvert_endtoend
elif gv.is_ljunction(): col = color_gvert_ljunction
elif gv.is_tjunction(): col = color_gvert_tjunction
elif gv.is_cross(): col = color_gvert_cross
if gv == self.sel_gvert: col = sel_fn(col)
p3d = [p0,p1,p2,p3,p0]
common_drawing.draw_polyline_from_3dpoints(context, p3d, col, 2, "GL_LINE_SMOOTH")
if draw_gvert_orientations:
p,x,y = gv.snap_pos,gv.snap_tanx,gv.snap_tany
common_drawing.draw_polyline_from_3dpoints(context, [p,p+x*0.005], (1,0,0,1), 1, "GL_LINE_SMOOTH")
common_drawing.draw_polyline_from_3dpoints(context, [p,p+y*0.005], (0,1,0,1), 1, "GL_LINE_SMOOTH")
if draw_gvert_unsnapped:
for gv in self.polystrips.gverts:
p,x,y,n = gv.position,gv.snap_tanx,gv.snap_tany,gv.snap_norm
common_drawing.draw_polyline_from_3dpoints(context, [p,p+x*0.01], (1,0,0,1), 1, "GL_LINE_SMOOTH")
common_drawing.draw_polyline_from_3dpoints(context, [p,p+y*0.01], (0,1,0,1), 1, "GL_LINE_SMOOTH")
common_drawing.draw_polyline_from_3dpoints(context, [p,p+n*0.01], (0,0,1,1), 1, "GL_LINE_SMOOTH")
if self.sel_gedge:
if not self.sel_gedge.zip_to_gedge:
col = color_gvert_midpoints
for gv in self.sel_gedge.get_inner_gverts():
if not gv.is_visible(): continue
p0,p1,p2,p3 = gv.get_corners()
p3d = [p0,p1,p2,p3,p0]
common_drawing.draw_polyline_from_3dpoints(context, p3d, col, 2, "GL_LINE_SMOOTH")
draw_gedge_info(self.sel_gedge, context)
if self.sel_gvert:
col = color_gvert_midpoints
for ge in self.sel_gvert.get_gedges_notnone():
if ge.zip_to_gedge: continue
gv = ge.get_inner_gvert_at(self.sel_gvert)
if not gv.is_visible(): continue
p0,p1,p2,p3 = gv.get_corners()
p3d = [p0,p1,p2,p3,p0]
common_drawing.draw_polyline_from_3dpoints(context, p3d, col, 2, "GL_LINE_SMOOTH")
if self.mode == 'sketch':
common_drawing.draw_polyline_from_points(context, [self.sketch_curpos, self.sketch[-1][0]], (0.5,0.5,0.2,0.8), 1, "GL_LINE_SMOOTH")
common_drawing.draw_polyline_from_points(context, [co[0] for co in self.sketch], (1,1,.5,.8), 2, "GL_LINE_SMOOTH")
info = str(round(self.sketch_pressure,3))
''' draw text '''
txt_width, txt_height = blf.dimensions(0, info)
d = self.sketch_brush.pxl_rad
blf.position(0, self.sketch_curpos[0] - txt_width/2, self.sketch_curpos[1] + d + txt_height, 0)
blf.draw(0, info)
if self.mode in {'scale tool','rotate tool'}:
common_drawing.draw_polyline_from_points(context, [self.action_center, self.mode_pos], (0,0,0,0.5), 1, "GL_LINE_STIPPLE")
bgl.glLineWidth(1)
if self.mode not in {'grab tool','scale tool','rotate tool','brush scale tool'}:
ray,hit = common_utilities.ray_cast_region2d(region, r3d, self.cur_pos, self.obj, settings)
hit_p3d,hit_norm,hit_idx = hit
if hit_idx != -1:
mx = self.obj.matrix_world
hit_p3d = mx * hit_p3d
common_drawing.draw_circle(context, hit_p3d, hit_norm.normalized(), self.stroke_radius_pressure, (1,1,1,.5))
if not self.hover_ed:
self.sketch_brush.draw(context)
else:
common_drawing.draw_bmedge(context, self.hover_ed, self.to_obj.matrix_world, 2, color_selected)
############################
# function to convert polystrips => mesh
def create_mesh(self, context):
verts,quads = self.polystrips.create_mesh()
if self.to_bme and self.to_obj: #EDIT MDOE on Existing Mesh
bm = self.to_bme
mx = self.to_obj.matrix_world
imx = mx.inverted()
mx2 = self.obj.matrix_world
imx2 = mx2.inverted()
else:
bm = bmesh.new()
mx2 = Matrix.Identity(4)
imx = Matrix.Identity(4)
nm_polystrips = self.obj.name + "_polystrips"
dest_me = bpy.data.meshes.new(nm_polystrips)
dest_obj = bpy.data.objects.new(nm_polystrips, dest_me)
dest_obj.matrix_world = self.obj.matrix_world
dest_obj.update_tag()
dest_obj.show_all_edges = True
dest_obj.show_wire = True
dest_obj.show_x_ray = True
context.scene.objects.link(dest_obj)
dest_obj.select = True
context.scene.objects.active = dest_obj
bmverts = [bm.verts.new(imx * mx2 * v) for v in verts]
bm.verts.index_update()
for q in quads:
bm.faces.new([bmverts[i] for i in q])
bm.faces.index_update()
if self.to_bme and self.to_obj:
bmesh.update_edit_mesh(self.to_obj.data, tessface=False, destructive=True)
bm.free()
else:
bm.to_mesh(dest_me)
bm.free()
###########################
# hover functions
def hover_geom(self,eventd):
if not len(self.snap_eds):
return
context = eventd['context']
region,r3d = context.region,context.space_data.region_3d
new_matrix = [v for l in r3d.view_matrix for v in l]
x, y = eventd['mouse']
mouse_loc = Vector((x,y))
mx = self.to_obj.matrix_world
if self.post_update or self.last_matrix != new_matrix:
# Update all the visibility stuff
self.snap_eds_vis = [False not in common_utilities.ray_cast_visible([mx * ed.verts[0].co, mx * ed.verts[1].co], self.obj, r3d) for ed in self.snap_eds]
# Sticky highlight...check the hovered edge first
if self.hover_ed:
a = location_3d_to_region_2d(region, r3d, mx * self.hover_ed.verts[0].co)
b = location_3d_to_region_2d(region, r3d, mx * self.hover_ed.verts[1].co)
if a and b:
intersect = intersect_point_line(mouse_loc, a, b)
dist = (intersect[0] - mouse_loc).length_squared
bound = intersect[1]
if (dist < 100) and (bound < 1) and (bound > 0):
return
self.hover_ed = None
for i,ed in enumerate(self.snap_eds):
if self.snap_eds_vis[i]:
a = location_3d_to_region_2d(region, r3d, mx * ed.verts[0].co)
b = location_3d_to_region_2d(region, r3d, mx * ed.verts[1].co)
if a and b:
intersect = intersect_point_line(mouse_loc, a, b)
dist = (intersect[0] - mouse_loc).length_squared
bound = intersect[1]
if (dist < 100) and (bound < 1) and (bound > 0):
self.hover_ed = ed
break
###########################
# tool functions
def ready_tool(self, eventd, tool_fn):
rgn = eventd['context'].region
r3d = eventd['context'].space_data.region_3d
mx,my = eventd['mouse']
if self.sel_gvert:
loc = self.sel_gvert.position
cx,cy = location_3d_to_region_2d(rgn, r3d, loc)
elif self.sel_gedge:
loc = (self.sel_gedge.gvert0.position + self.sel_gedge.gvert3.position) / 2.0
cx,cy = location_3d_to_region_2d(rgn, r3d, loc)
else:
cx,cy = mx-100,my
rad = math.sqrt((mx-cx)**2 + (my-cy)**2)
self.action_center = (cx,cy)
self.mode_start = (mx,my)
self.action_radius = rad
self.mode_radius = rad
self.prev_pos = (mx,my)
# spc = bpy.data.window_managers['WinMan'].windows[0].screen.areas[4].spaces[0]
# r3d = spc.region_3d
vrot = r3d.view_rotation
self.tool_x = (vrot * Vector((1,0,0))).normalized()
self.tool_y = (vrot * Vector((0,1,0))).normalized()
self.tool_rot = 0.0
self.tool_fn = tool_fn
self.tool_fn('init', eventd)
def scale_tool_gvert(self, command, eventd):
if command == 'init':
self.footer = 'Scaling GVerts'
sgv = self.sel_gvert
lgv = [ge.gvert1 if ge.gvert0==sgv else ge.gvert2 for ge in sgv.get_gedges() if ge]
self.tool_data = [(gv,Vector(gv.position)) for gv in lgv]
elif command == 'commit':
pass
elif command == 'undo':
for gv,p in self.tool_data:
gv.position = p
gv.update()
self.sel_gvert.update()
self.sel_gvert.update_visibility(eventd['r3d'], update_gedges=True)
else:
m = command
sgv = self.sel_gvert
p = sgv.position
for ge in sgv.get_gedges():
if not ge: continue
gv = ge.gvert1 if ge.gvert0 == self.sel_gvert else ge.gvert2
gv.position = p + (gv.position-p) * m
gv.update()
sgv.update()
self.sel_gvert.update_visibility(eventd['r3d'], update_gedges=True)
def scale_tool_gvert_radius(self, command, eventd):
if command == 'init':
self.footer = 'Scaling GVert radius'
self.tool_data = self.sel_gvert.radius
elif command == 'commit':
pass
elif command == 'undo':
self.sel_gvert.radius = self.tool_data
self.sel_gvert.update()
self.sel_gvert.update_visibility(eventd['r3d'], update_gedges=True)
else:
m = command
self.sel_gvert.radius *= m
self.sel_gvert.update()
self.sel_gvert.update_visibility(eventd['r3d'], update_gedges=True)
def scale_tool_stroke_radius(self, command, eventd):
if command == 'init':
self.footer = 'Scaling Stroke radius'
self.tool_data = self.stroke_radius
elif command == 'commit':
pass
elif command == 'undo':
self.stroke_radius = self.tool_data
else:
m = command
self.stroke_radius *= m
def grab_tool_gvert_list(self, command, eventd, lgv):
'''
translates list of gverts
note: translation is relative to first gvert
'''
def l3dr2d(p): return location_3d_to_region_2d(eventd['region'], eventd['r3d'], p)
if command == 'init':
self.footer = 'Translating GVert position(s)'
s2d = l3dr2d(lgv[0].position)
self.tool_data = [(gv, Vector(gv.position), l3dr2d(gv.position)-s2d) for gv in lgv]
elif command == 'commit':
pass
elif command == 'undo':
for gv,p,_ in self.tool_data: gv.position = p
for gv,_,_ in self.tool_data:
gv.update()
gv.update_visibility(eventd['r3d'], update_gedges=True)
else:
factor_slow,factor_fast = 0.2,1.0
dv = Vector(command) * (factor_slow if eventd['shift'] else factor_fast)
s2d = l3dr2d(self.tool_data[0][0].position)
lgv2d = [s2d+relp+dv for _,_,relp in self.tool_data]
pts = common_utilities.ray_cast_path(eventd['context'], self.obj, lgv2d)
if len(pts) != len(lgv2d): return ''
for d,p2d in zip(self.tool_data, pts):
d[0].position = p2d
for gv,_,_ in self.tool_data:
gv.update()
gv.update_visibility(eventd['r3d'], update_gedges=True)
def grab_tool_gvert(self, command, eventd):
'''
translates selected gvert
'''
if command == 'init':
lgv = [self.sel_gvert]
else:
lgv = None
self.grab_tool_gvert_list(command, eventd, lgv)
def grab_tool_gvert_neighbors(self, command, eventd):
'''
translates selected gvert and its neighbors
note: translation is relative to selected gvert
'''
if command == 'init':
sgv = self.sel_gvert
lgv = [sgv] + [ge.get_inner_gvert_at(sgv) for ge in sgv.get_gedges_notnone()]
else:
lgv = None
self.grab_tool_gvert_list(command, eventd, lgv)
def grab_tool_gedge(self, command, eventd):
if command == 'init':
sge = self.sel_gedge
lgv = [sge.gvert0, sge.gvert3]
lgv += [ge.get_inner_gvert_at(gv) for gv in lgv for ge in gv.get_gedges_notnone()]
else:
lgv = None
self.grab_tool_gvert_list(command, eventd, lgv)
def rotate_tool_gvert_neighbors(self, command, eventd):
if command == 'init':
self.footer = 'Rotating GVerts'
self.tool_data = [(gv,Vector(gv.position)) for gv in self.sel_gvert.get_inner_gverts()]
elif command == 'commit':
pass
elif command == 'undo':
for gv,p in self.tool_data:
gv.position = p
gv.update()
else:
ang = command
q = Quaternion(self.sel_gvert.snap_norm, ang)
p = self.sel_gvert.position
for gv,up in self.tool_data:
gv.position = p+q*(up-p)
gv.update()
def scale_brush_pixel_radius(self,command, eventd):
if command == 'init':
self.footer = 'Scale Brush Pixel Size'
self.tool_data = self.stroke_radius
x,y = eventd['mouse']
self.sketch_brush.brush_pix_size_init(eventd['context'], x, y)
elif command == 'commit':
self.sketch_brush.brush_pix_size_confirm(eventd['context'])
if self.sketch_brush.world_width:
self.stroke_radius = self.sketch_brush.world_width
elif command == 'undo':
self.sketch_brush.brush_pix_size_cancel(eventd['context'])
self.stroke_radius = self.tool_data