AcademySoftwareFoundation / AcademySoftwareFoundation/openvdb

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

Open
#1,743 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
3.4k
Forks
774
Avg merge
3d 9h
Merged PRs (30d)
34

Description

### 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](https://napari.org/stable/) I get the expected result:
actual_array_napari
Using `pyopenvdb`’s `copyFromArray` function, I’ve attempted to import the same array within blender.
```python
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
```python
# 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](https://github.com/Tomaubier/OpenVDB_numpy_issue_MWE) with the blender file and .npy array.
Best.

Contributor guide

Open the contributing guide

Research direction

Start with the linked OpenVDB_numpy_issue_MWE repository, its Blender file, and the .npy array; reproduce the discrepancy using pyopenvdb.DoubleGrid.copyFromArray and Blender's volume import shown here. Done means identifying why the imported volume differs from the napari visualization and documenting or correcting the relevant array-order behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
api, computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.