feat: more time sequence choice
fix: contrast formulation cause leak of high value fix: empty series_id cause fail to read chore: restructure folder chore!: change layer vars BREAKING CHANGE: not support 4.1 any more
This commit is contained in:
0
bioxelnodes/operators/__init__.py
Normal file
0
bioxelnodes/operators/__init__.py
Normal file
283
bioxelnodes/operators/container.py
Normal file
283
bioxelnodes/operators/container.py
Normal file
@@ -0,0 +1,283 @@
|
||||
import bpy
|
||||
|
||||
import bmesh
|
||||
|
||||
from ..nodes import custom_nodes
|
||||
from ..bioxel.io import load_container, save_container
|
||||
from ..bioxelutils.container import (container_to_obj, obj_to_container,
|
||||
get_container_objs_from_selection)
|
||||
from ..bioxelutils.node import get_nodes_by_type, move_node_between_nodes, move_node_to_node
|
||||
from .utils import get_cache_dir, select_object
|
||||
|
||||
|
||||
class SaveContainer(bpy.types.Operator):
|
||||
bl_idname = "bioxelnodes.save_container"
|
||||
bl_label = "Save Container (.bioxel) (BETA)"
|
||||
bl_description = "Clean all caches saved in temp"
|
||||
|
||||
filepath: bpy.props.StringProperty(
|
||||
subtype="FILE_PATH"
|
||||
) # type: ignore
|
||||
|
||||
filename_ext = ".bioxel"
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
container_objs = get_container_objs_from_selection()
|
||||
return len(container_objs) > 0
|
||||
|
||||
def execute(self, context):
|
||||
container_objs = get_container_objs_from_selection()
|
||||
|
||||
if len(container_objs) == 0:
|
||||
self.report({"WARNING"}, "Cannot find any bioxel container.")
|
||||
return {'FINISHED'}
|
||||
|
||||
container = obj_to_container(container_objs[0])
|
||||
save_path = f"{self.filepath.split('.')[0]}.bioxel"
|
||||
save_container(container, save_path, overwrite=True)
|
||||
|
||||
self.report({"INFO"}, f"Successfully save to {save_path}")
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
context.window_manager.fileselect_add(self)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
|
||||
class LoadContainer(bpy.types.Operator):
|
||||
bl_idname = "bioxelnodes.load_container"
|
||||
bl_label = "Load Container (.bioxel) (BETA)"
|
||||
bl_description = "Clean all caches saved in temp"
|
||||
|
||||
filepath: bpy.props.StringProperty(
|
||||
subtype="FILE_PATH"
|
||||
) # type: ignore
|
||||
|
||||
filename_ext = ".bioxel"
|
||||
|
||||
def execute(self, context):
|
||||
load_path = self.filepath
|
||||
container = load_container(self.filepath)
|
||||
container_obj = container_to_obj(container,
|
||||
scene_scale=0.01,
|
||||
cache_dir=get_cache_dir(context))
|
||||
select_object(container_obj)
|
||||
|
||||
self.report({"INFO"}, f"Successfully load {load_path}")
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
context.window_manager.fileselect_add(self)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
|
||||
class PickObject():
|
||||
bl_options = {'UNDO'}
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
container_objs = get_container_objs_from_selection()
|
||||
return len(container_objs) > 0
|
||||
|
||||
def execute(self, context):
|
||||
container_objs = get_container_objs_from_selection()
|
||||
|
||||
if len(container_objs) == 0:
|
||||
self.report({"WARNING"}, "Cannot find any bioxel container.")
|
||||
return {'FINISHED'}
|
||||
|
||||
container_obj = container_objs[0]
|
||||
|
||||
bpy.ops.mesh.primitive_cube_add(
|
||||
size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
|
||||
obj = bpy.context.active_object
|
||||
|
||||
obj.name = f"{self.object_type}_{container_obj.name}"
|
||||
|
||||
bpy.ops.node.new_geometry_nodes_modifier()
|
||||
modifier = obj.modifiers[0]
|
||||
node_group = modifier.node_group
|
||||
|
||||
output_node = get_nodes_by_type(node_group, 'NodeGroupOutput')[0]
|
||||
pick_mesh_node = custom_nodes.add_node(node_group,
|
||||
f"BioxelNodes_Pick{self.object_type}")
|
||||
|
||||
pick_mesh_node.inputs[0].default_value = container_obj
|
||||
node_group.links.new(pick_mesh_node.outputs[0], output_node.inputs[0])
|
||||
|
||||
select_object(obj)
|
||||
|
||||
self.report({"INFO"}, f"Successfully picked")
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class PickMesh(bpy.types.Operator, PickObject):
|
||||
bl_idname = "bioxelnodes.pick_mesh"
|
||||
bl_label = "Pick Mesh"
|
||||
bl_description = "Pick Container Mesh"
|
||||
object_type = "Mesh"
|
||||
|
||||
|
||||
class PickVolume(bpy.types.Operator, PickObject):
|
||||
bl_idname = "bioxelnodes.pick_volume"
|
||||
bl_label = "Pick Volume"
|
||||
bl_description = "Pick Container Volume"
|
||||
object_type = "Volume"
|
||||
|
||||
|
||||
class PickBboxWire(bpy.types.Operator, PickObject):
|
||||
bl_idname = "bioxelnodes.pick_bbox_wire"
|
||||
bl_label = "Pick Bbox Wire"
|
||||
bl_description = "Pick Container Bbox Wire"
|
||||
object_type = "BboxWire"
|
||||
|
||||
|
||||
class AddCutter():
|
||||
bl_options = {'UNDO'}
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
container_objs = get_container_objs_from_selection()
|
||||
return len(container_objs) > 0
|
||||
|
||||
def execute(self, context):
|
||||
container_objs = get_container_objs_from_selection()
|
||||
|
||||
if len(container_objs) == 0:
|
||||
self.report({"WARNING"}, "Cannot find any bioxel container.")
|
||||
return {'FINISHED'}
|
||||
|
||||
container_obj = container_objs[0]
|
||||
|
||||
if self.object_type == "plane":
|
||||
node_type = "BioxelNodes_PlaneObjectCutter"
|
||||
bpy.ops.mesh.primitive_plane_add(
|
||||
size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
|
||||
elif self.object_type == "cylinder":
|
||||
node_type = "BioxelNodes_CylinderObjectCutter"
|
||||
bpy.ops.mesh.primitive_cylinder_add(
|
||||
radius=1, depth=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
|
||||
bpy.context.object.rotation_euler[0] = container_obj.rotation_euler[0]
|
||||
elif self.object_type == "cube":
|
||||
node_type = "BioxelNodes_CubeObjectCutter"
|
||||
bpy.ops.mesh.primitive_cube_add(
|
||||
size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
|
||||
elif self.object_type == "sphere":
|
||||
node_type = "BioxelNodes_SphereObjectCutter"
|
||||
bpy.ops.mesh.primitive_ico_sphere_add(
|
||||
radius=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
|
||||
elif self.object_type == "pie":
|
||||
node_type = "BioxelNodes_PieObjectCutter"
|
||||
# Create mesh
|
||||
pie_mesh = bpy.data.meshes.new('Pie')
|
||||
|
||||
# Create object
|
||||
pie = bpy.data.objects.new('Pie', pie_mesh)
|
||||
|
||||
# Link object to scene
|
||||
bpy.context.scene.collection.objects.link(pie)
|
||||
# Get a BMesh representation
|
||||
bm = bmesh.new() # create an empty BMesh
|
||||
bm.from_mesh(pie_mesh) # fill it in from a Mesh
|
||||
|
||||
# Hot to create vertices
|
||||
v_0 = bm.verts.new((0.0, -1.0, 0.0))
|
||||
v_1 = bm.verts.new((-1.0, -1.0, 1.0))
|
||||
v_2 = bm.verts.new((0.0, 1.0, 0.0))
|
||||
v_3 = bm.verts.new((-1.0, 1.0, 1.0))
|
||||
v_4 = bm.verts.new((1.0, -1.0, 1.0))
|
||||
v_5 = bm.verts.new((1.0, 1.0, 1.0))
|
||||
|
||||
# Initialize the index values of this sequence.
|
||||
bm.verts.index_update()
|
||||
|
||||
# How to create a face
|
||||
# it's not necessary to create the edges before, I made it only to show how create
|
||||
# edges too
|
||||
bm.faces.new((v_0, v_1, v_3, v_2))
|
||||
bm.faces.new((v_0, v_2, v_5, v_4))
|
||||
|
||||
# Finish up, write the bmesh back to the mesh
|
||||
bm.to_mesh(pie_mesh)
|
||||
bpy.context.view_layer.objects.active = pie
|
||||
|
||||
cutter_obj = bpy.context.active_object
|
||||
cutter_obj.visible_camera = False
|
||||
cutter_obj.visible_diffuse = False
|
||||
cutter_obj.visible_glossy = False
|
||||
cutter_obj.visible_transmission = False
|
||||
cutter_obj.visible_volume_scatter = False
|
||||
cutter_obj.visible_shadow = False
|
||||
cutter_obj.hide_render = True
|
||||
cutter_obj.display_type = 'WIRE'
|
||||
|
||||
modifier = container_obj.modifiers[0]
|
||||
node_group = modifier.node_group
|
||||
cutter_node = custom_nodes.add_node(node_group, node_type)
|
||||
cutter_node.inputs[0].default_value = cutter_obj
|
||||
|
||||
cut_nodes = get_nodes_by_type(node_group,
|
||||
'BioxelNodes_Cut')
|
||||
output_node = get_nodes_by_type(node_group, 'NodeGroupOutput')[0]
|
||||
if len(cut_nodes) == 0:
|
||||
cut_node = custom_nodes.add_node(node_group, 'BioxelNodes_Cut')
|
||||
if len(output_node.inputs[0].links) == 0:
|
||||
node_group.links.new(cut_node.outputs[0],
|
||||
output_node.inputs[0])
|
||||
move_node_to_node(cut_node, output_node, (-300, 0))
|
||||
else:
|
||||
pre_output_node = output_node.inputs[0].links[0].from_node
|
||||
node_group.links.new(pre_output_node.outputs[0],
|
||||
cut_node.inputs[0])
|
||||
node_group.links.new(cut_node.outputs[0],
|
||||
output_node.inputs[0])
|
||||
move_node_between_nodes(cut_node,
|
||||
[pre_output_node, output_node])
|
||||
|
||||
node_group.links.new(cutter_node.outputs[0],
|
||||
cut_node.inputs[1])
|
||||
|
||||
move_node_to_node(cutter_node, cut_node, (-300, -300))
|
||||
select_object(cutter_obj)
|
||||
else:
|
||||
move_node_to_node(cutter_node, output_node, (0, -100))
|
||||
select_object(container_obj)
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class AddPlaneCutter(bpy.types.Operator, AddCutter):
|
||||
bl_idname = "bioxelnodes.add_plane_cutter"
|
||||
bl_label = "Add a Plane Cutter"
|
||||
bl_description = "Add a Plane Cutter to Container"
|
||||
object_type = "plane"
|
||||
|
||||
|
||||
class AddCylinderCutter(bpy.types.Operator, AddCutter):
|
||||
bl_idname = "bioxelnodes.add_cylinder_cutter"
|
||||
bl_label = "Add a Cylinder Cutter"
|
||||
bl_description = "Add a Cylinder Cutter to Container"
|
||||
object_type = "cylinder"
|
||||
|
||||
|
||||
class AddCubeCutter(bpy.types.Operator, AddCutter):
|
||||
bl_idname = "bioxelnodes.add_cube_cutter"
|
||||
bl_label = "Add a Cube Cutter"
|
||||
bl_description = "Add a Cube Cutter to Container"
|
||||
object_type = "cube"
|
||||
|
||||
|
||||
class AddSphereCutter(bpy.types.Operator, AddCutter):
|
||||
bl_idname = "bioxelnodes.add_sphere_cutter"
|
||||
bl_label = "Add a Sphere Cutter"
|
||||
bl_description = "Add a Sphere Cutter to Container"
|
||||
object_type = "sphere"
|
||||
|
||||
|
||||
class AddPieCutter(bpy.types.Operator, AddCutter):
|
||||
bl_idname = "bioxelnodes.add_pie_cutter"
|
||||
bl_label = "Add a Pie Cutter"
|
||||
bl_description = "Add a Pie Cutter to Container"
|
||||
object_type = "pie"
|
||||
808
bioxelnodes/operators/io.py
Normal file
808
bioxelnodes/operators/io.py
Normal file
@@ -0,0 +1,808 @@
|
||||
import math
|
||||
import bpy
|
||||
import shutil
|
||||
import threading
|
||||
import numpy as np
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
from ..exceptions import CancelledByUser
|
||||
from ..props import BIOXELNODES_Series
|
||||
from ..bioxel.layer import Layer
|
||||
from ..bioxelutils.layer import (get_all_layer_objs,
|
||||
get_layer_obj)
|
||||
from ..bioxelutils.container import (Container,
|
||||
add_layers,
|
||||
container_to_obj,
|
||||
get_container_objs_from_selection)
|
||||
from ..bioxel.parse import (DICOM_EXTS, SUPPORT_EXTS,
|
||||
get_ext, parse_volumetric_data)
|
||||
from .utils import (get_cache_dir, get_preferences,
|
||||
progress_update, progress_bar, select_object)
|
||||
|
||||
# 3rd-party
|
||||
import SimpleITK as sitk
|
||||
import transforms3d
|
||||
|
||||
FH_EXTS = ['', '.dcm', '.DCM', '.DICOM', '.ima', '.IMA',
|
||||
'.gipl', '.gipl.gz',
|
||||
'.mnc', '.MNC',
|
||||
'.mrc', '.rec',
|
||||
'.mha', '.mhd',
|
||||
'.nia', '.nii', '.nii.gz', '.hdr', '.img', '.img.gz',
|
||||
'.hdf', '.h4', '.hdf4', '.he2', '.h5', '.hdf5', '.he5',
|
||||
'.nrrd', '.nhdr',
|
||||
'.vtk',
|
||||
'.gz'
|
||||
'.ome.tiff', '.ome.tif',
|
||||
'.mrc', '.mrc.gz', '.map', '.map.gz']
|
||||
|
||||
|
||||
def get_layer_shape(bioxel_size: float, orig_shape: tuple, orig_spacing: tuple):
|
||||
shape = (int(orig_shape[0] / bioxel_size * orig_spacing[0]),
|
||||
int(orig_shape[1] / bioxel_size * orig_spacing[1]),
|
||||
int(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 get_layer_size(shape: tuple, bioxel_size: float, scale: float = 1.0):
|
||||
size = (float(shape[0] * bioxel_size * scale),
|
||||
float(shape[1] * bioxel_size * scale),
|
||||
float(shape[2] * bioxel_size * scale))
|
||||
|
||||
return size
|
||||
|
||||
|
||||
"""
|
||||
ImportVolumetricData
|
||||
-> ParseVolumetricData -> ImportVolumetricDataDialog
|
||||
FH_ImportVolumetricData
|
||||
|
||||
start import parse data execute import
|
||||
"""
|
||||
|
||||
|
||||
class ImportVolumetricData():
|
||||
bl_options = {'UNDO'}
|
||||
|
||||
filepath: bpy.props.StringProperty(subtype="FILE_PATH") # type: ignore
|
||||
directory: bpy.props.StringProperty(subtype='DIR_PATH') # type: ignore
|
||||
|
||||
read_as = "scalar"
|
||||
|
||||
def execute(self, context):
|
||||
container_objs = get_container_objs_from_selection()
|
||||
|
||||
if len(container_objs) > 0:
|
||||
bpy.ops.bioxelnodes.parse_volumetric_data('INVOKE_DEFAULT',
|
||||
filepath=self.filepath,
|
||||
directory=self.directory,
|
||||
container_obj_name=container_objs[0].name,
|
||||
read_as=self.read_as)
|
||||
else:
|
||||
bpy.ops.bioxelnodes.parse_volumetric_data('INVOKE_DEFAULT',
|
||||
filepath=self.filepath,
|
||||
directory=self.directory,
|
||||
read_as=self.read_as)
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
context.window_manager.fileselect_add(self)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
|
||||
class ImportAsScalarLayer(bpy.types.Operator, ImportVolumetricData):
|
||||
bl_idname = "bioxelnodes.import_as_scalar_layer"
|
||||
bl_label = "Import as Scalar"
|
||||
bl_description = "Import Volumetric Data to Container as Scalar"
|
||||
read_as = "scalar"
|
||||
|
||||
|
||||
class ImportAsLabelLayer(bpy.types.Operator, ImportVolumetricData):
|
||||
bl_idname = "bioxelnodes.import_as_label_layer"
|
||||
bl_label = "Import as Label"
|
||||
bl_description = "Import Volumetric Data to Container as Label"
|
||||
read_as = "label"
|
||||
|
||||
|
||||
class BIOXELNODES_FH_ImportVolumetricData(bpy.types.FileHandler):
|
||||
bl_idname = "BIOXELNODES_FH_ImportVolumetricData"
|
||||
bl_label = "File handler for dicom import"
|
||||
bl_import_operator = "bioxelnodes.parse_volumetric_data"
|
||||
bl_file_extensions = ";".join(FH_EXTS)
|
||||
|
||||
@classmethod
|
||||
def poll_drop(cls, context):
|
||||
return (context.area and context.area.type == 'VIEW_3D')
|
||||
|
||||
|
||||
def get_series_ids(self, context):
|
||||
items = []
|
||||
for index, series_id in enumerate(self.series_ids):
|
||||
items.append((
|
||||
series_id.id,
|
||||
series_id.label,
|
||||
"",
|
||||
index
|
||||
))
|
||||
|
||||
return items
|
||||
|
||||
|
||||
class ParseVolumetricData(bpy.types.Operator):
|
||||
bl_idname = "bioxelnodes.parse_volumetric_data"
|
||||
bl_label = "Import Volumetric Data"
|
||||
bl_description = "Import Volumetric Data as Layer"
|
||||
bl_options = {'UNDO'}
|
||||
|
||||
meta = None
|
||||
thread = None
|
||||
_timer = None
|
||||
|
||||
progress: bpy.props.FloatProperty(name="Progress",
|
||||
options={"SKIP_SAVE"},
|
||||
default=1) # type: ignore
|
||||
|
||||
filepath: bpy.props.StringProperty(subtype="FILE_PATH") # type: ignore
|
||||
directory: bpy.props.StringProperty(subtype='DIR_PATH') # type: ignore
|
||||
container_obj_name: bpy.props.StringProperty() # type: ignore
|
||||
|
||||
read_as: bpy.props.EnumProperty(name="Read as",
|
||||
default="scalar",
|
||||
items=[("scalar", "Scalar", ""),
|
||||
("label", "Labels", "")]) # type: ignore
|
||||
|
||||
series_id: bpy.props.EnumProperty(name="Select Series",
|
||||
items=get_series_ids) # type: ignore
|
||||
|
||||
series_ids: bpy.props.CollectionProperty(
|
||||
type=BIOXELNODES_Series) # type: ignore
|
||||
|
||||
def execute(self, context):
|
||||
data_path = Path(self.filepath).resolve()
|
||||
ext = get_ext(data_path)
|
||||
if ext not in SUPPORT_EXTS:
|
||||
self.report({"WARNING"}, "Not supported extension.")
|
||||
return {'CANCELLED'}
|
||||
|
||||
print("Collecting Meta Data...")
|
||||
|
||||
def parse_volumetric_data_func(self, context, cancel):
|
||||
def progress_callback(factor, text):
|
||||
if cancel():
|
||||
raise CancelledByUser
|
||||
progress_update(context, factor, text)
|
||||
|
||||
try:
|
||||
series_id = self.series_id if self.series_id != "empty" else ""
|
||||
data, meta = parse_volumetric_data(data_file=self.filepath,
|
||||
series_id=series_id,
|
||||
progress_callback=progress_callback)
|
||||
except CancelledByUser:
|
||||
return
|
||||
except Exception as e:
|
||||
self.has_error = e
|
||||
return
|
||||
|
||||
if cancel():
|
||||
return
|
||||
|
||||
self.meta = meta
|
||||
|
||||
# Init cancel flag
|
||||
self.is_cancelled = False
|
||||
self.has_error = None
|
||||
|
||||
# Create the thread
|
||||
self.thread = threading.Thread(target=parse_volumetric_data_func,
|
||||
args=(self, context, lambda: self.is_cancelled))
|
||||
|
||||
# Start the thread
|
||||
self.thread.start()
|
||||
# Add a timmer for modal
|
||||
self._timer = context.window_manager.event_timer_add(time_step=0.1,
|
||||
window=context.window)
|
||||
# Append progress bar to status bar
|
||||
bpy.types.STATUSBAR_HT_header.append(progress_bar)
|
||||
|
||||
# Start modal handler
|
||||
context.window_manager.modal_handler_add(self)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
def modal(self, context, event):
|
||||
# Check if user press 'ESC'
|
||||
if event.type == 'ESC':
|
||||
self.is_cancelled = True
|
||||
progress_update(context, 0, "Canceling...")
|
||||
return {'PASS_THROUGH'}
|
||||
|
||||
# Check if is the timer time
|
||||
if event.type != 'TIMER':
|
||||
return {'PASS_THROUGH'}
|
||||
|
||||
# Force update status bar
|
||||
bpy.context.workspace.status_text_set_internal(None)
|
||||
|
||||
# Check if thread is still running
|
||||
if self.thread.is_alive():
|
||||
return {'PASS_THROUGH'}
|
||||
|
||||
# Release the thread
|
||||
self.thread.join()
|
||||
# Remove the timer
|
||||
context.window_manager.event_timer_remove(self._timer)
|
||||
# Remove the progress bar from status bar
|
||||
bpy.types.STATUSBAR_HT_header.remove(progress_bar)
|
||||
|
||||
# Check if thread is cancelled by user
|
||||
if self.is_cancelled:
|
||||
self.report({"WARNING"}, "Canncelled by user.")
|
||||
return {'CANCELLED'}
|
||||
|
||||
# Check if thread is cancelled by user
|
||||
if self.has_error:
|
||||
raise self.has_error
|
||||
|
||||
# Check if has return
|
||||
if self.meta is None:
|
||||
self.report({"ERROR"}, "Some thing went wrong.")
|
||||
return {'CANCELLED'}
|
||||
|
||||
# If not canncelled...
|
||||
for key, value in self.meta.items():
|
||||
print(f"{key}: {value}")
|
||||
|
||||
orig_shape = self.meta['xyz_shape']
|
||||
orig_spacing = self.meta['spacing']
|
||||
min_size = min(orig_spacing[0],
|
||||
orig_spacing[1], orig_spacing[2])
|
||||
bioxel_size = max(min_size, 1.0)
|
||||
|
||||
layer_shape = get_layer_shape(1, orig_shape, orig_spacing)
|
||||
layer_size = get_layer_size(layer_shape,
|
||||
bioxel_size)
|
||||
log10 = math.floor(math.log10(max(*layer_size)))
|
||||
log10 = max(1, log10)
|
||||
log10 = min(3, log10)
|
||||
scene_scale = math.pow(10, -log10)
|
||||
|
||||
if self.container_obj_name:
|
||||
container_obj = bpy.data.objects.get(self.container_obj_name)
|
||||
container_name = container_obj.name
|
||||
else:
|
||||
container_name = self.meta['name']
|
||||
|
||||
series_id = self.series_id if self.series_id != "empty" else ""
|
||||
bpy.ops.bioxelnodes.import_volumetric_data_dialog(
|
||||
'INVOKE_DEFAULT',
|
||||
filepath=self.filepath,
|
||||
container_name=container_name,
|
||||
layer_name=self.meta['description'],
|
||||
orig_shape=orig_shape,
|
||||
orig_spacing=orig_spacing,
|
||||
bioxel_size=bioxel_size,
|
||||
series_id=series_id,
|
||||
frame_count=self.meta['frame_count'],
|
||||
channel_count=self.meta['channel_count'],
|
||||
container_obj_name=self.container_obj_name,
|
||||
read_as=self.read_as,
|
||||
scene_scale=scene_scale
|
||||
)
|
||||
|
||||
self.report({"INFO"}, "Successfully Readed.")
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
if not self.filepath and not self.directory:
|
||||
return {'CANCELLED'}
|
||||
|
||||
data_path = Path(self.filepath).resolve()
|
||||
ext = get_ext(data_path)
|
||||
|
||||
# Series Selection
|
||||
if ext in DICOM_EXTS:
|
||||
data_dirpath = data_path.parent
|
||||
reader = sitk.ImageSeriesReader()
|
||||
reader.MetaDataDictionaryArrayUpdateOn()
|
||||
reader.LoadPrivateTagsOn()
|
||||
|
||||
series_ids = reader.GetGDCMSeriesIDs(str(data_dirpath))
|
||||
series_items = {}
|
||||
|
||||
for series_id in series_ids:
|
||||
series_files = reader.GetGDCMSeriesFileNames(
|
||||
str(data_dirpath), series_id)
|
||||
single = sitk.ImageFileReader()
|
||||
single.SetFileName(series_files[0])
|
||||
single.LoadPrivateTagsOn()
|
||||
single.ReadImageInformation()
|
||||
|
||||
def get_meta(key):
|
||||
try:
|
||||
stirng = single.GetMetaData(key).removesuffix(" ")
|
||||
if stirng in ["No study description",
|
||||
"No series description",
|
||||
""]:
|
||||
return "Unknown"
|
||||
else:
|
||||
return stirng
|
||||
except:
|
||||
return "Unknown"
|
||||
|
||||
study_description = get_meta("0008|1030")
|
||||
series_description = get_meta("0008|103e")
|
||||
series_modality = get_meta("0008|0060")
|
||||
size_x = get_meta("0028|0011")
|
||||
size_y = get_meta("0028|0010")
|
||||
count = get_meta("0020|0013")
|
||||
count = count if count != "Unknown" else 1
|
||||
|
||||
# some series image count = 0 ????
|
||||
if int(count) == 0:
|
||||
continue
|
||||
|
||||
# series_id cannot be "" in blender selection
|
||||
if series_id == "":
|
||||
series_id = "empty"
|
||||
|
||||
series_items[series_id] = "{:<20} {:>1}".format(f"{study_description}>{series_description}({series_modality})",
|
||||
f"({size_x}x{size_y})x{count}")
|
||||
|
||||
for key, value in series_items.items():
|
||||
series_item = self.series_ids.add()
|
||||
series_item.id = key
|
||||
series_item.label = value
|
||||
|
||||
if len(series_items.keys()) > 1:
|
||||
context.window_manager.invoke_props_dialog(self,
|
||||
width=400,
|
||||
title="Which series to import?")
|
||||
return {'RUNNING_MODAL'}
|
||||
elif len(series_items.keys()) == 1:
|
||||
self.series_id = list(series_items.keys())[0]
|
||||
else:
|
||||
self.report({"ERROR"}, "Get no vaild series.")
|
||||
return {'CANCELLED'}
|
||||
|
||||
self.execute(context)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
layout.label(
|
||||
text='Detect multi-series in DICOM, pick one')
|
||||
layout.prop(self, "series_id")
|
||||
|
||||
|
||||
def get_sequence_sources(self, context):
|
||||
items = [("-1", "None (Static)", "")]
|
||||
orig_shape = tuple(self.orig_shape)
|
||||
if self.frame_count > 1:
|
||||
items.append(("0", f"Frame (Get {self.frame_count} frames)", ""))
|
||||
elif self.frame_count == 1 and self.channel_count > 1:
|
||||
items.append(("4", f"Channel (Get {self.channel_count} frames)", ""))
|
||||
elif self.frame_count == 1 and self.channel_count == 1:
|
||||
items.append(("1", f"X (Get {orig_shape[0]} frames)", ""))
|
||||
items.append(("2", f"Y (Get {orig_shape[1]} frames)", ""))
|
||||
items.append(("3", f"Z (Get {orig_shape[2]} frames)", ""))
|
||||
|
||||
return items
|
||||
|
||||
|
||||
class ImportVolumetricDataDialog(bpy.types.Operator):
|
||||
bl_idname = "bioxelnodes.import_volumetric_data_dialog"
|
||||
bl_label = "Import Volumetric Data"
|
||||
bl_description = "Import Volumetric Data as Layer"
|
||||
bl_options = {'UNDO'}
|
||||
|
||||
layers = None
|
||||
thread = None
|
||||
_timer = None
|
||||
|
||||
filepath: bpy.props.StringProperty(subtype="FILE_PATH") # type: ignore
|
||||
|
||||
container_name: bpy.props.StringProperty(
|
||||
name="Container Name") # type: ignore
|
||||
|
||||
layer_name: bpy.props.StringProperty(name="Layer Name") # type: ignore
|
||||
|
||||
series_id: bpy.props.StringProperty() # type: ignore
|
||||
|
||||
container_obj_name: bpy.props.StringProperty() # type: ignore
|
||||
|
||||
frame_count: bpy.props.IntProperty() # type: ignore
|
||||
|
||||
channel_count: bpy.props.IntProperty() # type: ignore
|
||||
|
||||
read_as: bpy.props.EnumProperty(name="Read as",
|
||||
default="scalar",
|
||||
items=[("scalar", "Scalar", ""),
|
||||
("label", "Labels", "")]) # type: ignore
|
||||
|
||||
bioxel_size: bpy.props.FloatProperty(name="Bioxel Size (Larger size means small resolution)",
|
||||
soft_min=0.1, soft_max=10.0,
|
||||
min=0.1, max=1e2,
|
||||
default=1) # type: ignore
|
||||
|
||||
orig_spacing: bpy.props.FloatVectorProperty(name="Original Spacing",
|
||||
default=(1, 1, 1)) # type: ignore
|
||||
|
||||
orig_shape: bpy.props.IntVectorProperty(name="Original Shape",
|
||||
default=(100, 100, 100)) # type: ignore
|
||||
|
||||
scene_scale: bpy.props.FloatProperty(name="Scene Scale (Bioxel Unit pre Blender Unit)",
|
||||
soft_min=0.0001, soft_max=10.0,
|
||||
min=1e-6, max=1e6,
|
||||
default=0.01) # type: ignore
|
||||
|
||||
split_channels: bpy.props.BoolProperty(name="Split Channels",
|
||||
default=False) # type: ignore
|
||||
|
||||
sequence_source: bpy.props.EnumProperty(name="Time Sequence From",
|
||||
items=get_sequence_sources) # type: ignore
|
||||
|
||||
def execute(self, context):
|
||||
def import_volumetric_data_func(self, context, cancel):
|
||||
progress_update(context, 0.0, "Parsing Volumetirc Data...")
|
||||
|
||||
def progress_callback(factor, text):
|
||||
if cancel():
|
||||
raise CancelledByUser
|
||||
progress_update(context, factor*0.2, text)
|
||||
|
||||
try:
|
||||
data, meta = parse_volumetric_data(data_file=self.filepath,
|
||||
series_id=self.series_id,
|
||||
progress_callback=progress_callback)
|
||||
except CancelledByUser:
|
||||
return
|
||||
except Exception as e:
|
||||
self.has_error = e
|
||||
return
|
||||
|
||||
if cancel():
|
||||
return
|
||||
|
||||
shape = get_layer_shape(self.bioxel_size,
|
||||
self.orig_shape,
|
||||
self.orig_spacing)
|
||||
|
||||
mat_scale = transforms3d.zooms.zfdir2aff(self.bioxel_size)
|
||||
affine = np.dot(meta['affine'], mat_scale)
|
||||
kind = self.read_as
|
||||
|
||||
if cancel():
|
||||
return
|
||||
|
||||
# change shape as sequence or not
|
||||
if self.sequence_source == "-1":
|
||||
data = data[0:1, :, :, :, :]
|
||||
elif self.sequence_source == "0":
|
||||
# frame as frame
|
||||
pass
|
||||
elif self.sequence_source == "1":
|
||||
# X as frame
|
||||
data = data.transpose(1, 0, 2, 3, 4)
|
||||
shape = (1, shape[1], shape[2])
|
||||
elif self.sequence_source == "2":
|
||||
# Y as frame
|
||||
data = data.transpose(2, 1, 0, 3, 4)
|
||||
shape = (shape[0], 1, shape[2])
|
||||
elif self.sequence_source == "3":
|
||||
# Z as frame
|
||||
data = data.transpose(3, 1, 2, 0, 4)
|
||||
shape = (shape[0], shape[1], 1)
|
||||
else:
|
||||
# channel as frame
|
||||
data = data.transpose(4, 1, 2, 3, 0)
|
||||
|
||||
def progress_callback_factory(layer_name, progress, progress_step):
|
||||
def progress_callback(frame, total):
|
||||
if cancel():
|
||||
raise CancelledByUser
|
||||
sub_progress_step = progress_step/total
|
||||
sub_progress = progress + frame * sub_progress_step
|
||||
progress_update(context, sub_progress,
|
||||
f"Processing {layer_name} Frame {frame+1}...")
|
||||
return progress_callback
|
||||
|
||||
layers = []
|
||||
if kind == "label":
|
||||
name = self.layer_name or "Label"
|
||||
data = data.astype(int)
|
||||
label_count = int(np.max(data))
|
||||
progress_step = 0.7/label_count
|
||||
|
||||
for i in range(label_count):
|
||||
if cancel():
|
||||
return
|
||||
|
||||
name_i = f"{name}_{i+1}"
|
||||
progress = 0.2+i*progress_step
|
||||
progress_update(context, progress,
|
||||
f"Processing {name_i}...")
|
||||
|
||||
progress_callback = progress_callback_factory(name_i,
|
||||
progress,
|
||||
progress_step)
|
||||
try:
|
||||
layer = Layer(data=data == np.full_like(data, i+1),
|
||||
name=name_i,
|
||||
kind=kind,
|
||||
affine=affine)
|
||||
|
||||
layer.resize(shape=shape,
|
||||
progress_callback=progress_callback)
|
||||
|
||||
layers.append(layer)
|
||||
except CancelledByUser:
|
||||
return
|
||||
except Exception as e:
|
||||
self.has_error = e
|
||||
return
|
||||
|
||||
elif kind == "scalar":
|
||||
name = self.layer_name or "Scalar"
|
||||
|
||||
if self.split_channels:
|
||||
progress_step = 0.7/self.channel_count
|
||||
|
||||
for i in range(self.channel_count):
|
||||
if cancel():
|
||||
return
|
||||
|
||||
name_i = f"{name}_{i+1}"
|
||||
progress = 0.2 + i*progress_step
|
||||
progress_update(context, progress,
|
||||
f"Processing {name_i}...")
|
||||
progress_callback = progress_callback_factory(name_i,
|
||||
progress,
|
||||
progress_step)
|
||||
try:
|
||||
layer = Layer(data=data[:, :, :, :, i:i+1],
|
||||
name=name_i,
|
||||
kind=kind,
|
||||
affine=affine)
|
||||
|
||||
layer.resize(shape=shape,
|
||||
progress_callback=progress_callback)
|
||||
|
||||
layers.append(layer)
|
||||
except CancelledByUser:
|
||||
return
|
||||
except Exception as e:
|
||||
self.has_error = e
|
||||
return
|
||||
else:
|
||||
if cancel():
|
||||
return
|
||||
|
||||
progress_update(context, 0.2,
|
||||
f"Processing {name}...")
|
||||
progress_callback = progress_callback_factory(name,
|
||||
0.2,
|
||||
0.7)
|
||||
|
||||
try:
|
||||
layer = Layer(data=data,
|
||||
name=name,
|
||||
kind=kind,
|
||||
affine=affine)
|
||||
|
||||
layer.resize(shape=shape,
|
||||
progress_callback=progress_callback)
|
||||
|
||||
layers.append(layer)
|
||||
except CancelledByUser:
|
||||
return
|
||||
except Exception as e:
|
||||
self.has_error = e
|
||||
return
|
||||
|
||||
if cancel():
|
||||
return
|
||||
|
||||
self.layers = layers
|
||||
progress_update(context, 0.9, "Creating Layers...")
|
||||
|
||||
self.is_cancelled = False
|
||||
self.has_error = None
|
||||
|
||||
self.thread = threading.Thread(target=import_volumetric_data_func,
|
||||
args=(self, context, lambda: self.is_cancelled))
|
||||
|
||||
self.thread.start()
|
||||
self._timer = context.window_manager.event_timer_add(time_step=0.1,
|
||||
window=context.window)
|
||||
bpy.types.STATUSBAR_HT_header.append(progress_bar)
|
||||
|
||||
context.window_manager.modal_handler_add(self)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
def modal(self, context, event):
|
||||
if event.type == 'ESC':
|
||||
self.is_cancelled = True
|
||||
progress_update(context, 0, "Canceling...")
|
||||
return {'PASS_THROUGH'}
|
||||
|
||||
if event.type != 'TIMER':
|
||||
return {'PASS_THROUGH'}
|
||||
|
||||
bpy.context.workspace.status_text_set_internal(None)
|
||||
if self.thread.is_alive():
|
||||
return {'PASS_THROUGH'}
|
||||
|
||||
self.thread.join()
|
||||
context.window_manager.event_timer_remove(self._timer)
|
||||
bpy.types.STATUSBAR_HT_header.remove(progress_bar)
|
||||
|
||||
if self.is_cancelled:
|
||||
self.report({"WARNING"}, "Canncelled by user.")
|
||||
return {'CANCELLED'}
|
||||
|
||||
# Check if thread is cancelled by user
|
||||
if self.has_error:
|
||||
raise self.has_error
|
||||
|
||||
# Check if has return
|
||||
if self.layers is None:
|
||||
self.report({"ERROR"}, "Some thing went wrong.")
|
||||
return {'CANCELLED'}
|
||||
|
||||
is_first_import = len(get_all_layer_objs()) == 0
|
||||
|
||||
if self.container_obj_name:
|
||||
container_obj = bpy.data.objects.get(self.container_obj_name)
|
||||
if container_obj is None:
|
||||
raise Exception("Could not find target container")
|
||||
|
||||
container_obj = add_layers(self.layers,
|
||||
container_obj=container_obj,
|
||||
cache_dir=get_cache_dir(context))
|
||||
else:
|
||||
container = Container(name=self.container_name,
|
||||
layers=self.layers)
|
||||
|
||||
container_obj = container_to_obj(container,
|
||||
scene_scale=self.scene_scale,
|
||||
cache_dir=get_cache_dir(context))
|
||||
|
||||
select_object(container_obj)
|
||||
|
||||
# Change render setting for better result
|
||||
preferences = get_preferences(context)
|
||||
if preferences.do_change_render_setting and is_first_import:
|
||||
bpy.context.scene.render.engine = 'CYCLES'
|
||||
try:
|
||||
bpy.context.scene.cycles.shading_system = True
|
||||
bpy.context.scene.cycles.volume_bounces = 12
|
||||
bpy.context.scene.cycles.transparent_max_bounces = 16
|
||||
bpy.context.scene.cycles.volume_preview_step_rate = 10
|
||||
bpy.context.scene.cycles.volume_step_rate = 10
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
bpy.context.scene.eevee.use_taa_reprojection = False
|
||||
bpy.context.scene.eevee.volumetric_tile_size = '2'
|
||||
bpy.context.scene.eevee.volumetric_shadow_samples = 128
|
||||
bpy.context.scene.eevee.volumetric_samples = 256
|
||||
bpy.context.scene.eevee.use_volumetric_shadows = True
|
||||
except:
|
||||
pass
|
||||
|
||||
self.report({"INFO"}, "Successfully Imported")
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
if self.read_as == "label":
|
||||
volume_dtype = "Label"
|
||||
elif self.read_as == "scalar":
|
||||
volume_dtype = "Scalar"
|
||||
title = f"As {volume_dtype} Opitons (Add to Container: {self.container_obj_name})" \
|
||||
if self.container_obj_name != "" else f"As {volume_dtype} Options (Init a Container)"
|
||||
context.window_manager.invoke_props_dialog(self,
|
||||
width=500,
|
||||
title=title)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
def draw(self, context):
|
||||
layer_shape = get_layer_shape(self.bioxel_size,
|
||||
self.orig_shape,
|
||||
self.orig_spacing)
|
||||
|
||||
orig_shape = tuple(self.orig_shape)
|
||||
|
||||
# change shape as sequence or not
|
||||
channel_count = self.channel_count
|
||||
if self.sequence_source == "-1":
|
||||
pass
|
||||
elif self.sequence_source == "0":
|
||||
# frame as frame
|
||||
pass
|
||||
elif self.sequence_source == "1":
|
||||
layer_shape = (1, layer_shape[1], layer_shape[2])
|
||||
elif self.sequence_source == "2":
|
||||
layer_shape = (layer_shape[0], 1, layer_shape[2])
|
||||
elif self.sequence_source == "3":
|
||||
layer_shape = (layer_shape[0], layer_shape[1], 1)
|
||||
else:
|
||||
channel_count = 1
|
||||
|
||||
import_channel = channel_count if self.split_channels or channel_count == 1 else "combined"
|
||||
|
||||
bioxel_count = layer_shape[0] * layer_shape[1] * layer_shape[2]
|
||||
layer_shape_text = f"Shape from {str(orig_shape)} to {str(layer_shape)}"
|
||||
if bioxel_count > 100000000:
|
||||
layer_shape_text += "**TOO LARGE!**"
|
||||
|
||||
layout = self.layout
|
||||
if self.container_obj_name == "":
|
||||
layout.prop(self, "container_name")
|
||||
layout.prop(self, "layer_name")
|
||||
|
||||
panel = layout.box()
|
||||
panel.prop(self, "bioxel_size")
|
||||
row = panel.row()
|
||||
row.prop(self, "orig_spacing")
|
||||
panel.label(text=layer_shape_text)
|
||||
|
||||
panel = layout.box()
|
||||
panel.prop(self, "sequence_source")
|
||||
|
||||
if self.read_as == "scalar":
|
||||
panel = layout.box()
|
||||
panel.prop(self, "split_channels",
|
||||
text=f"Split Channels (Get {channel_count} channels, import {import_channel} channels)")
|
||||
|
||||
if self.container_obj_name == "":
|
||||
layer_size = get_layer_size(layer_shape,
|
||||
self.bioxel_size,
|
||||
self.scene_scale)
|
||||
layer_size_text = f"Size will be: ({layer_size[0]:.2f}, {layer_size[1]:.2f}, {layer_size[2]:.2f}) m"
|
||||
panel = layout.box()
|
||||
panel.prop(self, "scene_scale")
|
||||
panel.label(text=layer_size_text)
|
||||
|
||||
|
||||
class ExportVolumetricData(bpy.types.Operator):
|
||||
bl_idname = "bioxelnodes.export_volumetric_data"
|
||||
bl_label = "Export Layer as VDB"
|
||||
bl_description = "Export Layer as VDB"
|
||||
bl_options = {'UNDO'}
|
||||
|
||||
filepath: bpy.props.StringProperty(
|
||||
subtype="FILE_PATH"
|
||||
) # type: ignore
|
||||
|
||||
filename_ext = ".vdb"
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
layer_obj = get_layer_obj(bpy.context.active_object)
|
||||
return True if layer_obj else False
|
||||
|
||||
def invoke(self, context, event):
|
||||
context.window_manager.fileselect_add(self)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
def execute(self, context):
|
||||
layer_obj = get_layer_obj(bpy.context.active_object)
|
||||
|
||||
filepath = f"{self.filepath.split('.')[0]}.vdb"
|
||||
# "//"
|
||||
source_dir = bpy.path.abspath(layer_obj.data.filepath)
|
||||
|
||||
output_path: Path = Path(filepath).resolve()
|
||||
source_path: Path = Path(source_dir).resolve()
|
||||
# print('output_path', output_path)
|
||||
# print('source_path', source_path)
|
||||
shutil.copy(source_path, output_path)
|
||||
|
||||
self.report({"INFO"}, f"Successfully exported to {output_path}")
|
||||
|
||||
return {'FINISHED'}
|
||||
251
bioxelnodes/operators/layer.py
Normal file
251
bioxelnodes/operators/layer.py
Normal file
@@ -0,0 +1,251 @@
|
||||
from pathlib import Path
|
||||
import bpy
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
from ..bioxel.layer import Layer
|
||||
from ..bioxelutils.node import get_nodes_by_type
|
||||
from ..bioxelutils.container import add_layers, get_container_obj
|
||||
from ..bioxelutils.layer import obj_to_layer, get_container_layer_objs, get_layer_obj
|
||||
from .utils import get_preferences, select_object
|
||||
|
||||
|
||||
def get_layer_prop_value(layer_obj: bpy.types.Object, prop: str):
|
||||
try:
|
||||
node_group = layer_obj.modifiers[0].node_group
|
||||
layer_node = get_nodes_by_type(node_group, "BioxelNodes__Layer")[0]
|
||||
return layer_node.inputs[prop].default_value
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def get_label_layer_selection(self, context):
|
||||
items = [("None", "None", "")]
|
||||
container_obj = get_container_obj(bpy.context.active_object)
|
||||
|
||||
for layer_obj in get_container_layer_objs(container_obj):
|
||||
if get_layer_prop_value(layer_obj, "kind") == "label":
|
||||
items.append((layer_obj.name,
|
||||
layer_obj.name,
|
||||
""))
|
||||
|
||||
return items
|
||||
|
||||
|
||||
class LayerOperator():
|
||||
bl_options = {'UNDO'}
|
||||
base_obj: bpy.types.Object = None
|
||||
|
||||
def add_layer(self, context, layer):
|
||||
preferences = get_preferences(context)
|
||||
cache_dir = Path(preferences.cache_dir, 'VDBs')
|
||||
container_obj = self.base_obj.parent
|
||||
|
||||
add_layers([layer],
|
||||
container_obj,
|
||||
cache_dir)
|
||||
|
||||
select_object(container_obj)
|
||||
|
||||
def invoke(self, context, event):
|
||||
self.base_obj = get_layer_obj(bpy.context.active_object)
|
||||
return self.execute(context)
|
||||
|
||||
|
||||
class ScalarOperator(LayerOperator):
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
label_objs = [obj for obj in context.selected_ids
|
||||
if get_layer_prop_value(obj, "kind") == "scalar"]
|
||||
return True if len(label_objs) > 0 else False
|
||||
|
||||
|
||||
class LabelOperator(LayerOperator):
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
label_objs = [obj for obj in context.selected_ids
|
||||
if get_layer_prop_value(obj, "kind") == "label"]
|
||||
return True if len(label_objs) > 0 else False
|
||||
|
||||
|
||||
class SignScalar(bpy.types.Operator, ScalarOperator):
|
||||
bl_idname = "bioxelnodes.sign_scalar"
|
||||
bl_label = "Sign Scalar"
|
||||
bl_description = "Sign the scalar value"
|
||||
|
||||
def execute(self, context):
|
||||
base_layer = obj_to_layer(self.base_obj)
|
||||
|
||||
modified_layer = base_layer.copy()
|
||||
modified_layer.data = -base_layer.data
|
||||
modified_layer.name = f"{base_layer.name}_Signed"
|
||||
|
||||
self.add_layer(context, modified_layer)
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class FillOperator(ScalarOperator):
|
||||
def invoke(self, context, event):
|
||||
self.base_obj = get_layer_obj(bpy.context.active_object)
|
||||
scalar_min = get_layer_prop_value(self.base_obj, "min")
|
||||
self.fill_value = min(scalar_min, 0)
|
||||
context.window_manager.invoke_props_dialog(self, width=400)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
|
||||
class FillByThreshold(bpy.types.Operator, FillOperator):
|
||||
bl_idname = "bioxelnodes.fill_by_threshold"
|
||||
bl_label = "Fill Value by Threshold"
|
||||
bl_description = "Fill Value by Threshold"
|
||||
|
||||
threshold: bpy.props.FloatProperty(
|
||||
name="Threshold",
|
||||
soft_min=0, soft_max=1024,
|
||||
default=128,
|
||||
) # type: ignore
|
||||
|
||||
fill_value: bpy.props.FloatProperty(
|
||||
name="Fill Value",
|
||||
soft_min=0, soft_max=1024.0,
|
||||
default=0,
|
||||
) # type: ignore
|
||||
|
||||
invert: bpy.props.BoolProperty(
|
||||
name="Invert Area",
|
||||
default=True,
|
||||
) # type: ignore
|
||||
|
||||
def execute(self, context):
|
||||
base_layer = obj_to_layer(self.base_obj)
|
||||
|
||||
data = np.amax(base_layer.data, -1)
|
||||
mask = data <= self.threshold \
|
||||
if self.invert else data > self.threshold
|
||||
|
||||
modified_layer = base_layer.copy()
|
||||
modified_layer.fill(self.fill_value, mask)
|
||||
modified_layer.name = f"{base_layer.name}_{self.threshold}-Filled"
|
||||
|
||||
self.add_layer(context, modified_layer)
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class FillByRange(bpy.types.Operator, FillOperator):
|
||||
bl_idname = "bioxelnodes.fill_by_range"
|
||||
bl_label = "Fill Value by Range"
|
||||
bl_description = "Fill Value by Range"
|
||||
|
||||
from_min: bpy.props.FloatProperty(
|
||||
name="From Min",
|
||||
soft_min=0, soft_max=1024,
|
||||
default=128,
|
||||
) # type: ignore
|
||||
|
||||
from_max: bpy.props.FloatProperty(
|
||||
name="From Max",
|
||||
soft_min=0, soft_max=1024,
|
||||
default=256,
|
||||
) # type: ignore
|
||||
|
||||
fill_value: bpy.props.FloatProperty(
|
||||
name="Fill Value",
|
||||
soft_min=0, soft_max=1024.0,
|
||||
default=0,
|
||||
) # type: ignore
|
||||
|
||||
invert: bpy.props.BoolProperty(
|
||||
name="Invert Area",
|
||||
default=True,
|
||||
) # type: ignore
|
||||
|
||||
def execute(self, context):
|
||||
base_layer = obj_to_layer(self.base_obj)
|
||||
|
||||
data = np.amax(base_layer.data, -1)
|
||||
mask = (data <= self.from_min) | (data >= self.from_max) if self.invert else \
|
||||
(data > self.from_min) & (data < self.from_max)
|
||||
|
||||
modified_layer = base_layer.copy()
|
||||
modified_layer.fill(self.fill_value, mask)
|
||||
modified_layer.name = f"{base_layer.name}_{self.from_min}-{self.from_max}-Filled"
|
||||
|
||||
self.add_layer(context, modified_layer)
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class FillByLabel(bpy.types.Operator, FillOperator):
|
||||
bl_idname = "bioxelnodes.fill_by_label"
|
||||
bl_label = "Fill Value by Label"
|
||||
bl_description = "Fill Value by Label Area"
|
||||
|
||||
label_obj_name: bpy.props.EnumProperty(
|
||||
name="Label Layer",
|
||||
items=get_label_layer_selection
|
||||
) # type: ignore
|
||||
|
||||
fill_value: bpy.props.FloatProperty(
|
||||
name="Fill Value",
|
||||
soft_min=0, soft_max=1024.0,
|
||||
default=0,
|
||||
) # type: ignore
|
||||
|
||||
invert: bpy.props.BoolProperty(
|
||||
name="Invert Label",
|
||||
default=True,
|
||||
) # type: ignore
|
||||
|
||||
def execute(self, context):
|
||||
label_obj = bpy.data.objects.get(self.label_obj_name)
|
||||
if not label_obj:
|
||||
self.report({"WARNING"}, "Cannot find any label layer.")
|
||||
return {'FINISHED'}
|
||||
|
||||
base_layer = obj_to_layer(self.base_obj)
|
||||
|
||||
label_layer = obj_to_layer(label_obj)
|
||||
label_layer.resize(base_layer.shape)
|
||||
mask = np.amax(label_layer.data, -1)
|
||||
if self.invert:
|
||||
mask = 1 - mask
|
||||
|
||||
modified_layer = base_layer.copy()
|
||||
modified_layer.fill(self.fill_value, mask)
|
||||
modified_layer.name = f"{base_layer.name}_{label_layer.name}-Filled"
|
||||
|
||||
self.add_layer(context, modified_layer)
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class CombineLabels(bpy.types.Operator, LabelOperator):
|
||||
bl_idname = "bioxelnodes.combine_labels"
|
||||
bl_label = "Combine Labels"
|
||||
bl_description = "Combine all selected labels"
|
||||
|
||||
def execute(self, context):
|
||||
label_objs = [obj for obj in context.selected_ids
|
||||
if get_layer_prop_value(obj, "kind") == "label"]
|
||||
|
||||
if len(label_objs) < 2:
|
||||
self.report({"WARNING"}, "Not enough layers.")
|
||||
return {'FINISHED'}
|
||||
base_obj = label_objs[0]
|
||||
label_objs = label_objs[1:]
|
||||
container_obj = base_obj.parent
|
||||
|
||||
base_layer = obj_to_layer(base_obj)
|
||||
modified_layer = base_layer.copy()
|
||||
label_names = [base_layer.name]
|
||||
|
||||
for label_obj in label_objs:
|
||||
label_layer = obj_to_layer(label_obj)
|
||||
label_layer.resize(base_layer.shape)
|
||||
modified_layer.data = np.maximum(base_layer.data, label_layer.data)
|
||||
label_names.append(label_layer.name)
|
||||
|
||||
modified_layer.name = f"{'-'.join(label_names)}-Combined"
|
||||
|
||||
self.add_layer(context, modified_layer)
|
||||
|
||||
return {'FINISHED'}
|
||||
215
bioxelnodes/operators/misc.py
Normal file
215
bioxelnodes/operators/misc.py
Normal file
@@ -0,0 +1,215 @@
|
||||
import re
|
||||
import bpy
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
|
||||
|
||||
from ..utils import copy_to_dir
|
||||
from .utils import get_cache_dir
|
||||
from ..nodes import custom_nodes
|
||||
from ..bioxelutils.container import get_container_objs_from_selection
|
||||
from ..bioxelutils.layer import get_all_layer_objs, get_container_layer_objs
|
||||
|
||||
CLASS_PREFIX = "BIOXELNODES_MT_NODES"
|
||||
|
||||
|
||||
class ReLinkNodes(bpy.types.Operator):
|
||||
bl_idname = "bioxelnodes.relink_nodes"
|
||||
bl_label = "Relink Nodes to Addon"
|
||||
bl_description = "Relink all nodes to addon source"
|
||||
|
||||
def execute(self, context):
|
||||
file_name = Path(custom_nodes.nodes_file).name
|
||||
for lib in bpy.data.libraries:
|
||||
lib_path = Path(bpy.path.abspath(lib.filepath)).resolve()
|
||||
lib_name = lib_path.name
|
||||
if lib_name == file_name:
|
||||
if str(lib_path) != custom_nodes.nodes_file:
|
||||
lib.filepath = custom_nodes.nodes_file
|
||||
|
||||
self.report({"INFO"}, f"Successfully relinked.")
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
def save_layer_cache(layer_obj, output_dir):
|
||||
pattern = r'\.\d{4}\.'
|
||||
|
||||
# "//"
|
||||
output_dir = bpy.path.abspath(output_dir)
|
||||
source_dir = bpy.path.abspath(layer_obj.data.filepath)
|
||||
|
||||
source_path: Path = Path(source_dir).resolve()
|
||||
is_sequence = re.search(pattern, source_path.name) is not None
|
||||
name = layer_obj.name if is_sequence else f"{layer_obj.name}.vdb"
|
||||
output_path: Path = Path(output_dir, name, source_path.name).resolve() \
|
||||
if is_sequence else Path(output_dir, name).resolve()
|
||||
|
||||
if output_path != source_path:
|
||||
copy_to_dir(source_path.parent if is_sequence else source_path,
|
||||
output_path.parent.parent if is_sequence else output_path.parent,
|
||||
new_name=name)
|
||||
|
||||
blend_path = Path(bpy.path.abspath("//")).resolve()
|
||||
|
||||
layer_obj.data.filepath = bpy.path.relpath(
|
||||
str(output_path), start=str(blend_path))
|
||||
|
||||
|
||||
class SaveStagedData(bpy.types.Operator):
|
||||
bl_idname = "bioxelnodes.save_staged_data"
|
||||
bl_label = "Save Staged Data"
|
||||
bl_description = "Save all staged data in this file for sharing"
|
||||
|
||||
save_layer: bpy.props.BoolProperty(
|
||||
name="Save Layer VDB Cache",
|
||||
default=True,
|
||||
) # type: ignore
|
||||
|
||||
cache_dir: bpy.props.StringProperty(
|
||||
name="Cache Directory",
|
||||
subtype='DIR_PATH',
|
||||
default="//"
|
||||
) # type: ignore
|
||||
|
||||
save_lib: bpy.props.BoolProperty(
|
||||
name="Save Node Library File",
|
||||
default=True,
|
||||
) # type: ignore
|
||||
|
||||
lib_dir: bpy.props.StringProperty(
|
||||
name="Library Directory",
|
||||
subtype='DIR_PATH',
|
||||
default="//"
|
||||
) # type: ignore
|
||||
|
||||
def execute(self, context):
|
||||
if self.save_lib:
|
||||
files = []
|
||||
for classname in dir(bpy.types):
|
||||
if CLASS_PREFIX in classname:
|
||||
cls = getattr(bpy.types, classname)
|
||||
files.append(cls.nodes_file)
|
||||
files = list(set(files))
|
||||
|
||||
for file in files:
|
||||
file_name = Path(file).name
|
||||
# "//"
|
||||
lib_dir = bpy.path.abspath(self.lib_dir)
|
||||
|
||||
output_path: Path = Path(lib_dir, file_name).resolve()
|
||||
source_path: Path = Path(file).resolve()
|
||||
|
||||
if output_path != source_path:
|
||||
shutil.copy(source_path, output_path)
|
||||
|
||||
for lib in bpy.data.libraries:
|
||||
lib_path = Path(bpy.path.abspath(lib.filepath)).resolve()
|
||||
if lib_path == source_path:
|
||||
blend_path = Path(bpy.path.abspath("//")).resolve()
|
||||
lib.filepath = bpy.path.relpath(
|
||||
str(output_path), start=str(blend_path))
|
||||
|
||||
self.report({"INFO"}, f"Successfully saved to {output_path}")
|
||||
|
||||
if self.save_layer:
|
||||
fails = []
|
||||
for layer in get_all_layer_objs():
|
||||
try:
|
||||
save_layer(layer, self.cache_dir)
|
||||
except:
|
||||
fails.append(layer)
|
||||
|
||||
if len(fails) == 0:
|
||||
self.report({"INFO"}, f"Successfully saved bioxel layers.")
|
||||
else:
|
||||
self.report(
|
||||
{"WARNING"}, f"{','.join([layer.name for layer in fails])} fail to save.")
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
context.window_manager.invoke_props_dialog(self,
|
||||
width=500)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
return bpy.data.is_saved
|
||||
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
panel = layout.box()
|
||||
panel.prop(self, "save_layer")
|
||||
panel.prop(self, "cache_dir")
|
||||
panel = layout.box()
|
||||
panel.prop(self, "save_lib")
|
||||
panel.prop(self, "lib_dir")
|
||||
|
||||
|
||||
class SaveCaches(bpy.types.Operator):
|
||||
bl_idname = "bioxelnodes.save_layers"
|
||||
bl_label = "Save Caches"
|
||||
bl_description = "Save Container's caches to directory."
|
||||
|
||||
cache_dir: bpy.props.StringProperty(
|
||||
name="Layer Directory",
|
||||
subtype='DIR_PATH',
|
||||
default="//"
|
||||
) # type: ignore
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
container_objs = get_container_objs_from_selection()
|
||||
return len(container_objs) > 0
|
||||
|
||||
def execute(self, context):
|
||||
container_objs = get_container_objs_from_selection()
|
||||
|
||||
if len(container_objs) == 0:
|
||||
self.report({"WARNING"}, "Cannot find any bioxel container.")
|
||||
return {'FINISHED'}
|
||||
|
||||
fails = []
|
||||
for container_obj in container_objs:
|
||||
for layer_obj in get_container_layer_objs(container_obj):
|
||||
try:
|
||||
save_layer_cache(layer_obj, self.cache_dir)
|
||||
except:
|
||||
fails.append(layer_obj)
|
||||
|
||||
if len(fails) == 0:
|
||||
self.report({"INFO"}, f"Successfully saved bioxel layers.")
|
||||
else:
|
||||
self.report(
|
||||
{"WARNING"}, f"{','.join([layer.name for layer in fails])} fail to save.")
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
context.window_manager.invoke_props_dialog(self,
|
||||
width=500)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
|
||||
class CleanAllCaches(bpy.types.Operator):
|
||||
bl_idname = "bioxelnodes.clear_all_caches"
|
||||
bl_label = "Clean All Caches in Temp"
|
||||
bl_description = "Clean all caches saved in temp"
|
||||
|
||||
def execute(self, context):
|
||||
cache_dir = get_cache_dir(context)
|
||||
try:
|
||||
shutil.rmtree(cache_dir)
|
||||
self.report({"INFO"}, f"Successfully cleaned caches.")
|
||||
return {'FINISHED'}
|
||||
except:
|
||||
self.report({"WARNING"},
|
||||
"Fail to clean caches, you may do it manually.")
|
||||
return {'CANCELLED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
context.window_manager.invoke_confirm(self,
|
||||
event,
|
||||
message="All caches will be cleaned, include other project files, do you still want to clean?")
|
||||
return {'RUNNING_MODAL'}
|
||||
37
bioxelnodes/operators/utils.py
Normal file
37
bioxelnodes/operators/utils.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import bpy
|
||||
from pathlib import Path
|
||||
from .. import __package__ as base_package
|
||||
|
||||
|
||||
def select_object(target_obj):
|
||||
for obj in bpy.data.objects:
|
||||
obj.select_set(False)
|
||||
|
||||
target_obj.select_set(True)
|
||||
bpy.context.view_layer.objects.active = target_obj
|
||||
|
||||
|
||||
def progress_bar(self, context):
|
||||
row = self.layout.row()
|
||||
row.progress(
|
||||
factor=context.window_manager.bioxelnodes_progress_factor,
|
||||
type="BAR",
|
||||
text=context.window_manager.bioxelnodes_progress_text
|
||||
)
|
||||
row.scale_x = 2
|
||||
|
||||
|
||||
def progress_update(context, factor, text=""):
|
||||
context.window_manager.bioxelnodes_progress_factor = factor
|
||||
context.window_manager.bioxelnodes_progress_text = text
|
||||
|
||||
|
||||
def get_preferences(context):
|
||||
return context.preferences.addons[base_package].preferences
|
||||
|
||||
|
||||
def get_cache_dir(context):
|
||||
preferences = get_preferences(context)
|
||||
cache_path = Path(preferences.cache_dir, 'VDBs')
|
||||
cache_path.mkdir(parents=True, exist_ok=True)
|
||||
return str(cache_path)
|
||||
Reference in New Issue
Block a user