fix: io
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import bpy
|
import bpy
|
||||||
|
|
||||||
from .nodes import custom_nodes
|
|
||||||
from . import auto_load
|
from . import auto_load
|
||||||
|
from . import menus
|
||||||
|
|
||||||
|
|
||||||
bl_info = {
|
bl_info = {
|
||||||
@@ -9,7 +9,7 @@ bl_info = {
|
|||||||
"author": "Ma Nan",
|
"author": "Ma Nan",
|
||||||
"description": "",
|
"description": "",
|
||||||
"blender": (4, 0, 0),
|
"blender": (4, 0, 0),
|
||||||
"version": (0, 1, 0),
|
"version": (0, 1, 1),
|
||||||
"location": "File -> Import",
|
"location": "File -> Import",
|
||||||
"warning": "",
|
"warning": "",
|
||||||
"category": "Node"
|
"category": "Node"
|
||||||
@@ -20,6 +20,7 @@ auto_load.init()
|
|||||||
|
|
||||||
def register():
|
def register():
|
||||||
auto_load.register()
|
auto_load.register()
|
||||||
|
menus.add()
|
||||||
bpy.types.Scene.bioxels_dir = bpy.props.StringProperty(
|
bpy.types.Scene.bioxels_dir = bpy.props.StringProperty(
|
||||||
name="Bioxels Directory",
|
name="Bioxels Directory",
|
||||||
subtype='DIR_PATH',
|
subtype='DIR_PATH',
|
||||||
@@ -28,7 +29,5 @@ def register():
|
|||||||
|
|
||||||
|
|
||||||
def unregister():
|
def unregister():
|
||||||
try:
|
menus.remove()
|
||||||
auto_load.unregister()
|
auto_load.unregister()
|
||||||
except RuntimeError:
|
|
||||||
pass
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ blender_version = bpy.app.version
|
|||||||
modules = None
|
modules = None
|
||||||
ordered_classes = None
|
ordered_classes = None
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
global modules
|
global modules
|
||||||
global ordered_classes
|
global ordered_classes
|
||||||
@@ -23,6 +24,7 @@ def init():
|
|||||||
modules = get_all_submodules(Path(__file__).parent)
|
modules = get_all_submodules(Path(__file__).parent)
|
||||||
ordered_classes = get_ordered_classes_to_register(modules)
|
ordered_classes = get_ordered_classes_to_register(modules)
|
||||||
|
|
||||||
|
|
||||||
def register():
|
def register():
|
||||||
for cls in ordered_classes:
|
for cls in ordered_classes:
|
||||||
bpy.utils.register_class(cls)
|
bpy.utils.register_class(cls)
|
||||||
@@ -33,15 +35,22 @@ def register():
|
|||||||
if hasattr(module, "register"):
|
if hasattr(module, "register"):
|
||||||
module.register()
|
module.register()
|
||||||
|
|
||||||
|
|
||||||
def unregister():
|
def unregister():
|
||||||
for cls in reversed(ordered_classes):
|
for cls in reversed(ordered_classes):
|
||||||
bpy.utils.unregister_class(cls)
|
try:
|
||||||
|
bpy.utils.unregister_class(cls)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Fail to unregister {cls}, skiped")
|
||||||
|
|
||||||
for module in modules:
|
for module in modules:
|
||||||
if module.__name__ == __name__:
|
try:
|
||||||
continue
|
if module.__name__ == __name__:
|
||||||
if hasattr(module, "unregister"):
|
continue
|
||||||
module.unregister()
|
if hasattr(module, "unregister"):
|
||||||
|
module.unregister()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Fail to unregister {module}, skiped")
|
||||||
|
|
||||||
|
|
||||||
# Import modules
|
# Import modules
|
||||||
@@ -50,10 +59,12 @@ def unregister():
|
|||||||
def get_all_submodules(directory):
|
def get_all_submodules(directory):
|
||||||
return list(iter_submodules(directory, directory.name))
|
return list(iter_submodules(directory, directory.name))
|
||||||
|
|
||||||
|
|
||||||
def iter_submodules(path, package_name):
|
def iter_submodules(path, package_name):
|
||||||
for name in sorted(iter_submodule_names(path)):
|
for name in sorted(iter_submodule_names(path)):
|
||||||
yield importlib.import_module("." + name, package_name)
|
yield importlib.import_module("." + name, package_name)
|
||||||
|
|
||||||
|
|
||||||
def iter_submodule_names(path, root=""):
|
def iter_submodule_names(path, root=""):
|
||||||
for _, module_name, is_package in pkgutil.iter_modules([str(path)]):
|
for _, module_name, is_package in pkgutil.iter_modules([str(path)]):
|
||||||
if is_package:
|
if is_package:
|
||||||
@@ -70,19 +81,24 @@ def iter_submodule_names(path, root=""):
|
|||||||
def get_ordered_classes_to_register(modules):
|
def get_ordered_classes_to_register(modules):
|
||||||
return toposort(get_register_deps_dict(modules))
|
return toposort(get_register_deps_dict(modules))
|
||||||
|
|
||||||
|
|
||||||
def get_register_deps_dict(modules):
|
def get_register_deps_dict(modules):
|
||||||
my_classes = set(iter_my_classes(modules))
|
my_classes = set(iter_my_classes(modules))
|
||||||
my_classes_by_idname = {cls.bl_idname : cls for cls in my_classes if hasattr(cls, "bl_idname")}
|
my_classes_by_idname = {
|
||||||
|
cls.bl_idname: cls for cls in my_classes if hasattr(cls, "bl_idname")}
|
||||||
|
|
||||||
deps_dict = {}
|
deps_dict = {}
|
||||||
for cls in my_classes:
|
for cls in my_classes:
|
||||||
deps_dict[cls] = set(iter_my_register_deps(cls, my_classes, my_classes_by_idname))
|
deps_dict[cls] = set(iter_my_register_deps(
|
||||||
|
cls, my_classes, my_classes_by_idname))
|
||||||
return deps_dict
|
return deps_dict
|
||||||
|
|
||||||
|
|
||||||
def iter_my_register_deps(cls, my_classes, my_classes_by_idname):
|
def iter_my_register_deps(cls, my_classes, my_classes_by_idname):
|
||||||
yield from iter_my_deps_from_annotations(cls, my_classes)
|
yield from iter_my_deps_from_annotations(cls, my_classes)
|
||||||
yield from iter_my_deps_from_parent_id(cls, my_classes_by_idname)
|
yield from iter_my_deps_from_parent_id(cls, my_classes_by_idname)
|
||||||
|
|
||||||
|
|
||||||
def iter_my_deps_from_annotations(cls, my_classes):
|
def iter_my_deps_from_annotations(cls, my_classes):
|
||||||
for value in typing.get_type_hints(cls, {}, {}).values():
|
for value in typing.get_type_hints(cls, {}, {}).values():
|
||||||
dependency = get_dependency_from_annotation(value)
|
dependency = get_dependency_from_annotation(value)
|
||||||
@@ -90,6 +106,7 @@ def iter_my_deps_from_annotations(cls, my_classes):
|
|||||||
if dependency in my_classes:
|
if dependency in my_classes:
|
||||||
yield dependency
|
yield dependency
|
||||||
|
|
||||||
|
|
||||||
def get_dependency_from_annotation(value):
|
def get_dependency_from_annotation(value):
|
||||||
if blender_version >= (2, 93):
|
if blender_version >= (2, 93):
|
||||||
if isinstance(value, bpy.props._PropertyDeferred):
|
if isinstance(value, bpy.props._PropertyDeferred):
|
||||||
@@ -100,6 +117,7 @@ def get_dependency_from_annotation(value):
|
|||||||
return value[1]["type"]
|
return value[1]["type"]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def iter_my_deps_from_parent_id(cls, my_classes_by_idname):
|
def iter_my_deps_from_parent_id(cls, my_classes_by_idname):
|
||||||
if bpy.types.Panel in cls.__bases__:
|
if bpy.types.Panel in cls.__bases__:
|
||||||
parent_idname = getattr(cls, "bl_parent_id", None)
|
parent_idname = getattr(cls, "bl_parent_id", None)
|
||||||
@@ -108,6 +126,7 @@ def iter_my_deps_from_parent_id(cls, my_classes_by_idname):
|
|||||||
if parent_cls is not None:
|
if parent_cls is not None:
|
||||||
yield parent_cls
|
yield parent_cls
|
||||||
|
|
||||||
|
|
||||||
def iter_my_classes(modules):
|
def iter_my_classes(modules):
|
||||||
base_types = get_register_base_types()
|
base_types = get_register_base_types()
|
||||||
for cls in get_classes_in_modules(modules):
|
for cls in get_classes_in_modules(modules):
|
||||||
@@ -115,6 +134,7 @@ def iter_my_classes(modules):
|
|||||||
if not getattr(cls, "is_registered", False):
|
if not getattr(cls, "is_registered", False):
|
||||||
yield cls
|
yield cls
|
||||||
|
|
||||||
|
|
||||||
def get_classes_in_modules(modules):
|
def get_classes_in_modules(modules):
|
||||||
classes = set()
|
classes = set()
|
||||||
for module in modules:
|
for module in modules:
|
||||||
@@ -122,11 +142,13 @@ def get_classes_in_modules(modules):
|
|||||||
classes.add(cls)
|
classes.add(cls)
|
||||||
return classes
|
return classes
|
||||||
|
|
||||||
|
|
||||||
def iter_classes_in_module(module):
|
def iter_classes_in_module(module):
|
||||||
for value in module.__dict__.values():
|
for value in module.__dict__.values():
|
||||||
if inspect.isclass(value):
|
if inspect.isclass(value):
|
||||||
yield value
|
yield value
|
||||||
|
|
||||||
|
|
||||||
def get_register_base_types():
|
def get_register_base_types():
|
||||||
return set(getattr(bpy.types, name) for name in [
|
return set(getattr(bpy.types, name) for name in [
|
||||||
"Panel", "Operator", "PropertyGroup", "FileHandler",
|
"Panel", "Operator", "PropertyGroup", "FileHandler",
|
||||||
@@ -151,5 +173,6 @@ def toposort(deps_dict):
|
|||||||
sorted_values.add(value)
|
sorted_values.add(value)
|
||||||
else:
|
else:
|
||||||
unsorted.append(value)
|
unsorted.append(value)
|
||||||
deps_dict = {value : deps_dict[value] - sorted_values for value in unsorted}
|
deps_dict = {value: deps_dict[value] -
|
||||||
|
sorted_values for value in unsorted}
|
||||||
return sorted_list
|
return sorted_list
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from pathlib import Path
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
import mathutils
|
import mathutils
|
||||||
import math
|
import math
|
||||||
from .utils import calc_bbox_verts, get_text_index, get_bioxels_obj, get_node_by_type, show_message
|
from .utils import calc_bbox_verts, get_text_index_str, get_bioxels_obj, get_node_by_type, show_message
|
||||||
from .nodes import custom_nodes
|
from .nodes import custom_nodes
|
||||||
from .props import BIOXELNODES_Series
|
from .props import BIOXELNODES_Series
|
||||||
try:
|
try:
|
||||||
@@ -52,30 +52,16 @@ FH_EXTS = ['.dcm', '.DICOM',
|
|||||||
|
|
||||||
|
|
||||||
def get_bioxels_shape(bioxel_size: float, orig_shape: tuple, orig_spacing: tuple):
|
def get_bioxels_shape(bioxel_size: float, orig_shape: tuple, orig_spacing: tuple):
|
||||||
return (
|
shape = (int(orig_shape[0] / bioxel_size * orig_spacing[0]),
|
||||||
math.ceil(orig_shape[0] / bioxel_size * orig_spacing[0]),
|
int(orig_shape[1] / bioxel_size * orig_spacing[1]),
|
||||||
math.ceil(orig_shape[1] / bioxel_size * orig_spacing[1]),
|
int(orig_shape[2] / bioxel_size * orig_spacing[2]))
|
||||||
math.ceil(orig_shape[2] / bioxel_size * orig_spacing[2]),
|
|
||||||
)
|
return (shape[0] if shape[0] > 0 else 1,
|
||||||
|
shape[1] if shape[1] > 0 else 1,
|
||||||
|
shape[2] if shape[2] > 0 else 1)
|
||||||
|
|
||||||
|
|
||||||
def collect_image_sequence(filepath: str):
|
def get_ext(filepath: str) -> str:
|
||||||
file_path = Path(filepath).resolve()
|
|
||||||
|
|
||||||
files = list(file_path.parent.iterdir())
|
|
||||||
files = [f for f in files if f.is_file()
|
|
||||||
and get_ext(file_path) == get_ext(f)
|
|
||||||
and get_text_index(f.stem)]
|
|
||||||
|
|
||||||
def get_index(f: Path):
|
|
||||||
return get_text_index(f.stem)
|
|
||||||
|
|
||||||
files.sort(key=get_index)
|
|
||||||
sequence = [str(f) for f in files]
|
|
||||||
return sequence
|
|
||||||
|
|
||||||
|
|
||||||
def get_ext(filepath: str):
|
|
||||||
file_path = Path(filepath)
|
file_path = Path(filepath)
|
||||||
if file_path.name.endswith(".nii.gz"):
|
if file_path.name.endswith(".nii.gz"):
|
||||||
return ".nii.gz"
|
return ".nii.gz"
|
||||||
@@ -87,11 +73,38 @@ def get_ext(filepath: str):
|
|||||||
return file_path.suffix
|
return file_path.suffix
|
||||||
|
|
||||||
|
|
||||||
|
def get_sequence_name(filepath: str) -> str:
|
||||||
|
ext = get_ext(filepath)
|
||||||
|
filename = Path(filepath).name.removesuffix(ext)
|
||||||
|
index: str = get_text_index_str(filename)
|
||||||
|
return filename.removesuffix(index)
|
||||||
|
|
||||||
|
|
||||||
|
def get_sequence_index(filepath: str) -> int:
|
||||||
|
ext = get_ext(filepath)
|
||||||
|
filename = Path(filepath).name.removesuffix(ext)
|
||||||
|
index: str = get_text_index_str(filename)
|
||||||
|
return int(index) if index else 0
|
||||||
|
|
||||||
|
|
||||||
|
def collect_image_sequence(filepath: str):
|
||||||
|
file_path = Path(filepath).resolve()
|
||||||
|
|
||||||
|
files = list(file_path.parent.iterdir())
|
||||||
|
files = [f for f in files if f.is_file()
|
||||||
|
and get_ext(file_path) == get_ext(f)
|
||||||
|
and get_sequence_name(file_path) == get_sequence_name(f)]
|
||||||
|
|
||||||
|
files.sort(key=get_sequence_index)
|
||||||
|
sequence = [str(f) for f in files]
|
||||||
|
return sequence
|
||||||
|
|
||||||
|
|
||||||
def read_image(filepath: str, series_id=""):
|
def read_image(filepath: str, series_id=""):
|
||||||
ext = get_ext(filepath)
|
ext = get_ext(filepath)
|
||||||
dir_path = Path(filepath).resolve().parent
|
|
||||||
filename = Path(filepath).resolve().stem
|
|
||||||
if ext in DICOM_EXTS:
|
if ext in DICOM_EXTS:
|
||||||
|
dir_path = Path(filepath).resolve().parent
|
||||||
reader = sitk.ImageSeriesReader()
|
reader = sitk.ImageSeriesReader()
|
||||||
reader.MetaDataDictionaryArrayUpdateOn()
|
reader.MetaDataDictionaryArrayUpdateOn()
|
||||||
reader.LoadPrivateTagsOn()
|
reader.LoadPrivateTagsOn()
|
||||||
@@ -99,19 +112,36 @@ def read_image(filepath: str, series_id=""):
|
|||||||
str(dir_path), series_id)
|
str(dir_path), series_id)
|
||||||
reader.SetFileNames(series_files)
|
reader.SetFileNames(series_files)
|
||||||
image = reader.Execute()
|
image = reader.Execute()
|
||||||
elif ext in SEQUENCE_EXTS and get_text_index(filename):
|
name = dir_path.name
|
||||||
sequence = collect_image_sequence(filepath)
|
|
||||||
image = sitk.ReadImage(sequence)
|
elif ext in SEQUENCE_EXTS:
|
||||||
else:
|
|
||||||
image = sitk.ReadImage(filepath)
|
image = sitk.ReadImage(filepath)
|
||||||
if image.GetDimension() == 2:
|
if image.GetDimension() == 2:
|
||||||
image = sitk.ReadImage([filepath])
|
sequence = collect_image_sequence(filepath)
|
||||||
|
image = sitk.ReadImage(sequence)
|
||||||
|
name = get_sequence_name(filepath)
|
||||||
|
else:
|
||||||
|
image = sitk.ReadImage(filepath)
|
||||||
|
name = Path(filepath).name.removesuffix(ext)
|
||||||
|
else:
|
||||||
|
image = sitk.ReadImage(filepath)
|
||||||
|
name = Path(filepath).name.removesuffix(ext)
|
||||||
|
|
||||||
return image
|
return image, name
|
||||||
|
|
||||||
|
|
||||||
class ImportImageDialog(bpy.types.Operator):
|
def rgb2gray(image):
|
||||||
bl_idname = "bioxelnodes.import_image_dialog"
|
# Convert sRGB image to gray scale and rescale results to [0,255]
|
||||||
|
channels = [sitk.VectorIndexSelectionCast(
|
||||||
|
image, i, sitk.sitkFloat32) for i in range(image.GetNumberOfComponentsPerPixel())]
|
||||||
|
# linear mapping
|
||||||
|
I = (0.2126*channels[0] + 0.7152*channels[1] + 0.0722*channels[2])
|
||||||
|
|
||||||
|
return sitk.Cast(I, sitk.sitkFloat32)
|
||||||
|
|
||||||
|
|
||||||
|
class ImportVolumeDataDialog(bpy.types.Operator):
|
||||||
|
bl_idname = "bioxelnodes.import_volume_data_dialog"
|
||||||
bl_label = "Volume Data as Bioxels"
|
bl_label = "Volume Data as Bioxels"
|
||||||
bl_description = "Import Volume Data as Bioxels (VDB)"
|
bl_description = "Import Volume Data as Bioxels (VDB)"
|
||||||
bl_options = {'UNDO'}
|
bl_options = {'UNDO'}
|
||||||
@@ -123,10 +153,19 @@ class ImportImageDialog(bpy.types.Operator):
|
|||||||
|
|
||||||
series_id: bpy.props.StringProperty() # type: ignore
|
series_id: bpy.props.StringProperty() # type: ignore
|
||||||
|
|
||||||
|
resample_method: bpy.props.EnumProperty(
|
||||||
|
name="Resample Method",
|
||||||
|
default="linear",
|
||||||
|
items=[("linear", "Linear", ""),
|
||||||
|
("nearest_neighbor", "Nearest Neighbor", ""),
|
||||||
|
("gaussian", "Gaussian", "")]
|
||||||
|
) # type: ignore
|
||||||
|
|
||||||
read_as: bpy.props.EnumProperty(
|
read_as: bpy.props.EnumProperty(
|
||||||
name="Read as",
|
name="Read as",
|
||||||
default="scalar",
|
default="scalar",
|
||||||
items=[("scalar", "Scalar", ""), ("labels", "Labels", "")]
|
items=[("scalar", "Scalar", ""),
|
||||||
|
("labels", "Labels", "")]
|
||||||
) # type: ignore
|
) # type: ignore
|
||||||
|
|
||||||
label_index: bpy.props.IntProperty(
|
label_index: bpy.props.IntProperty(
|
||||||
@@ -169,11 +208,7 @@ class ImportImageDialog(bpy.types.Operator):
|
|||||||
) # type: ignore
|
) # type: ignore
|
||||||
|
|
||||||
def execute(self, context):
|
def execute(self, context):
|
||||||
bioxels_name = Path(self.filepath).parent.name \
|
image, bioxels_name = read_image(self.filepath, self.series_id)
|
||||||
if get_ext(self.filepath) in SEQUENCE_EXTS \
|
|
||||||
else Path(self.filepath).name.removesuffix(get_ext(self.filepath))
|
|
||||||
|
|
||||||
image = read_image(self.filepath, self.series_id)
|
|
||||||
|
|
||||||
bioxel_size = self.bioxel_size
|
bioxel_size = self.bioxel_size
|
||||||
orig_spacing = self.orig_spacing
|
orig_spacing = self.orig_spacing
|
||||||
@@ -189,16 +224,37 @@ class ImportImageDialog(bpy.types.Operator):
|
|||||||
bioxels_shape = get_bioxels_shape(
|
bioxels_shape = get_bioxels_shape(
|
||||||
bioxel_size, image_shape, orig_spacing)
|
bioxel_size, image_shape, orig_spacing)
|
||||||
|
|
||||||
print("Resampling...")
|
if self.read_as == "labels":
|
||||||
|
image = sitk.Cast(image, sitk.sitkUInt16)
|
||||||
|
default_value = 0
|
||||||
|
elif self.read_as == "scalar":
|
||||||
|
if "vector" in image.GetPixelIDTypeAsString():
|
||||||
|
print("Convet to Grayscale...")
|
||||||
|
image = rgb2gray(image)
|
||||||
|
else:
|
||||||
|
image = sitk.Cast(image, sitk.sitkFloat32)
|
||||||
|
|
||||||
|
stats = sitk.StatisticsImageFilter()
|
||||||
|
stats.Execute(image)
|
||||||
|
default_value = stats.GetMaximum() if self.invert_scalar else stats.GetMinimum()
|
||||||
|
|
||||||
|
if self.resample_method == "linear":
|
||||||
|
interpolator = sitk.sitkLinear
|
||||||
|
elif self.resample_method == "nearest_neighbor":
|
||||||
|
interpolator = sitk.sitkNearestNeighbor
|
||||||
|
elif self.resample_method == "gaussian":
|
||||||
|
interpolator = sitk.sitkGaussian
|
||||||
|
|
||||||
|
print(f"Resampling...")
|
||||||
image = sitk.Resample(
|
image = sitk.Resample(
|
||||||
image1=image,
|
image1=image,
|
||||||
size=bioxels_shape,
|
size=bioxels_shape,
|
||||||
transform=sitk.Transform(),
|
transform=sitk.Transform(),
|
||||||
interpolator=sitk.sitkLinear,
|
interpolator=interpolator,
|
||||||
outputOrigin=image.GetOrigin(),
|
outputOrigin=image.GetOrigin(),
|
||||||
outputSpacing=bioxels_spacing,
|
outputSpacing=bioxels_spacing,
|
||||||
outputDirection=image.GetDirection(),
|
outputDirection=image.GetDirection(),
|
||||||
defaultPixelValue=0,
|
defaultPixelValue=default_value,
|
||||||
outputPixelType=image.GetPixelID(),
|
outputPixelType=image.GetPixelID(),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -219,17 +275,20 @@ class ImportImageDialog(bpy.types.Operator):
|
|||||||
# A (nterior) j -> j -> y
|
# A (nterior) j -> j -> y
|
||||||
# S (uperior) k -> i -> z
|
# S (uperior) k -> i -> z
|
||||||
array = sitk.GetArrayFromImage(image)
|
array = sitk.GetArrayFromImage(image)
|
||||||
array = np.transpose(array)
|
|
||||||
|
|
||||||
orig_dtype = str(array.dtype)
|
orig_dtype = str(array.dtype)
|
||||||
# print(f"Coverting Dtype from {orig_dtype} to float...")
|
# print(f"Coverting Dtype from {orig_dtype} to float...")
|
||||||
# array = array.astype(float)
|
# array = array.astype(float)
|
||||||
|
|
||||||
if array.ndim == 4:
|
if array.ndim == 4:
|
||||||
if array.shape[3] == 3 and self.read_as == "scalar":
|
# if array.shape[0] == 3 and self.read_as == "scalar":
|
||||||
# RGB -> Grayscale
|
# # RGB -> Grayscale
|
||||||
array = np.dot(array[..., :3], [0.2989, 0.5870, 0.1140])
|
# array = np.dot(array[..., :3], [0.2989, 0.5870, 0.1140])
|
||||||
else:
|
# else:
|
||||||
array = array[:, :, :, 0]
|
# array = array[:, :, :, 0]
|
||||||
|
array = array[:, :, :, 0]
|
||||||
|
|
||||||
|
array = np.transpose(array)
|
||||||
|
|
||||||
bioxels_shape = array.shape
|
bioxels_shape = array.shape
|
||||||
bioxels_offset = 0.0
|
bioxels_offset = 0.0
|
||||||
@@ -260,16 +319,14 @@ class ImportImageDialog(bpy.types.Operator):
|
|||||||
bioxels_max = float(np.max(array))
|
bioxels_max = float(np.max(array))
|
||||||
bioxels_min = float(np.min(array))
|
bioxels_min = float(np.min(array))
|
||||||
|
|
||||||
stat_table = [("Max", orig_max),
|
stats_table = [("Max", orig_max),
|
||||||
("Min", orig_min),
|
("Min", orig_min),
|
||||||
("Median", orig_median),
|
("Median", orig_median),
|
||||||
("Percentile 80%", orig_percentile80),
|
("80%", orig_percentile80)]
|
||||||
("Max (Offset)", bioxels_max),
|
|
||||||
("Min (Offset)", bioxels_min)]
|
|
||||||
|
|
||||||
print("Bioxels Value Stat: Max:")
|
print("Bioxels Value Stats:")
|
||||||
for stat in stat_table:
|
for stats in stats_table:
|
||||||
print("| {: >20} | {: >20} |".format(*stat))
|
print("| {: >10} | {: >40} |".format(*stats))
|
||||||
|
|
||||||
# # Build VDB
|
# # Build VDB
|
||||||
grid = vdb.BoolGrid() if self.read_as == "labels" else vdb.FloatGrid()
|
grid = vdb.BoolGrid() if self.read_as == "labels" else vdb.FloatGrid()
|
||||||
@@ -436,6 +493,7 @@ class ImportImageDialog(bpy.types.Operator):
|
|||||||
text += "**TOO LARGE!**"
|
text += "**TOO LARGE!**"
|
||||||
|
|
||||||
panel = layout.box()
|
panel = layout.box()
|
||||||
|
panel.prop(self, "resample_method")
|
||||||
panel.prop(self, "bioxel_size")
|
panel.prop(self, "bioxel_size")
|
||||||
row = panel.row()
|
row = panel.row()
|
||||||
row.prop(self, "orig_spacing")
|
row.prop(self, "orig_spacing")
|
||||||
@@ -472,9 +530,8 @@ class ReadDICOM(bpy.types.Operator):
|
|||||||
bl_description = "Import Volume Data as Bioxels (VDB)"
|
bl_description = "Import Volume Data as Bioxels (VDB)"
|
||||||
bl_options = {'UNDO'}
|
bl_options = {'UNDO'}
|
||||||
|
|
||||||
filepath: bpy.props.StringProperty(
|
filepath: bpy.props.StringProperty(subtype="FILE_PATH") # type: ignore
|
||||||
subtype="FILE_PATH"
|
directory: bpy.props.StringProperty(subtype='DIR_PATH') # type: ignore
|
||||||
) # type: ignore
|
|
||||||
|
|
||||||
series_id: bpy.props.EnumProperty(
|
series_id: bpy.props.EnumProperty(
|
||||||
name="Select Series",
|
name="Select Series",
|
||||||
@@ -490,20 +547,27 @@ class ReadDICOM(bpy.types.Operator):
|
|||||||
self.report({"WARNING"}, "Not supported extension.")
|
self.report({"WARNING"}, "Not supported extension.")
|
||||||
return {'CANCELLED'}
|
return {'CANCELLED'}
|
||||||
|
|
||||||
image = read_image(self.filepath, self.series_id)
|
|
||||||
|
|
||||||
print("Collecting Meta Data...")
|
print("Collecting Meta Data...")
|
||||||
print("Original Shape:", image.GetSize())
|
image, name = read_image(self.filepath, self.series_id)
|
||||||
print("Original Spacing:", image.GetSpacing())
|
stats_table = [("Shape", str(image.GetSize())),
|
||||||
print("Original Origin:", image.GetOrigin())
|
("Spacing", str(image.GetSpacing())),
|
||||||
print("Original Direction:", image.GetDirection())
|
("Origin", str(image.GetOrigin())),
|
||||||
|
("Direction", str(image.GetDirection())),
|
||||||
|
("Data Type", image.GetPixelIDTypeAsString())]
|
||||||
|
|
||||||
bpy.ops.bioxelnodes.import_image_dialog(
|
print("Meta Data:")
|
||||||
|
for stats in stats_table:
|
||||||
|
print("| {: >10} | {: >40} |".format(*stats))
|
||||||
|
|
||||||
|
do_orient = ext not in SEQUENCE_EXTS or ext in DICOM_EXTS
|
||||||
|
|
||||||
|
bpy.ops.bioxelnodes.import_volume_data_dialog(
|
||||||
'INVOKE_DEFAULT',
|
'INVOKE_DEFAULT',
|
||||||
filepath=self.filepath,
|
filepath=self.filepath,
|
||||||
orig_shape=image.GetSize(),
|
orig_shape=image.GetSize(),
|
||||||
orig_spacing=image.GetSpacing(),
|
orig_spacing=image.GetSpacing(),
|
||||||
series_id=self.series_id or ""
|
series_id=self.series_id or "",
|
||||||
|
do_orient=do_orient
|
||||||
)
|
)
|
||||||
|
|
||||||
self.report({"INFO"}, "Successfully Readed.")
|
self.report({"INFO"}, "Successfully Readed.")
|
||||||
@@ -515,7 +579,7 @@ class ReadDICOM(bpy.types.Operator):
|
|||||||
return {'FINISHED'}
|
return {'FINISHED'}
|
||||||
|
|
||||||
def invoke(self, context, event):
|
def invoke(self, context, event):
|
||||||
if not self.filepath:
|
if not self.filepath and not self.directory:
|
||||||
return {'CANCELLED'}
|
return {'CANCELLED'}
|
||||||
|
|
||||||
show_message('Reading image data, it may take a while...',
|
show_message('Reading image data, it may take a while...',
|
||||||
@@ -551,20 +615,20 @@ class ReadDICOM(bpy.types.Operator):
|
|||||||
text="Please be patient...")
|
text="Please be patient...")
|
||||||
|
|
||||||
|
|
||||||
class ImportImage(bpy.types.Operator):
|
class ImportVolumeData(bpy.types.Operator):
|
||||||
bl_idname = "bioxelnodes.import_image"
|
bl_idname = "bioxelnodes.import_volume_data"
|
||||||
bl_label = "Volume Data as Bioxels"
|
bl_label = "Volume Data as Bioxels"
|
||||||
bl_description = "Import Volume Data as Bioxels (VDB)"
|
bl_description = "Import Volume Data as Bioxels (VDB)"
|
||||||
bl_options = {'UNDO'}
|
bl_options = {'UNDO'}
|
||||||
|
|
||||||
filepath: bpy.props.StringProperty(
|
filepath: bpy.props.StringProperty(subtype="FILE_PATH") # type: ignore
|
||||||
subtype="FILE_PATH"
|
directory: bpy.props.StringProperty(subtype='DIR_PATH') # type: ignore
|
||||||
) # type: ignore
|
|
||||||
|
|
||||||
def execute(self, context):
|
def execute(self, context):
|
||||||
bpy.ops.bioxelnodes.read_dicom(
|
bpy.ops.bioxelnodes.read_dicom(
|
||||||
'INVOKE_DEFAULT',
|
'INVOKE_DEFAULT',
|
||||||
filepath=self.filepath
|
filepath=self.filepath,
|
||||||
|
directory=self.directory
|
||||||
)
|
)
|
||||||
return {'FINISHED'}
|
return {'FINISHED'}
|
||||||
|
|
||||||
@@ -574,8 +638,8 @@ class ImportImage(bpy.types.Operator):
|
|||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
class BIOXELNODES_FH_ImportImage(bpy.types.FileHandler):
|
class BIOXELNODES_FH_ImportVolumeData(bpy.types.FileHandler):
|
||||||
bl_idname = "BIOXELNODES_FH_ImportImage"
|
bl_idname = "BIOXELNODES_FH_ImportVolumeData"
|
||||||
bl_label = "File handler for dicom import"
|
bl_label = "File handler for dicom import"
|
||||||
bl_import_operator = "bioxelnodes.read_dicom"
|
bl_import_operator = "bioxelnodes.read_dicom"
|
||||||
bl_file_extensions = ";".join(FH_EXTS)
|
bl_file_extensions = ";".join(FH_EXTS)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import bpy
|
import bpy
|
||||||
from .convert import ConvertToMesh
|
from .convert import ConvertToMesh
|
||||||
from .io import ExportVDB, ImportImage
|
from .io import ExportVDB, ImportVolumeData
|
||||||
from .misc import SaveBioxels
|
from .misc import SaveBioxels
|
||||||
|
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ class ObjectMenu(bpy.types.Menu):
|
|||||||
def TOPBAR_FILE_IMPORT(self, context):
|
def TOPBAR_FILE_IMPORT(self, context):
|
||||||
layout = self.layout
|
layout = self.layout
|
||||||
layout.separator()
|
layout.separator()
|
||||||
layout.operator(ImportImage.bl_idname)
|
layout.operator(ImportVolumeData.bl_idname)
|
||||||
|
|
||||||
|
|
||||||
def TOPBAR_FILE_EXPORT(self, context):
|
def TOPBAR_FILE_EXPORT(self, context):
|
||||||
@@ -33,13 +33,13 @@ def VIEW3D_OBJECT(self, context):
|
|||||||
layout.separator()
|
layout.separator()
|
||||||
|
|
||||||
|
|
||||||
def register():
|
def add():
|
||||||
bpy.types.TOPBAR_MT_file_import.append(TOPBAR_FILE_IMPORT)
|
bpy.types.TOPBAR_MT_file_import.append(TOPBAR_FILE_IMPORT)
|
||||||
bpy.types.TOPBAR_MT_file_export.append(TOPBAR_FILE_EXPORT)
|
bpy.types.TOPBAR_MT_file_export.append(TOPBAR_FILE_EXPORT)
|
||||||
bpy.types.VIEW3D_MT_object_context_menu.prepend(VIEW3D_OBJECT)
|
bpy.types.VIEW3D_MT_object_context_menu.prepend(VIEW3D_OBJECT)
|
||||||
|
|
||||||
|
|
||||||
def unregister():
|
def remove():
|
||||||
bpy.types.TOPBAR_MT_file_import.remove(TOPBAR_FILE_IMPORT)
|
bpy.types.TOPBAR_MT_file_import.remove(TOPBAR_FILE_IMPORT)
|
||||||
bpy.types.TOPBAR_MT_file_export.remove(TOPBAR_FILE_EXPORT)
|
bpy.types.TOPBAR_MT_file_export.remove(TOPBAR_FILE_EXPORT)
|
||||||
bpy.types.VIEW3D_MT_object_context_menu.remove(VIEW3D_OBJECT)
|
bpy.types.VIEW3D_MT_object_context_menu.remove(VIEW3D_OBJECT)
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ def get_all_bioxels_objs():
|
|||||||
return list(set(bioxels_objs))
|
return list(set(bioxels_objs))
|
||||||
|
|
||||||
|
|
||||||
def get_text_index(text):
|
def get_text_index_str(text):
|
||||||
# Initialize an empty string to store the digits
|
# Initialize an empty string to store the digits
|
||||||
digits = ""
|
digits = ""
|
||||||
|
|
||||||
@@ -115,7 +115,7 @@ def get_text_index(text):
|
|||||||
# Reverse the digits string to get the correct order
|
# Reverse the digits string to get the correct order
|
||||||
last_number = digits[::-1]
|
last_number = digits[::-1]
|
||||||
|
|
||||||
return int(last_number) if last_number else None
|
return last_number
|
||||||
|
|
||||||
|
|
||||||
def add_driver(target_prop, var_sources, expression):
|
def add_driver(target_prop, var_sources, expression):
|
||||||
|
|||||||
Reference in New Issue
Block a user