Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Numpy array to openvdb conversion returns a shuffled volume #1743

Open
Tomaubier opened this issue Dec 28, 2023 · 2 comments
Open

[BUG] Numpy array to openvdb conversion returns a shuffled volume #1743

Tomaubier opened this issue Dec 28, 2023 · 2 comments
Labels

Comments

@Tomaubier
Copy link

Tomaubier commented Dec 28, 2023

Environment

Operating System: macOS 14.1.1
Version: Blender 4.0.2

Issue

I have a 3d numpy array containing results from a simulation which I want to render using Blender. When visualized using napari I get the expected result:
actual_array_napari
Using pyopenvdb’s copyFromArray function, I’ve attempted to import the same array within blender.

import bpy, os
import pyopenvdb as vdb
import numpy as np

# --- Scenario 3 -> .npy import of the actual array ---
actual_vol_path = '/Users/tomaubier/actual_volume.npy'
volume = np.load(actual_vol_path)

# numpy array to openvdb object
vecgrid = vdb.DoubleGrid()
vecgrid.name = 'density'
vecgrid.copyFromArray(volume)

# Saving the openvdb object
vdb_file_path = f'/Users/tomaubier/openvdb_volume.vdb'
vdb.write(vdb_file_path, grids=[vecgrid])

# Importing the volume within blender
bpy.ops.object.volume_import(filepath=vdb_file_path, files=[])

However this code gives me what looks like a array of shuffled values.
actual_array_blender
I then made up an arbitrary array defined as

# Making up a MWE volume
x = np.linspace(0, 5*np.pi, x_shape)
y = np.linspace(0, np.pi, y_shape)
z = np.linspace(0, .2*np.pi, z_shape)
X, Y, Z = np.meshgrid(x, y, z)
mwe_volume_presave = np.cos(X*Y*Z)

and got the expected result:
mwe_volume_blender
To check whether or not saving and loading the values in a .npy file could be at the root of this issue, I rendered the same dummy array after saving and loading it in a numpy file. As expected this test led to the same rendered output.

I currently don’t have any clues as to whats going on.. Do you guys have any ideas?
If anyone wants to perform tests with the data I used, I've created a repo with the blender file and .npy array.
Best.

@Tomaubier Tomaubier added the bug label Dec 28, 2023
@Dxyk
Copy link

Dxyk commented Feb 24, 2024

Hi, I recently ran into the same problem. This might be a bit late, but I suspect there is an issue/implicit assumption with how OpenVDB copies data from Numpy arrays.

For what it's worth, what fixed it for me was making the array contiguous with np.ascontiguousarray before calling copyFromArray.

@Tomaubier
Copy link
Author

Awesome that solved the issue!
No idea why this would be necessary though..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants