-
Notifications
You must be signed in to change notification settings - Fork 2
/
box.py
219 lines (184 loc) · 9.12 KB
/
box.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
# box.py
"""
Contains routines to generate the bounding box.
"""
def bounding_box(extrema=None):
"""
Plot a bounding box with a given size.
Signature:
bounding_box(extrema)
Parameters
----------
extrema: If none given, use globals.
Array of length 6 with components [x_min, x_max, y_min, y_max, z_min, z_max].
Returns
-------
Class containing the bounding box.
Examples
--------
>>> import numpy as np
>>> import blendaviz as blt
>>> extrema = np.array([0, 1, 0, 2, 0, 3])
>>> blt.bounding_box(extrema)
"""
import inspect
# Assign parameters to the PathLine objects.
bounding_box_return = BoundingBox()
argument_dict = inspect.getargvalues(inspect.currentframe()).locals
for argument in argument_dict:
setattr(bounding_box_return, argument, argument_dict[argument])
# Plot the bounding box.
bounding_box_return.plot()
return bounding_box_return
class BoundingBox():
"""
Bounding box class including the splinces, parameters and plotting function.
"""
def __init__(self):
"""
Fill members with default values.
"""
import blendaviz as blt
# Define the members that can be seen by the user.
self.extrema = None
self.curve_data = None
self.curve_object = None
self.poly_line = None
self.deletable_object = None
# Add the plot to the stack.
blt.plot_stack.append(self)
def plot(self):
"""
Plot the bounding box.
"""
import bpy
# from blendaviz import colors
# Check if extrema are given.
if self.extrema is None:
self.get_extrema()
# Delete existing curve.
if not self.curve_data is None:
for curve_data in self.curve_data:
bpy.data.curves.remove(curve_data)
# Initialize the list of curve data and object.
self.curve_data = []
self.curve_object = []
self.poly_line = []
# Create the bezier curve.
self.curve_data.append(bpy.data.curves.new('DataCurve', type='CURVE'))
self.curve_data[-1].dimensions = '3D'
self.curve_object.append(bpy.data.objects.new('ObjCurve', self.curve_data[-1]))
# Set the origin.
self.curve_object[-1].location = tuple((self.extrema[0], self.extrema[2], self.extrema[4]))
# Add the rest of the curve.
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[1] - self.extrema[0],
self.extrema[2] - self.extrema[2],
self.extrema[4] - self.extrema[4], 0)
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[0] - self.extrema[0],
self.extrema[3] - self.extrema[2],
self.extrema[4] - self.extrema[4], 0)
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[0] - self.extrema[0],
self.extrema[2] - self.extrema[2],
self.extrema[5] - self.extrema[4], 0)
# Create the bezier curve.
self.curve_data.append(bpy.data.curves.new('DataCurve', type='CURVE'))
self.curve_data[-1].dimensions = '3D'
self.curve_object.append(bpy.data.objects.new('ObjCurve', self.curve_data[-1]))
# Set the origin.
self.curve_object[-1].location = tuple((self.extrema[1], self.extrema[3], self.extrema[4]))
# Add the rest of the curve.
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[2] - self.extrema[1],
self.extrema[3] - self.extrema[3],
self.extrema[4] - self.extrema[4], 0)
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[1] - self.extrema[1],
self.extrema[2] - self.extrema[3],
self.extrema[4] - self.extrema[4], 0)
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[1] - self.extrema[1],
self.extrema[3] - self.extrema[3],
self.extrema[5] - self.extrema[4], 0)
# Create the bezier curve.
self.curve_data.append(bpy.data.curves.new('DataCurve', type='CURVE'))
self.curve_data[-1].dimensions = '3D'
self.curve_object.append(bpy.data.objects.new('ObjCurve', self.curve_data[-1]))
# Set the origin.
self.curve_object[-1].location = tuple((self.extrema[0], self.extrema[3], self.extrema[5]))
# Add the rest of the curve.
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[1] - self.extrema[0],
self.extrema[3] - self.extrema[3],
self.extrema[5] - self.extrema[5], 0)
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[0] - self.extrema[0],
self.extrema[2] - self.extrema[3],
self.extrema[5] - self.extrema[5], 0)
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[0] - self.extrema[0],
self.extrema[3] - self.extrema[3],
self.extrema[4] - self.extrema[5], 0)
# Create the bezier curve.
self.curve_data.append(bpy.data.curves.new('DataCurve', type='CURVE'))
self.curve_data[-1].dimensions = '3D'
self.curve_object.append(bpy.data.objects.new('ObjCurve', self.curve_data[-1]))
# Set the origin.
self.curve_object[-1].location = tuple((self.extrema[1], self.extrema[2], self.extrema[5]))
# Add the rest of the curve.
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[0] - self.extrema[1],
self.extrema[2] - self.extrema[2],
self.extrema[5] - self.extrema[5], 0)
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[1] - self.extrema[1],
self.extrema[3] - self.extrema[2],
self.extrema[5] - self.extrema[5], 0)
self.poly_line.append(self.curve_data[-1].splines.new('POLY'))
self.poly_line[-1].points.add(1)
self.poly_line[-1].points[0].co = (self.extrema[1] - self.extrema[1],
self.extrema[2] - self.extrema[2],
self.extrema[4] - self.extrema[5], 0)
# # Add 3d structure.
# self.curve_data.splines.data.bevel_depth = self._radius[0]
# self.curve_data.splines.data.bevel_resolution = self.resolution
# self.curve_data.splines.data.fill_mode = 'FULL'
#
# # Set the material/color.
# self.mesh_material = bpy.data.materials.new('material')
# self.mesh_material.diffuse_color = color_rgba[0]
# self.mesh_material.roughness = self.roughness
# self.curve_object.active_material = self.mesh_material
# Link the curve object with the scene.
for curve_object in self.curve_object:
bpy.context.scene.collection.objects.link(curve_object)
# Group the splines together.
for curve_object in self.curve_object[::-1]:
curve_object.select_set(state=True)
bpy.context.view_layer.objects.active = curve_object
bpy.ops.object.join()
self.curve_object = self.curve_object[0]
# Make this box the object to be deleted.
self.deletable_object = self.curve_object
return 0
def get_extrema(self):
"""
Get the extrema from the global structure.
"""
import blendaviz as blt
self.extrema = (blt.house_keeping.x_min, blt.house_keeping.x_max,
blt.house_keeping.y_min, blt.house_keeping.y_max,
blt.house_keeping.z_min, blt.house_keeping.z_max)