Add new functionality for image rectification and depth metadata handling

- Implemented a preview tool for rectifying images with interactive aspect controls in `preview.py`.
- Added tests for depth metadata loading to ensure consistent schema usage in `test_depth_metadata.py`.
- Developed comprehensive tests for cutout calibration processes in `test_cutout_calibration.py`, including stability checks and depth calibration.
- Created tests for frame compositing operations in `test_frame_compositing.py`, ensuring correct handling of depth and transparency.
- Introduced material slot inheritance functionality in `material_slots.py` to manage geometry material assignments.
- Built shared shader nodes for material processing in `shader.py`, including roughness and bump mapping setups.
This commit is contained in:
2026-09-12 19:20:06 +08:00
parent e8268504c2
commit 938d95db0f
175 changed files with 3812 additions and 6864 deletions

View File

@@ -15,14 +15,14 @@ from ..common.cutout_boundary import remove_boundary_triangles, taper_split_prof
NODE_GROUP_NAME = "O Image Depth Cutout"
def build_normal_weight(group, uniform):
def build_normal_weight(group, is_shell):
"""Scale normal smoothing by the active mode's relative thickness."""
control = group_input(group.nodes, {"Thickness", "Uniform Thickness", "Reference Depth"})
control = group_input(group.nodes, {"Thickness", "Shell Thickness", "Reference Depth"})
amount = group.nodes.new("GeometryNodeSwitch")
amount.input_type = "FLOAT"
group.links.new(uniform, amount.inputs["Switch"])
group.links.new(is_shell, amount.inputs["Switch"])
group.links.new(control.outputs["Thickness"], amount.inputs["False"])
ratio = _math(group, "DIVIDE", control.outputs["Uniform Thickness"],
ratio = _math(group, "DIVIDE", control.outputs["Shell Thickness"],
_math(group, "MAXIMUM", control.outputs["Reference Depth"], 1e-8))
group.links.new(ratio, amount.inputs["True"])
ramp = group.nodes.new("ShaderNodeMapRange")
@@ -73,7 +73,7 @@ def build_volume_fields(group):
return {"rim": rim, "center": center, "thickness": thickness}
def build_shell_fields(group, geometry, fields, uniform, normal_weight=1):
def build_shell_fields(group, geometry, fields, is_shell, normal_weight=1):
"""Evaluate rear directions on the center surface and select shell thickness."""
nodes, links = group.nodes, group.links
position = nodes.new("GeometryNodeInputPosition")
@@ -93,12 +93,12 @@ def build_shell_fields(group, geometry, fields, uniform, normal_weight=1):
links.new(normal.outputs[0], blur.inputs["Value"])
normalized = blend_normal(group, normal.outputs[0], blur.outputs[0], normal_weight)
rear_normal = sample_field(group, surface.outputs[0], normalized, "FLOAT_VECTOR")
control = group_input(nodes, {"Uniform Thickness"})
control = group_input(nodes, {"Shell Thickness"})
thickness = nodes.new("GeometryNodeSwitch")
thickness.input_type = "FLOAT"
links.new(uniform, thickness.inputs["Switch"])
links.new(is_shell, thickness.inputs["Switch"])
links.new(fields["thickness"], thickness.inputs["False"])
links.new(control.outputs["Uniform Thickness"], thickness.inputs["True"])
links.new(control.outputs["Shell Thickness"], thickness.inputs["True"])
return {"thickness": thickness.outputs[0], "normal": rear_normal}
@@ -145,8 +145,8 @@ def build_front_fields(group, rim, front_normal, normal_weight=1):
return {"structure": structure, "inflation_normal": normalized}
def build_inflation_position(group, position, distance, uniform, fields):
"""Grow both modes continuously while leaving the rear cap in place."""
def build_inflation_position(group, position, distance, fields):
"""Inflate the Balloon front while leaving the rear cap in place."""
links = group.links
structure = fields["structure"]
@@ -170,15 +170,7 @@ def build_inflation_position(group, position, distance, uniform, fields):
profile = group.nodes.new("GeometryNodeInputNamedAttribute")
profile.data_type = "FLOAT"
profile.inputs["Name"].default_value = "o_balloon"
balloon = _math(group, "MULTIPLY", profile.outputs["Attribute"], response)
uniform_control = group.nodes.new("NodeGroupInput")
uniform_amount = _math(group, "MULTIPLY", uniform_control.outputs["Uniform Thickness"], uniform_control.outputs["Front Inflation"])
selected = group.nodes.new("GeometryNodeSwitch")
selected.input_type = "FLOAT"
links.new(uniform, selected.inputs["Switch"])
links.new(balloon, selected.inputs["False"])
links.new(uniform_amount, selected.inputs["True"])
displacement = selected.outputs[0]
displacement = _math(group, "MULTIPLY", profile.outputs["Attribute"], response)
height = fields["thickness"]
layer = _math(group, "DIVIDE", distance, height)
layer = _math(group, "ADD", 1, layer)
@@ -213,7 +205,7 @@ def build_front_normal(group, normal_weight):
return evaluate_field(group, normalized, "FLOAT_VECTOR", "POINT")
def build_shell_position(group, position, front, uniform, fields):
def build_shell_position(group, position, front, is_shell, fields):
links = group.links
def connect(value, target):
@@ -234,13 +226,13 @@ def build_shell_position(group, position, front, uniform, fields):
links.new(value, result.inputs[0])
return result.outputs["Z"]
# Uniform and the thin end of Balloon share the actual front normal.
# Shell and the thin end of Balloon share the actual front normal.
front_z = z(front)
raw_z = z(position)
distance = _math(group, "SUBTRACT", raw_z, front_z)
normal = fields["front_normal"]
thin_offset = vector("SCALE", normal, distance)
uniform_position = vector("ADD", front, thin_offset)
shell_position = vector("ADD", front, thin_offset)
control = group_input(group.nodes, {"Thickness"})
clamp = group.nodes.new("ShaderNodeClamp")
@@ -267,16 +259,17 @@ def build_shell_position(group, position, front, uniform, fields):
balloon_position = vector("ADD", front, mix.outputs["Result"])
choose = group.nodes.new("GeometryNodeSwitch")
choose.input_type = "VECTOR"
links.new(uniform, choose.inputs["Switch"])
links.new(balloon_position, choose.inputs["False"])
links.new(uniform_position, choose.inputs["True"])
return build_inflation_position(group, choose.outputs[0], distance, uniform, fields)
links.new(is_shell, choose.inputs["Switch"])
inflated = build_inflation_position(group, balloon_position, distance, fields)
links.new(inflated, choose.inputs["False"])
links.new(shell_position, choose.inputs["True"])
return choose.outputs[0]
def _build_depth_surface(group, geometry, controls):
"""Project the mesh and sample front-surface fields while shaping the shell."""
links = group.links
split = controls.outputs["Split Threshold"]
split = controls.outputs["Depth Split"]
image = controls.outputs["Depth Image"]
scale = controls.outputs["Uniform Scale"]
original = geometry
@@ -292,15 +285,15 @@ def _build_depth_surface(group, geometry, controls):
projection = smooth_cut_boundary(group, projection)
projection = taper_split_profile(group, original, projection)
# The mode field is shared by the geometry and thickness choices.
mode = menu_switch(group, "Mode", ("Balloon", "Uniform"), "BOOLEAN")
mode = menu_switch(group, "Mode", ("Balloon", "Shell"), "BOOLEAN")
mode.inputs["Balloon"].default_value = False
mode.inputs["Uniform"].default_value = True
uniform = mode.outputs[0]
mode.inputs["Shell"].default_value = True
is_shell = mode.outputs[0]
volume = build_volume_fields(group)
normal_weight = build_normal_weight(group, uniform)
normal_weight = build_normal_weight(group, is_shell)
normal = build_front_normal(group, normal_weight)
front = build_front_fields(group, volume["rim"], normal, normal_weight)
shell_fields = build_shell_fields(group, projection, volume, uniform, normal_weight)
shell_fields = build_shell_fields(group, projection, volume, is_shell, normal_weight)
thickness = sample_field(group, projection, shell_fields["thickness"], "FLOAT")
shell = thicken_depth_surface(
group, projection, thickness, back_mask=NORMAL_SCALE_ATTRIBUTE_NAME, weld_zero_thickness=True,
@@ -317,13 +310,13 @@ def _build_depth_surface(group, geometry, controls):
index=shell["source_index"])
for name, value in shell_fields.items()
})
oriented = build_shell_position(group, shell["position"], shell["front"], uniform, fields)
oriented = build_shell_position(group, shell["position"], shell["front"], is_shell, fields)
links.new(oriented, shell["position_input"])
amount = group.nodes.new("GeometryNodeSwitch")
amount.input_type = "FLOAT"
links.new(uniform, amount.inputs["Switch"])
links.new(is_shell, amount.inputs["Switch"])
links.new(controls.outputs["Thickness"], amount.inputs["False"])
links.new(controls.outputs["Uniform Thickness"], amount.inputs["True"])
links.new(controls.outputs["Shell Thickness"], amount.inputs["True"])
return _remove_zero_thickness(group, projection, shell["geometry"], amount.outputs[0])
@@ -335,29 +328,30 @@ def build_image_depth_cutout_group():
group = bpy.data.node_groups.new(name, 'GeometryNodeTree')
group.is_modifier, group.use_fake_user = (True, True)
group.color_tag = 'GEOMETRY'
group['anyimage_asset_revision'] = 23
group['anyimage_asset_revision'] = 26
interface_socket(group, 'Geometry', 'INPUT', 'NodeSocketGeometry')
interface_socket(group, 'Mode', 'INPUT', 'NodeSocketMenu')
float_input(group, 'Thickness', 1.0).description = 'Balloon profile multiplier; zero returns the sheet'
float_input(group, 'Uniform Thickness', 0.0, subtype='DISTANCE').description = 'Distance used only by Uniform thickness mode'
float_input(group, 'Thickness', 1.0).description = 'Control how much the surface bulges in Balloon mode. Zero removes the bulge.'
float_input(group, 'Shell Thickness', 0.5, subtype='DISTANCE').description = 'Add thickness behind the original surface while keeping the front surface in place.'
float_input(group, 'Depth Scale', 1.0)
float_input(group, 'Split Threshold', 0.1, maximum=1, subtype='FACTOR')
float_input(group, 'Depth Split', 0.1, maximum=1, subtype='FACTOR').description = 'Split the mesh where depth changes abruptly. Higher values detect smaller depth jumps. Zero disables splitting.'
iterations = interface_socket(group, 'Smooth', 'INPUT', 'NodeSocketInt')
iterations.description = 'Set the number of mesh smoothing passes. Higher values smooth the surface more but may soften details. Zero disables smoothing.'
iterations.default_value = 0
iterations.min_value, iterations.max_value = (0, 20)
options = group.interface.new_panel(name='Options', default_closed=True)
float_input(group, 'Cleanup Threshold', 0.0, parent=options)
float_input(group, 'Smooth Weight', 1.0, maximum=1, subtype='FACTOR', parent=options)
float_input(group, 'Smooth Weight', 1.0, maximum=1, subtype='FACTOR', parent=options).description = 'Control the strength of each mesh smoothing pass. Higher values move vertices more.'
float_input(group, 'Reference Depth', 1.0, subtype='DISTANCE', parent=options)
interface_socket(group, 'Depth Axis', 'INPUT', 'NodeSocketMenu', options).menu_expanded = True
boundary_smooth = interface_socket(group, 'Boundary Smooth', 'INPUT', 'NodeSocketInt', options)
boundary_smooth.default_value, boundary_smooth.min_value, boundary_smooth.max_value = 2, 1, 20
boundary_smooth.description = 'Boundary geometry iterations and depth blur steps per outline smoothing round'
boundary_smooth.description = 'Control boundary smoothing within each Outline Depth Fix pass. Higher values apply more smoothing per pass.'
outline_smooth = interface_socket(group, 'Outline Depth Fix', 'INPUT', 'NodeSocketInt', options)
outline_smooth.default_value, outline_smooth.min_value, outline_smooth.max_value = 16, 0, 200
outline_smooth.description = 'Adaptive ray-distance smoothing rounds around the original outline; preserves split boundary distance'
float_input(group, 'Front Inflation', 0.0, maximum=1, subtype='FACTOR', parent=options)
outline_smooth.description = 'Reduce abrupt depth changes near the original Cutout outline. Higher values apply more correction passes. Zero disables correction.'
float_input(group, 'Front Inflation', 0.5, maximum=1, subtype='FACTOR', parent=options).description = 'Control how much the front surface bulges in Balloon mode. Zero keeps the original front surface.'
data = group.interface.new_panel(name='Data', default_closed=True)
interface_socket(group, 'Inward Axis', 'INPUT', 'NodeSocketMenu', data).hide_in_modifier = True
float_input(group, 'Uniform Scale', 1.0, parent=data).hide_in_modifier = True
interface_socket(group, 'Depth Image', 'INPUT', 'NodeSocketImage', data).hide_in_modifier = True
interface_socket(group, 'Geometry', 'OUTPUT', 'NodeSocketGeometry')
@@ -373,7 +367,7 @@ def build_image_depth_cutout_group():
orient = group.nodes.new('GeometryNodeTransform')
orient.inputs['Rotation'].default_value = Matrix(CUTOUT_Z_TO_X).to_euler()
group.links.new(geometry, orient.inputs['Geometry'])
axis = menu_switch(group, 'Inward Axis', ('-Z', '+X'), 'GEOMETRY')
axis = menu_switch(group, 'Depth Axis', ('-Z', '+X'), 'GEOMETRY')
group.links.new(geometry, axis.inputs['-Z'])
group.links.new(orient.outputs[0], axis.inputs['+X'])
_shade_output(group, axis.outputs[0])
@@ -383,8 +377,8 @@ def build_image_depth_cutout_group():
continue
if item.name == 'Mode':
item.default_value = 'Balloon'
elif item.name == 'Inward Axis':
elif item.name == 'Depth Axis':
item.default_value = '-Z'
elif item.name == 'Uniform Thickness':
elif item.name == 'Shell Thickness':
item.name = 'Thickness'
return group