forked from bnpr/Abnormal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_drawing.py
295 lines (234 loc) · 9.16 KB
/
functions_drawing.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
import bpy
import bgl
import traceback
from mathutils import Vector
from gpu_extras.batch import batch_for_shader
from bpy_extras import view3d_utils
from .functions_general import *
def refresh_batches(modal, context):
# ACTIVELY DRAWING DATA LISTS
if modal.selection_drawing:
create_selection_drawing_lists(modal)
if modal.redraw:
modal._container.update_static()
if modal.redraw_active:
modal._container.update_active()
modal.redraw = False
modal.redraw_active = False
force_scene_update()
return
def draw_callback_3d(modal, context):
clear_draw = False
try:
if modal._modal_running == False:
clear_draw = True
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_VERTEX_PROGRAM_POINT_SIZE)
if modal._x_ray_mode == False:
bgl.glEnable(bgl.GL_DEPTH_TEST)
modal._container.draw()
if len(modal.translate_draw_line) > 0:
bgl.glEnable(bgl.GL_DEPTH_TEST)
bgl.glLineWidth(2)
modal.shader_3d.bind()
if modal.translate_axis == 0:
modal.shader_3d.uniform_float("color", (1.0, 0.0, 0.0, 1.0))
if modal.translate_axis == 1:
modal.shader_3d.uniform_float("color", (0.0, 1.0, 0.0, 1.0))
if modal.translate_axis == 2:
modal.shader_3d.uniform_float("color", (0.0, 0.0, 1.0, 1.0))
modal.batch_translate_line.draw(modal.shader_3d)
bgl.glDisable(bgl.GL_DEPTH_TEST)
if modal._x_ray_mode == False:
bgl.glDisable(bgl.GL_DEPTH_TEST)
if modal._use_gizmo:
modal._window.gizmo_draw()
bgl.glDisable(bgl.GL_BLEND)
bgl.glDisable(bgl.GL_VERTEX_PROGRAM_POINT_SIZE)
except Exception:
print()
print()
print()
print('######')
print('ABNORMAL DRAW 3D ERROR! INCLUDE THE NEXT ERROR MESSAGE WITH ANY BUG REPORT')
print('######')
print()
print()
traceback.print_exc()
print()
print()
print()
clear_draw = True
if clear_draw:
print('Something is wrong, 3D Draw Handler is being removed')
dns = bpy.app.driver_namespace
dc = dns.get("dh3d")
try:
bpy.types.SpaceView3D.draw_handler_remove(dc, 'WINDOW')
except:
pass
return
def draw_callback_2d(modal, context):
clear_draw = False
try:
if modal._modal_running == False:
clear_draw = True
if context.area == modal._draw_area:
bgl.glLineWidth(2)
modal.shader_2d.bind()
modal.shader_2d.uniform_float("color", (0.05, 0.05, 0.05, 1))
modal.batch_rotate_screen_lines.draw(modal.shader_2d)
bgl.glLineWidth(1)
modal.shader_2d.bind()
modal.shader_2d.uniform_float("color", (1.0, 1.0, 1.0, 1))
modal.batch_boxsel_screen_lines.draw(modal.shader_2d)
bgl.glLineWidth(1)
modal.shader_2d.bind()
modal.shader_2d.uniform_float("color", (1.0, 1.0, 1.0, 1))
modal.batch_circlesel_screen_lines.draw(modal.shader_2d)
bgl.glLineWidth(1)
modal.shader_2d.bind()
modal.shader_2d.uniform_float("color", (1.0, 1.0, 1.0, 1))
modal.batch_lassosel_screen_lines.draw(modal.shader_2d)
modal._window.draw()
except Exception:
print()
print()
print()
print('######')
print('ABNORMAL DRAW 2D ERROR! INCLUDE THE NEXT ERROR MESSAGE WITH ANY BUG REPORT')
print('######')
print()
print()
traceback.print_exc()
print()
print()
print()
clear_draw = True
if clear_draw:
print('Something is wrong, 2D Draw Handler is being removed')
dns = bpy.app.driver_namespace
dc = dns.get("dh2d")
try:
bpy.types.SpaceView3D.draw_handler_remove(dc, 'WINDOW')
except:
pass
return
def clear_drawing(modal):
try:
bpy.types.SpaceView3D.draw_handler_remove(
modal._draw_handle_2d, "WINDOW")
bpy.types.SpaceView3D.draw_handler_remove(
modal._draw_handle_3d, "WINDOW")
except:
pass
modal._draw_handle_2d = None
modal._draw_handle_3d = None
return
def viewport_change_cache(modal, context):
if context.area.type == 'VIEW_3D':
for space in context.area.spaces:
if space.type == 'VIEW_3D':
modal._reg_header = space.show_region_toolbar
modal._reg_ui = space.show_region_ui
modal._cursor = space.overlay.show_cursor
modal._wireframe = space.overlay.show_wireframes
modal._thresh = space.overlay.wireframe_threshold
modal._text = space.overlay.show_text
modal._use_wireframe_overlay = modal._display_prefs.display_wireframe
space.overlay.show_wireframes = modal._use_wireframe_overlay
space.overlay.wireframe_threshold = 1.0
space.show_region_toolbar = False
space.show_region_ui = False
space.overlay.show_cursor = False
space.overlay.show_text = False
return
#
#
def start_active_drawing(modal):
modal._container.update_active()
modal._container.update_static(exclude_active=True)
return
def end_active_drawing(modal):
modal._container.clear_active_batches()
modal._container.update_static()
return
def end_selection_drawing(modal):
modal.selection_drawing = False
empty_selection_drawing_lists(modal)
return
def create_selection_drawing_lists(modal):
# region outline calculation
rh = modal.act_reg.height
rw = modal.act_reg.width
center = rw/2
# CIRCLE SELECTION LINES
circlesel_screen_lines = []
if modal.circle_selecting or modal.circle_resizing:
if modal.circle_resizing:
cur_loc = modal._mouse_init[:2]
else:
cur_loc = modal._mouse_reg_loc[:2]
circlesel_screen_lines = get_circle_cos(
cur_loc, 32, modal.circle_radius, close_end=True).tolist()
# LASSO SELECTION LINES
lassosel_screen_lines = []
if modal.lasso_selecting:
lassosel_screen_lines = np.vstack(
(np.array(modal._mode_cache[0]), modal._mode_cache[0][0]))[:, [0, 1]].tolist()
# BOX SELECTION LINES
boxsel_screen_lines = []
if modal.box_selecting:
init_loc = Vector(modal._mode_cache[0][0]).xy
cur_loc = Vector(modal._mode_cache[0][1]).xy
top_right = Vector(
(modal._mode_cache[0][1][0], modal._mode_cache[0][0][1]))
bot_left = Vector(
(modal._mode_cache[0][0][0], modal._mode_cache[0][1][1]))
vec = init_loc-top_right
start_co = top_right
dashed_lines = vec_to_dashed(start_co, vec, int(vec.length/10))
boxsel_screen_lines += dashed_lines
vec = top_right-cur_loc
start_co = cur_loc
dashed_lines = vec_to_dashed(start_co, vec, int(vec.length/10))
boxsel_screen_lines += dashed_lines
vec = cur_loc-bot_left
start_co = bot_left
dashed_lines = vec_to_dashed(start_co, vec, int(vec.length/10))
boxsel_screen_lines += dashed_lines
vec = bot_left-init_loc
start_co = init_loc
dashed_lines = vec_to_dashed(start_co, vec, int(vec.length/10))
boxsel_screen_lines += dashed_lines
# ROTATION CENTER LINE
rot_screen_lines = []
if modal.rotating:
cent_loc = view3d_utils.location_3d_to_region_2d(
modal.act_reg, modal.act_rv3d, modal._mode_cache[0])
cur_loc = Vector(modal._mouse_reg_loc[:2])
vec = cur_loc-cent_loc
start_co = cent_loc
dashed_lines = vec_to_dashed(start_co, vec, int(vec.length/10))
rot_screen_lines += dashed_lines
# STORE 2D BATCHES
modal.batch_boxsel_screen_lines = batch_for_shader(
modal.shader_2d, 'LINES', {"pos": boxsel_screen_lines})
modal.batch_lassosel_screen_lines = batch_for_shader(
modal.shader_2d, 'LINE_STRIP', {"pos": lassosel_screen_lines})
modal.batch_circlesel_screen_lines = batch_for_shader(
modal.shader_2d, 'LINE_STRIP', {"pos": circlesel_screen_lines})
modal.batch_rotate_screen_lines = batch_for_shader(
modal.shader_2d, 'LINES', {"pos": rot_screen_lines})
return
def empty_selection_drawing_lists(modal):
# STORE 2D BATCHES
modal.batch_boxsel_screen_lines = batch_for_shader(
modal.shader_2d, 'LINES', {"pos": []})
modal.batch_lassosel_screen_lines = batch_for_shader(
modal.shader_2d, 'LINE_STRIP', {"pos": []})
modal.batch_circlesel_screen_lines = batch_for_shader(
modal.shader_2d, 'LINE_STRIP', {"pos": []})
modal.batch_rotate_screen_lines = batch_for_shader(
modal.shader_2d, 'LINES', {"pos": []})
return