-
Notifications
You must be signed in to change notification settings - Fork 0
/
devdisp.py
233 lines (185 loc) · 5.5 KB
/
devdisp.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
import bpy, bgl, blf
import numpy as np
from bpy_extras.view3d_utils import location_3d_to_region_2d
from bgl import glEnable, glDisable, glColor3f, glColor4f, glVertex3f, glPointSize, glLineWidth, glBegin, glEnd, glLineStipple, GL_POINTS, GL_LINE_STRIP, GL_LINES, GL_LINE_STIPPLE
from mathutils import Vector, Matrix
draw_handle = []
### STORAGE
points = {}
segments = {}
chains = {}
plots = {}
transform = Matrix()
textdraw = True
gldraw = True
### STORAGE CONTROL
def clear():
points.clear()
segments.clear()
chains.clear()
global plot
plot = None
tag_redraw_all_view3d()
def set_transform(matrix):
global transform
if type(matrix) == Matrix:
transform = matrix
def linear(value):
return value
def plot_add(funcy=linear, rangex=[0,1], resolution=0.1, k=''):
if not k:
k = next_int_key(plots.keys())
X = np.arange(*rangex, resolution).tolist()
Y = [funcy(x) for x in X]
plots[k] = [X,Y]
tag_redraw_all_view3d()
def points_add(value, k=''):
if not k:
k = next_int_key(points.keys())
#if transform:
value = transform * value
points[str(k)] = value
tag_redraw_all_view3d()
def segments_add(value, k=''):
if not k:
k = next_int_key(segments.keys())
#if transform:
value = [transform*v for v in value]
segments[str(k)] = value
def point_chain_add(values, k=''):
values = create_segment_list(values)
if not k:
k = next_int_key(chains.keys())
chains[k] = values
### UTILS
def create_segment_list(values=[]):
values = [list(v) for v in values]
segments = list(zip(values[:-1], values[1:]))
return segments
def next_int_key(keys):
nums = []
for k in keys:
try:
num = int(k)
nums.append(num)
except:
pass
if nums:
return sorted(nums)[-1] + 1
return 0
### DRAWING
def show_text(value):
global textdraw
textdraw = bool(value)
def show_gl(value):
global gldraw
gldraw = bool(value)
def tag_redraw_all_view3d():
context = bpy.context
for window in context.window_manager.windows:
for area in window.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
region.tag_redraw()
def draw_start():
SpaceView3D = bpy.types.SpaceView3D
if draw_handle:
return
handle_pixel = SpaceView3D.draw_handler_add(draw_devdisp_px, (), 'WINDOW', 'POST_PIXEL')
handle_view = SpaceView3D.draw_handler_add(draw_devdisp_view, (), 'WINDOW', 'POST_VIEW')
draw_handle[:] = [handle_pixel, handle_view]
tag_redraw_all_view3d()
def draw_stop():
SpaceView3D = bpy.types.SpaceView3D
if not draw_handle:
return
handle_pixel, handle_view = draw_handle
SpaceView3D.draw_handler_remove(handle_pixel, 'WINDOW')
SpaceView3D.draw_handler_remove(handle_view, 'WINDOW')
draw_handle[:] = []
tag_redraw_all_view3d()
### GL PRIMITIVES
def draw_line(v1, v2, width=1, color=(0,1,0)):
glColor3f(*color)
glLineWidth(width)
glBegin(GL_LINE_STRIP)
glVertex3f(*v1)
glVertex3f(*v2)
glEnd()
glLineWidth(1)
def draw_vert(v1, width=5.0, color=(1,0,0)):
glColor3f(*color)
glBegin(GL_POINTS)
glPointSize(width)
glVertex3f(*v1)
glEnd()
### VIEW DRAW
def draw_devdisp_view():
if not gldraw:
return
#context = bpy.context
#region = context.region
#region3d = context.space_data.region_3d
#region_mid_width = region.width / 2.0
#region_mid_height = region.height / 2.0
#perspective_matrix = region3d.perspective_matrix.copy()
if points:
#glColor3f(1, 0, 0)
for k,v in points.items():
draw_vert(Vector(v).to_3d(), color=(1, 0, 0))
if segments:
#glColor3f(0, 1, 0)
for k,v in segments.items():
draw_line(*v, color=(0,1,0))
if chains:
#glColor3f(0, 0, 1)
for k,v in chains.items():
if v:
for s in v:
draw_line(*v, color=(0,0,1))
if plots:
for k,v in plots.items():
draw_plot(v)
tag_redraw_all_view3d()
### TEXT PRIMITIVE
def draw_text(text, vec):
context = bpy.context
region = context.region
region3d = context.space_data.region_3d
font_id=0
vec = Vector(vec)
vec = location_3d_to_region_2d(region, region3d, vec)
blf.position(font_id, vec.x + 5, vec.y - 5, 1)
blf.size(font_id, 11, 72)
blf.draw(font_id, text)
### PIXEL DRAW
def draw_devdisp_px():
#context = bpy.context
#region = context.region
#region3d = context.space_data.region_3d
if textdraw:
if points:
for k,v in points.items():
draw_text(k,v)
if segments:
for k,v in segments.items():
draw_text(k, (v[0]+v[1])/2)
if chains:
for k,v in chains.items():
if v:
draw_text(k, (v[0][0] + v[0][1]) / 2)
# if plot:
# draw_plot()
tag_redraw_all_view3d()
def draw_plot(plot):
#context = bpy.context
#region = context.region
#region3d = context.space_data.region_3d
#region_mid_width = region.width / 2.0
#region_mid_height = region.height / 2.0
#perspective_matrix = region3d.perspective_matrix.copy()
veclist = [Vector((x,y,0)) for x,y in zip(plot[0], plot[1])]
plotlineedges = create_segment_list(veclist)
for e in plotlineedges:
draw_line(*e, color=(1,0,0))