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 2
/
common_drawing.py
210 lines (172 loc) · 6.55 KB
/
common_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
'''
Copyright (C) 2014 CG Cookie
http://cgcookie.com
hello@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/>.
'''
import bpy
import bgl
import blf
import bmesh
import time
import math
import random
from itertools import chain,combinations
from collections import deque
from bpy_extras import view3d_utils
from mathutils import Vector, Matrix, Quaternion
from mathutils.geometry import intersect_line_plane, intersect_point_line, distance_point_to_plane, intersect_line_line_2d, intersect_line_line
from bpy_extras.view3d_utils import location_3d_to_region_2d, region_2d_to_vector_3d, region_2d_to_location_3d, region_2d_to_origin_3d
from .common_utilities import dprint
def bgl_col(rgb, alpha):
'''
takes a Vector of len 3 (eg, a color setting)
returns a 4 item tuple (r,g,b,a) for use with
bgl drawing.
'''
#TODO Test variables for acceptability
color = (rgb[0], rgb[1], rgb[2], alpha)
return color
def draw_points(context, points, color, size):
'''
draw a bunch of dots
args:
points: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
size: integer? maybe a float
'''
bgl.glColor4f(*color)
bgl.glPointSize(size)
bgl.glBegin(bgl.GL_POINTS)
for coord in points:
bgl.glVertex2f(*coord)
bgl.glEnd()
return
def draw_circle(context, c,n,r,col,step=10):
x = Vector((0.42,-0.42,0.42)).cross(n).normalized() * r
y = n.cross(x).normalized() * r
d2r = math.pi/180
p3d = [c+x*math.cos(i*d2r)+y*math.sin(i*d2r) for i in range(0,360+step,step)]
draw_polyline_from_3dpoints(context, p3d, col, 1, "GL_LINE_SMOOTH")
def draw_3d_points(context, points, color, size):
'''
draw a bunch of dots
args:
points: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
size: integer? maybe a float
'''
points_2d = [location_3d_to_region_2d(context.region, context.space_data.region_3d, loc) for loc in points]
bgl.glColor4f(*color)
bgl.glPointSize(size)
bgl.glBegin(bgl.GL_POINTS)
for coord in points_2d:
#TODO: Debug this problem....perhaps loc_3d is returning points off of the screen.
if coord:
bgl.glVertex2f(*coord)
else:
print('how the f did nones get in here')
print(coord)
bgl.glEnd()
return
def draw_polyline_from_points(context, points, color, thickness, LINE_TYPE):
'''
a simple way to draw a line
args:
points: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
thickness: integer? maybe a float
LINE_TYPE: eg...bgl.GL_LINE_STIPPLE or
'''
if LINE_TYPE == "GL_LINE_STIPPLE":
bgl.glLineStipple(4, 0x5555) #play with this later
bgl.glEnable(bgl.GL_LINE_STIPPLE)
bgl.glEnable(bgl.GL_BLEND)
current_width = bgl.GL_LINE_WIDTH
bgl.glColor4f(*color)
bgl.glLineWidth(thickness)
bgl.glBegin(bgl.GL_LINE_STRIP)
for coord in points:
bgl.glVertex2f(*coord)
bgl.glEnd()
bgl.glLineWidth(1)
if LINE_TYPE == "GL_LINE_STIPPLE":
bgl.glDisable(bgl.GL_LINE_STIPPLE)
bgl.glEnable(bgl.GL_BLEND) # back to uninterupted lines
return
def draw_polyline_from_3dpoints(context, points_3d, color, thickness, LINE_TYPE):
'''
a simple way to draw a line
slow...becuase it must convert to screen every time
but allows you to pan and zoom around
args:
points_3d: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
thickness: integer? maybe a float
LINE_TYPE: eg...bgl.GL_LINE_STIPPLE or
'''
points = [location_3d_to_region_2d(context.region, context.space_data.region_3d, loc) for loc in points_3d]
if LINE_TYPE == "GL_LINE_STIPPLE":
bgl.glLineStipple(4, 0x5555) #play with this later
bgl.glEnable(bgl.GL_LINE_STIPPLE)
bgl.glEnable(bgl.GL_BLEND)
bgl.glColor4f(*color)
bgl.glLineWidth(thickness)
bgl.glBegin(bgl.GL_LINE_STRIP)
for coord in points:
bgl.glVertex2f(*coord)
bgl.glEnd()
if LINE_TYPE == "GL_LINE_STIPPLE":
bgl.glDisable(bgl.GL_LINE_STIPPLE)
bgl.glEnable(bgl.GL_BLEND) # back to uninterupted lines
bgl.glLineWidth(1)
return
def draw_quads_from_3dpoints(context, points_3d, color):
'''
a simple way to draw a set of quads
slow...becuase it must convert to screen every time
but allows you to pan and zoom around
args:
points_3d: a list of tuples as x,y,z
color: tuple (r,g,b,a)
'''
points = [location_3d_to_region_2d(context.region, context.space_data.region_3d, loc) for loc in points_3d]
bgl.glEnable(bgl.GL_BLEND)
bgl.glColor4f(*color)
bgl.glBegin(bgl.GL_QUADS)
for coord in points:
bgl.glVertex2f(*coord)
bgl.glEnd()
return
def draw_outline_or_region(mode, points, color):
'''
arg:
mode - either bgl.GL_POLYGON or bgl.GL_LINE_LOOP
color - will need to be set beforehand using theme colors. eg
bgl.glColor4f(self.ri, self.gi, self.bi, self.ai)
'''
bgl.glColor4f(color[0],color[1],color[2],color[3])
if mode == 'GL_LINE_LOOP':
bgl.glBegin(bgl.GL_LINE_LOOP)
else:
bgl.glBegin(bgl.GL_POLYGON)
# start with corner right-bottom
for i in range(0,len(points)):
bgl.glVertex2f(points[i][0],points[i][1])
bgl.glEnd()
def draw_bmedge(context, bmedge, mx, thickness, color):
'''
simple wrapper to drawp a bmedge
'''
points = [mx * bmedge.verts[0].co, mx*bmedge.verts[1].co]
draw_polyline_from_3dpoints(context, points, color, thickness, 'GL_LINE_SMOOTH')