-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathOpenVDB.py
More file actions
327 lines (252 loc) · 9.49 KB
/
OpenVDB.py
File metadata and controls
327 lines (252 loc) · 9.49 KB
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
r"""
:mod:`~gridData.OpenVDB` --- routines to write OpenVDB files
=============================================================
The `OpenVDB format`_ is used by Blender_ and other VFX software for
volumetric data.
.. _`OpenVDB format`: https://www.openvdb.org
.. _Blender: https://www.blender.org/
This module uses the openvdb_ library to write OpenVDB files.
.. _openvdb: https://github.com/AcademySoftwareFoundation/openvdb
.. Note:: This module implements a simple writer for 3D regular grids,
sufficient to export density data for visualization in Blender_.
See the `Blender volume docs`_ for details on importing VDB files.
.. _`Blender volume docs`: https://docs.blender.org/manual/en/latest/modeling/volumes/introduction.html
The OpenVDB format uses a sparse tree structure to efficiently store
volumetric data. It is the native format for Blender's volume system.
Writing OpenVDB files
---------------------
If you have a :class:`~gridData.core.Grid` object, you can write it to
OpenVDB format::
from gridData import Grid
g = Grid("data.dx")
g.export("data.vdb")
This will create a file that can be imported directly into Blender
(File -> Import -> OpenVDB) or (shift+A -> Volume -> Import OpenVDB). See `importing VDB in Blender`_ for details.
.. _`importing VDB in Blender`: https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/input/import/vdb.html
Building an OpenVDB field from a numpy array
---------------------------------------------
If you want to create VDB files without using the Grid class,
you can directly use the OpenVDB field API. This is useful
for custom workflows or when integrating with other libraries.
Requires:
grid
numpy 3D array
origin
cartesian coordinates of the center of the (0,0,0) grid cell
delta
n x n array with the length of a grid cell along each axis
Example::
from gridData import Grid
g = Grid("data.dx")
g.export("data.vdb")
Classes and functions
---------------------
"""
import numpy
import warnings
from dataclasses import dataclass
try:
import openvdb as vdb
except ImportError:
vdb = None
@dataclass
class DownCastTo:
gridType: str
class OpenVDBField(object):
"""OpenVDB field object for writing volumetric data.
This class provides a simple interface to write 3D grid data to
OpenVDB format, which can be imported into Blender and other
VFX software.
The field object holds grid data and metadata, and can write it
to a .vdb file.
Example
-------
Create a field and write it::
import gridData.OpenVDB as OpenVDB
vdb_field = OpenVDB.OpenVDBField('density')
vdb_field.write('output.vdb')
Or use directly from Grid::
g = Grid(...)
g.export('output.vdb', format='vdb')
"""
def __init__(
self,
grid=None,
origin=None,
delta=None,
name="density",
tolerance=None,
metadata=None,
):
"""Initialize an OpenVDB field.
Parameters
----------
grid : numpy.ndarray
3D numpy array with the data
origin : numpy.ndarray
Coordinates of the center of grid cell [0,0,0]
delta : numpy.ndarray
Grid spacing (can be 1D array or diagonal matrix)
name : str
Name of the grid (will be visible in Blender), default 'density'
tolerance : float (optional)
Values below this tolerance are treated as background (sparse),
default None
metadata : dict (optional)
Additional metadata to embed in the VDB file.
Raises
------
ImportError
If openvdb is not installed
ValueError
If grid is not 3D, or if delta is not 1D/2D or describes
non-orthorhombic cell
"""
if vdb is None:
raise ImportError(
"openvdb is required to write VDB files. "
"Install it with: conda install -c conda-forge openvdb"
)
self.name = name
self.tolerance = tolerance
if metadata is not None:
self.metadata = metadata
else:
self.metadata = {}
if grid is not None:
self._populate(grid, origin, delta)
self.vdb_grid = self._create_openvdb_grid()
else:
self.grid = None
self.origin = None
self.delta = None
self.vdb_grid = None
def _populate(self, grid, origin, delta):
"""Populate the field with grid data.
Parameters
----------
grid : numpy.ndarray
3D numpy array with the data
origin : numpy.ndarray
Coordinates of the center of grid cell [0,0,0]
delta : numpy.ndarray
Grid spacing (can be 1D array or diagonal matrix)
Raises
------
ValueError
If grid is not 3D, or if delta is not 1D/2D or describes
non-orthorhombic cell
"""
self.grid = numpy.asarray(grid)
if grid.ndim != 3:
raise ValueError(f"OpenVDB only supports 3D grids, got {grid.ndim}D")
self.grid = numpy.ascontiguousarray(self.grid)
self.origin = numpy.asarray(origin)
# Handle delta: could be 1D array or diagonal matrix
delta = numpy.asarray(delta)
if delta.ndim == 2:
if delta.shape != (3, 3):
raise ValueError("delta as a matrix must be 3x3")
if not numpy.allclose(delta, numpy.diag(numpy.diag(delta))):
raise ValueError("Non-orthorhombic cells are not supported")
self.delta = numpy.diag(delta)
elif delta.ndim == 1:
if len(delta) != 3:
raise ValueError("delta must have length-3 for 3D grids")
self.delta = delta
else:
raise ValueError(
"delta must be either a length-3 vector or a 3x3 diagonal matrix"
)
def _get_best_grid_type(self):
"""Selects the suitable OpenVDB grid type
Returns
-------
openvdb.GridBase
Raises
------
TypeError
If dtype is not supported or no suitable grid type is available
"""
datatypes = {
numpy.dtype("bool"): ["BoolGrid"],
numpy.dtype("int8"): ["Int32Grid", "FloatGrid"],
numpy.dtype("uint8"): ["Int32Grid", "FloatGrid"],
numpy.dtype("int16"): ["Int32Grid", "FloatGrid"],
numpy.dtype("uint16"): ["Int32Grid", "FloatGrid"],
numpy.dtype("int32"): ["Int32Grid", DownCastTo("FloatGrid")],
numpy.dtype("uint32"): [DownCastTo("Int32Grid"), DownCastTo("FloatGrid")],
numpy.dtype("int64"): ["Int64Grid", DownCastTo("FloatGrid")],
numpy.dtype("uint64"): ["Int64Grid", DownCastTo("FloatGrid")],
numpy.dtype("float16"): ["HalfGrid", "FloatGrid"],
numpy.dtype("float32"): ["FloatGrid"],
numpy.dtype("float64"): ["DoubleGrid", DownCastTo("FloatGrid")],
}
try:
vdb_gridtypes = datatypes[self.grid.dtype]
except KeyError:
raise TypeError(f"Data type {self.grid.dtype} not supported for VDB")
VDB_Grid = None
selected_gridtype = None
is_downcast = False
for gridtype in vdb_gridtypes:
if isinstance(gridtype, DownCastTo):
gridtype_name = gridtype.gridType
is_downcast = True
else:
gridtype_name = gridtype
is_downcast = False
try:
VDB_Grid = getattr(vdb, gridtype_name)
except AttributeError:
continue
else:
selected_gridtype = gridtype_name
break
else:
raise TypeError(
f"Could not find any VDB grid type for numpy dtype {self.grid.dtype}"
)
if is_downcast:
warnings.warn(
f"Grid type {vdb_gridtypes[0]} not available. Using {selected_gridtype} instead. Data may lose precision.",
RuntimeWarning,
)
return VDB_Grid()
def _create_openvdb_grid(self):
"""Create and populate an OpenVDB grid
Returns
-------
openvdb.GridBase
"""
vdb_grid = self._get_best_grid_type()
vdb_grid.name = self.name
vdb_grid.transform = vdb.createLinearTransform()
vdb_grid.transform.preScale(self.delta.tolist())
vdb_grid.transform.postTranslate(self.origin.tolist())
if self.metadata:
for key, val in self.metadata.items():
try:
vdb_grid[key] = val
except (TypeError, ValueError) as e:
warnings.warn(f"Could not set metadata '{key}': {e}", UserWarning)
if isinstance(vdb_grid, vdb.BoolGrid) and (
self.tolerance is None or self.tolerance == 0
):
vdb_grid.copyFromArray(self.grid)
vdb_grid.prune(tolerance=False)
else:
if self.tolerance is None:
vdb_grid.copyFromArray(self.grid)
else:
vdb_grid.copyFromArray(self.grid, tolerance=self.tolerance)
vdb_grid.prune()
return vdb_grid
def write(self, filename):
"""Write the field to an OpenVDB file.
Parameters
----------
filename : str
Output filename (should end in .vdb)
"""
vdb.write(filename, grids=[self.vdb_grid])