Convert Numpy Array to VDB

   1084   2   0
User Avatar
Member
1 posts
Joined: July 2020
Offline
I created a python script which converts a VDB volume into a numpy array (See file). How can I reverse this process: How to create a VDB from a numpy array?

Thanks for any advice

Attachments:
try_houdini_voxel_vdb.hipnc (261.4 KB)

User Avatar
Member
92 posts
Joined: Aug. 2010
Offline
Sorry I'm late on the reply! But heads up there are VDB Python bindings (which do interface with Numpy).

import openvdb as vdb
import numpy

filename = '/work/stanford/Dragon.obj'

print 'reading: %s' % filename

points = []
triangles = []
quads = []

f = open(filename, 'r')
lines = f.readlines()

for line in lines:
    t = line.split()
    if t[0] == 'v':         # vertex
        points.append( [ float(t[1]), float(t[2]), float(t[3]) ] )
    elif t[0] == 'f':
        if len(t) == 4:     # triangle
            triangles.append( [ int(t[1])-1, int(t[2])-1, int(t[3])-1 ] )
        elif len(t) == 5:   # quad
            quads.append( [ int(t[1])-1, int(t[2])-1, int(t[3])-1, int(t[4])-1 ] )


np_points = numpy.ndarray((len(points), 3), dtype=float)
np_triangles = numpy.ndarray((len(triangles), 3), dtype=int)
np_quads = numpy.ndarray((len(quads), 4), dtype=int)

for i in range(0, len(points)):
    np_points[i] = points[i]

for i in range(0, len(triangles)):
    np_triangles[i] = triangles[i]

for i in range(0, len(quads)):
    np_quads[i] = quads[i]

xform = vdb.createLinearTransform(voxelSize=0.5)
grid = vdb.FloatGrid.createLevelSetFromPolygons(np_points, np_triangles, np_quads, transform=xform)
grid.name = 'surface'

print 'name:', grid.name, ' class:', grid.gridClass, ' background value:', grid.background, ' voxel size:', grid.transform.voxelSize()[0], ' active voxels:', grid.activeVoxelCount()

vdb.write('/tmp/foo.vdb', grid)
User Avatar
Member
92 posts
Joined: Aug. 2010
Offline
That said, given your example is Houdini Python SOP, not sure we have access to that library. I poked at this briefly--perhaps you could convert your Numpy array to Houdini points, then build the appropriate VDB grid topology (I gave a couple options) and sample the point value into the VDB grid.

Attachments:
tmp.jpg (181.9 KB)
try_houdini_voxel_vdb2.hipnc (291.9 KB)

  • Quick Links