folder_path = '/Users/tkimura/Desktop/TCIA/manifest-1738120681225/TCGA-BLCA'
cc_scale = ax_slide_number * ax_slide_distance * size
ax_scale = size * pixel_pitch * pixel
if pixel_pitch * pixel < ax_slide_number * ax_slide_distance:
else : common_scale = ax_scale
def load_reference_image(image_path, object_name):
Load an image as an empty object and assign the specified name.
img = bpy.data.images.load(image_path)
print(f"Failed to load image: {image_path}, Error: {e}")
bpy.ops.object.add(type='EMPTY', location=(0, 0, 0))
empty_obj = bpy.context.object
empty_obj.empty_display_type = 'IMAGE'
empty_obj.name = object_name
class ImageSliderOperator(bpy.types.Operator):
bl_idname = "object.image_slider_operator"
bl_label = "Image Slider Operator"
def execute(self, context):
path_CT = folder_path + '/'
axis_type = context.scene.image_slider_axis
slider_value = context.scene.image_slider_property
if axis_type == 'AX': # Axial: Slide along the Y-axis (180-degree rotation along Z-axis)
ax_slides = ax_slide_number
image_path = path_CT + 'A/A' + str(slider_value) + '.jpg'
ax_distance = ax_slide_distance
# Remove existing image objects
for obj in bpy.context.scene.objects:
if obj.name.startswith("iA"):
bpy.data.objects.remove(obj, do_unlink=True)
image_obj = load_reference_image(image_path, "iA" + str(slider_value))
# 180-degree rotation along the Z-axis
image_obj.rotation_euler = (1.5708, 0, 0)
image_obj.location = (0, -(ax_distance / 50) * ((ax_slides + 1) / 2 - slider_value), 0)
image_obj.scale = (ax_scale, ax_scale, ax_scale)
elif axis_type == 'COR': # Coronal: Slide along the Z-axis (180-degree rotation along Y-axis)
image_path = path_CT + 'C/C' + str(slider_value) + '.jpg'
size_cor = ax_slide_number * ax_slide_distance * size
# Remove existing image objects
for obj in bpy.context.scene.objects:
if obj.name.startswith("iC"):
bpy.data.objects.remove(obj, do_unlink=True)
image_obj = load_reference_image(image_path, "iC" + str(slider_value))
# 180-degree rotation along the Y-axis
image_obj.rotation_euler = (0, 3.14159, 3.14159)
image_obj.location = (0, 0, (pixel_pitch / 50) * (cor_slides / 2 + 0.5 - slider_value))
image_obj.scale = (common_scale, common_scale, common_scale)
elif axis_type == 'SAG': # Sagittal: Same as Coronal (Slide along Z-axis, rotation along Y-axis)
image_path = path_CT + 'S/S' + str(slider_value) + '.jpg'
size_sag = ax_slide_number * ax_slide_distance * size
# Remove existing image objects
for obj in bpy.context.scene.objects:
if obj.name.startswith("iS"):
bpy.data.objects.remove(obj, do_unlink=True)
image_obj = load_reference_image(image_path, "iS" + str(slider_value))
# 180-degree rotation along the Y-axis
image_obj.rotation_euler = (0, 1.5708, 3.14159)
image_obj.location = (-(pixel_pitch / 50) * (sag_slides / 2 + 0.5 - slider_value), 0, 0)
image_obj.scale = (common_scale, common_scale, common_scale)
class ImageSliderPanel(bpy.types.Panel):
bl_label = "Image Slider Panel"
bl_idname = "OBJECT_PT_image_slider"
bl_space_type = 'VIEW_3D'
bl_category = 'Image Slider'
layout.prop(context.scene, 'image_slider_axis', expand=True) # Axis selection
layout.prop(context.scene, 'image_slider_property', slider=True) # Slider
def update_axis(self, context):
Update the slider's maximum value dynamically when the axis is changed.
if context.scene.image_slider_axis == 'AX':
bpy.types.Scene.image_slider_property = bpy.props.IntProperty(
elif context.scene.image_slider_axis in ['COR', 'SAG']:
bpy.types.Scene.image_slider_property = bpy.props.IntProperty(
# Refresh the UI to reflect the changes
bpy.context.scene.update()
def update_slider(self, context):
bpy.ops.object.image_slider_operator()
bpy.utils.register_class(ImageSliderOperator)
bpy.utils.register_class(ImageSliderPanel)
bpy.types.Scene.image_slider_axis = bpy.props.EnumProperty(
('AX', 'Axial', 'Axial slices'),
('COR', 'Coronal', 'Coronal slices'),
('SAG', 'Sagittal', 'Sagittal slices')
# Initialize the slider property
bpy.types.Scene.image_slider_property = bpy.props.IntProperty(
bpy.utils.unregister_class(ImageSliderOperator)
bpy.utils.unregister_class(ImageSliderPanel)
del bpy.types.Scene.image_slider_property
del bpy.types.Scene.image_slider_axis
if __name__ == "__main__":