Skip to content

Commit

Permalink
fixed density importing
Browse files Browse the repository at this point in the history
it seems that there is now a blender 4.1 issue where even if the underlying `.vdb` changes, if you reimport the file it still uses the preview for the 'old' version.

Because of this, the created .vdb file path now contains whether or not it was centred and inverted, which makes different files and fixes the issues.
  • Loading branch information
BradyAJohnston committed Apr 6, 2024
1 parent 05ae0e1 commit 383aa98
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 236 deletions.
14 changes: 9 additions & 5 deletions molecularnodes/io/density.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ def load(
invert: bool = False,
setup_nodes: bool = True,
style: str = 'density_surface',
center: bool = False
center: bool = False,
overwrite: bool = False
):
density = parse.MRC(file_path=file_path, center=center, invert=invert)
density = parse.MRC(
file_path=file_path,
center=center,
invert=invert,
overwrite=overwrite
)
density.create_model(
name=name,
invert=invert,
setup_nodes=setup_nodes,
style=style,
center=center
style=style
)
return density

Expand Down
4 changes: 3 additions & 1 deletion molecularnodes/io/parse/density.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, file_path):
self.threshold: float = None
self.object: bpy.types.Object = None

def path_to_vdb(self, file: str):
def path_to_vdb(self, file: str, center: False, invert: False):
"""
Convert a file path to a corresponding VDB file path.
Expand All @@ -34,6 +34,8 @@ def path_to_vdb(self, file: str):
# Set up file paths
folder_path = os.path.dirname(file)
name = os.path.basename(file).split(".")[0]
name += "_center" if center else ""
name += "_invert" if invert else ""
file_name = name + '.vdb'
file_path = os.path.join(folder_path, file_name)
return file_path
54 changes: 34 additions & 20 deletions molecularnodes/io/parse/mrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ class MRC(Density):
that can be written as `.vdb` files and the imported into Blender as volumetric objects.
"""

def __init__(self, file_path, center=False, invert=False):
def __init__(self, file_path, center=False, invert=False, overwrite=False):
super().__init__(self)
self.file_path = file_path
self.grid = self.map_to_grid(self.file_path, center=center)
self.file_vdb = self.map_to_vdb(
self.file_path, center=center, invert=invert)
self.file_path,
center=center,
invert=invert,
overwrite=overwrite
)

def create_model(
self,
name='NewDensity',
style='density_surface',
setup_nodes=True,
invert: bool = False,
center: bool = False,
world_scale: float = 0.01
setup_nodes=True
) -> bpy.types.Object:
"""
Loads an MRC file into Blender as a volumetric object.
Expand All @@ -39,20 +40,16 @@ def create_model(
Path to the MRC file.
name : str, optional
If not None, renames the object with the new name.
invert : bool, optional
Whether to invert the data from the grid, defaulting to False. Some file types
such as EM tomograms have inverted values, where a high value == low density.
world_scale : float, optional
Scale of the object in the world. Defaults to 0.01.
center : bool, optional
Whether to center the volume on the origin. Defaults to False.
Returns
-------
bpy.types.Object
The loaded volumetric object.
"""
# import and ensure object is at world origin to get corect alignment with
# structures
object = obj.import_vdb(self.file_vdb, collection=coll.mn())
object.location = (0, 0, 0)
self.object = object
object.mn['molecule_type'] = 'density'

Expand All @@ -62,7 +59,10 @@ def create_model(

if setup_nodes:
nodes.create_starting_nodes_density(
object, style=style, threshold=self.threshold)
object=object,
style=style,
threshold=self.threshold
)

return object

Expand Down Expand Up @@ -98,7 +98,7 @@ def map_to_vdb(
"""
import pyopenvdb as vdb

file_path = self.path_to_vdb(file)
file_path = self.path_to_vdb(file, center=center, invert=invert)

# If the map has already been converted to a .vdb and overwrite is False, return that instead
if os.path.exists(file_path) and not overwrite:
Expand All @@ -108,18 +108,32 @@ def map_to_vdb(
self.threshold = grid['MN_initial_threshold']
return file_path

print("Reading new file")
# Read in the MRC file and convert it to a pyopenvdb grid
grid = self.map_to_grid(file, invert=invert, center=center)
grid = self.map_to_grid(
file=file,
invert=invert,
center=center
)

grid.transform.scale(
np.array((1, 1, 1)) * world_scale * grid['MN_voxel_size']
)

grid.transform.scale(np.array((1, 1, 1)) *
world_scale * grid['MN_voxel_size'])
if center:
grid.transform.translate(-np.array(grid['MN_box_size'])
* 0.5 * world_scale * grid['MN_voxel_size'])
offset = -np.array(grid['MN_box_size']) * 0.5
offset *= grid['MN_voxel_size'] * world_scale
print("transforming")
grid.transform.translate(offset)

if os.path.exists(file_path):
os.remove(file_path)

Check warning on line 130 in molecularnodes/io/parse/mrc.py

View check run for this annotation

Codecov / codecov/patch

molecularnodes/io/parse/mrc.py#L130

Added line #L130 was not covered by tests

# Write the grid to a .vdb file
print('writing new file')
vdb.write(file_path, grids=[grid])
self.threshold = grid['MN_initial_threshold']
del grid

# Return the path to the output file
return file_path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[[0.84 0.36 0.808 1. ]
[[0.725 0.36 0.84 1. ]
[0.84 0.747 0.36 1. ]
[0.84 0.36 0.487 1. ]
[0.635 0.84 0.36 1. ]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,100 +1,100 @@
[[ 1.198 -1.013 0.382]
[-1.062 1.198 0.813]
[ 1.004 -0.996 1.198]
[-1.198 0.141 -0.456]
[-0.888 -0.689 1.198]
[ 1.198 -1.004 -0.656]
[-0.083 -1.198 -0.025]
[-1.198 -1.013 0.141]
[-1.195 -1.195 0.764]
[-1.198 1.038 -0.166]
[ 0.365 0.456 1.198]
[-0.614 1.198 0.282]
[ 1.054 0.83 -1.198]
[ 1.198 -1.096 -0.921]
[ 0.697 1.198 0.307]
[-0.365 0.091 -1.198]
[-1.129 -0.515 -1.198]
[ 1.198 -0.747 -0.573]
[-1.198 -0.506 -0.498]
[ 0.232 -1.198 0.697]
[-0.739 -0.789 1.198]
[ 1.198 -0.73 0.78 ]
[-0.963 -1.198 0.473]
[ 0.498 1.198 0.747]
[ 1.198 0.091 0.008]
[ 0.739 -1.046 -1.198]
[ 1.198 0.639 1.179]
[-0.133 0.647 1.198]
[-1.145 1.198 -0.946]
[-0.058 1.198 -0.772]
[-1.198 0.656 -0.523]
[-1.162 -1.198 0.888]
[-0.349 1.198 -0.556]
[-0.697 1.198 -0.589]
[ 0.548 1.198 -0.838]
[ 1.198 -0.647 0.465]
[ 0.515 1.198 0.257]
[ 0.05 -1.198 -0.058]
[ 0.349 0.481 1.198]
[ 1.198 -0.672 0.515]
[-0.539 0.963 1.198]
[ 0.124 1.198 -0.946]
[-1.198 0.456 -0.647]
[ 1.198 0.141 -0.44 ]
[ 0.041 0.531 -1.198]
[-0.623 -0.158 -1.198]
[-1.198 0.664 -1.087]
[-0.78 0.473 -1.198]
[ 1.198 -0.755 -0.083]
[ 0.656 -1.187 -1.198]
[ 1.198 -0.631 0.241]
[ 0.564 -1.198 -0.739]
[ 0.78 -0.041 -1.198]
[ 0.598 1.198 -0.813]
[-1.198 0.614 -0.672]
[ 0.805 1.087 1.198]
[-0.946 -1.198 -0.058]
[ 0.548 1.198 -0.158]
[ 0.473 -0.896 1.198]
[ 1.198 -0.872 -0.066]
[ 0.921 -1.198 0.133]
[-1.198 0.739 -0.73 ]
[-1.198 -0.78 0.788]
[-0.548 1.198 0.398]
[ 1.071 0.598 1.198]
[-0.855 -1.198 -0.108]
[ 1.195 -0.971 1.195]
[ 0.116 0.664 1.198]
[ 1.198 0.88 1.096]
[-0.813 0.581 -1.198]
[-0.847 -0.797 1.198]
[ 1.112 1.198 0.083]
[ 1.198 -0.407 1.179]
[ 1.198 0.581 0.938]
[-1.096 -1.198 0.249]
[-0.979 0.573 1.198]
[-1.004 1.198 0.896]
[ 0.813 -0.988 1.198]
[ 0.531 1.198 -0.681]
[ 1.198 0.755 0.224]
[ 1.198 -0.639 0.664]
[-0.232 -1.179 1.198]
[ 1.062 1.198 -0.49 ]
[-1.198 -0.274 -0.158]
[ 0.423 -0.656 1.198]
[-0.73 0.008 -1.198]
[ 0.382 -1.198 -0.639]
[-1.145 1.198 0.963]
[ 0.232 0.066 -1.198]
[ 1.038 -1.198 -0.191]
[-0.913 0.008 -1.198]
[-0.556 1.198 0.689]
[-1.198 0.44 0.598]
[-1.198 0.515 -0.813]
[-0.365 -0.946 1.198]
[ 1.198 0.357 -0.116]
[ 0.93 0.481 1.198]
[-0.581 -0.058 1.198]
[-0.88 -1.198 -0.008]
[-1.198 0.34 0.141]]
[[1.195 1.529 1.23 ]
[0.888 0.963 1.462]
[1.221 1.511 0.953]
[1.317 0.705 0.993]
[0.793 0.865 1.295]
[0.901 0.8 0.959]
[1.029 0.883 1.141]
[1.324 1.278 1.383]
[1.193 1.857 1.227]
[1.161 1.577 0.949]
[1.345 1.841 1.301]
[1.163 0.938 1.388]
[1.121 0.855 1.212]
[1.057 0.692 1.038]
[1.275 0.876 0.983]
[1.211 1.618 1.029]
[1.06 1.582 1.115]
[1.391 1.544 1.425]
[1.234 1.016 1.415]
[1.095 1.445 1.093]
[1.106 1.442 1.176]
[1.287 1.61 1.31 ]
[0.967 0.743 1.1 ]
[1.033 1.216 1.275]
[1.189 1.308 1.215]
[1.188 1.195 1.104]
[1.321 2.055 1.283]
[1.382 1.557 1.349]
[1.27 0.863 0.846]
[1.27 1.356 1.087]
[1.226 1.404 1.126]
[1.303 1.745 1.145]
[0.939 0.631 1.145]
[1.274 0.884 1.283]
[1.112 0.985 1.419]
[0.988 0.728 1.293]
[1.179 0.914 1.136]
[1.236 1.02 1.096]
[1.264 0.919 0.791]
[1.358 1.013 1.074]
[1.27 2.09 1.198]
[0.946 0.766 1.085]
[1.506 1.474 1.149]
[1.066 1.499 1.15 ]
[1.383 1.067 1.09 ]
[1.254 1.848 1.245]
[1.332 0.926 1.017]
[0.963 0.765 1.27 ]
[0.833 0.838 1.259]
[1.185 1.964 1.177]
[1.116 0.595 0.868]
[1.278 1.261 1.262]
[1.171 0.853 1.486]
[1.22 1.565 0.917]
[1.009 1.216 1.456]
[1.361 1.502 1.061]
[1.178 1.542 1.195]
[1.241 2.087 1.166]
[1.3 2.005 1.099]
[1.272 1.561 0.97 ]
[1.018 0.811 0.971]
[1.223 1.801 1.193]
[1.195 0.896 1.113]
[1.253 0.741 0.941]
[1.204 1.296 1.071]
[1.311 0.996 1.161]
[1.079 0.606 1.139]
[1.179 0.84 1.286]
[1.179 2.006 1.184]
[0.999 1.032 1.298]
[0.963 0.631 1.228]
[1.283 1.44 1.067]
[1.269 1.569 1.387]
[1.077 1.162 1.171]
[1.04 1.555 1.248]
[0.968 0.954 1.447]
[1.215 1.336 1.237]
[1.323 1.278 1.383]
[1.294 0.857 0.938]
[1.077 0.755 1.243]
[1.374 1.407 1.283]
[1.052 1.437 1.163]
[1.133 1.042 1.455]
[1.212 1.45 1.063]
[1.136 0.681 0.865]
[1.053 0.764 1.426]
[1.206 1.253 1.114]
[1.104 0.777 0.849]
[1.278 2.03 1.184]
[1.076 1.116 1.354]
[0.95 0.911 0.93 ]
[1.055 1.137 1.137]
[0.932 1.177 1.155]
[1.048 0.647 0.913]
[1.331 1.071 1.326]
[1.099 0.984 1.286]
[1.145 1.253 1.159]
[1.275 1.472 1.148]
[1.224 1.547 1.299]
[1.237 1.573 1.158]]
Loading

0 comments on commit 383aa98

Please sign in to comment.