Add tests and implement boundary smoothing and cutout functionality

- Introduced tests for boundary smoothing in `test_boundary_smoothing.py`, verifying that smoothing reduces stair-step artifacts while preserving boundary integrity.
- Added tests for cutout boundary behavior in `test_cutout_boundary.py`, ensuring proper handling of boundary triangles and normal blending.
- Created `test_temporary_attributes.py` to check the cleanup of temporary attributes across depth modifier branches, ensuring no warnings are raised.
- Implemented point culling functionality in `test_validity.py`, confirming that invalid points are removed without affecting adjacent geometry.
- Developed boundary smoothing logic in `boundary_smoothing.py`, allowing for depth smoothing while maintaining edge integrity.
- Added cutout boundary management in `cutout_boundary.py`, including functions for removing boundary triangles and tapering thickness profiles.
- Implemented validity checks in `validity.py` to ensure proper deletion of invalid vertices and associated wire edges.
This commit is contained in:
2026-09-11 20:37:20 +08:00
parent e8129fe495
commit 31074bd511
85 changed files with 2501 additions and 1451 deletions

View File

@@ -2,16 +2,51 @@
import bpy
from mathutils import Matrix
from anyimage.operators.cutout_tool.shape import CUTOUT_Z_TO_X, NORMAL_SCALE_ATTRIBUTE_NAME
from ..common.nodes import interface_socket, group_input, math_node, float_input, menu_switch, read_float_attribute, store_float_attribute, prepare_node_group, capture_fields, evaluate_field
from ..common.nodes import interface_socket, group_input, math_node, float_input, menu_switch, read_float_attribute, store_float_attribute, prepare_node_group, sample_field, evaluate_field
from ..common.smoothing import expand_smooth
from ..common.boundary_smoothing import smooth_boundary_camera, smooth_cut_boundary
from ..common.cutout import _island_cleanup, _position_field, _remove_zero_thickness, _shade_output
from ..common.depth_surface import _math, project_depth_surface, build_surface_camera, split_depth_surface, sample_face_camera, thicken_depth_surface
from ..common.smoothing import edge_boundary_field
from ..common.cutout_boundary import remove_boundary_triangles, taper_split_profile
NODE_GROUP_NAME = "O Image Depth Cutout"
def build_normal_weight(group, uniform):
"""Scale normal smoothing by the active mode's relative thickness."""
control = group_input(group.nodes, {"Thickness", "Uniform Thickness", "Reference Depth"})
amount = group.nodes.new("GeometryNodeSwitch")
amount.input_type = "FLOAT"
group.links.new(uniform, amount.inputs["Switch"])
group.links.new(control.outputs["Thickness"], amount.inputs["False"])
ratio = _math(group, "DIVIDE", control.outputs["Uniform Thickness"],
_math(group, "MAXIMUM", control.outputs["Reference Depth"], 1e-8))
group.links.new(ratio, amount.inputs["True"])
ramp = group.nodes.new("ShaderNodeMapRange")
ramp.interpolation_type = "SMOOTHSTEP"
ramp.clamp = True
group.links.new(amount.outputs[0], ramp.inputs["Value"])
return ramp.outputs["Result"]
def blend_normal(group, original, smoothed, weight):
"""Normalize the thickness-weighted blend on the consumer geometry."""
mix = group.nodes.new("ShaderNodeMix")
mix.data_type = "VECTOR"
if isinstance(weight, (int, float)):
mix.inputs[0].default_value = weight
else:
group.links.new(weight, mix.inputs[0])
group.links.new(original, mix.inputs[4])
group.links.new(smoothed, mix.inputs[5])
normalize = group.nodes.new("ShaderNodeVectorMath")
normalize.operation = "NORMALIZE"
group.links.new(mix.outputs["Result"], normalize.inputs[0])
return normalize.outputs[0]
def build_volume_fields(group):
"""Evaluate the center and thickness on the original front surface."""
links = group.links
@@ -37,7 +72,7 @@ def build_volume_fields(group):
return {"rim": rim, "center": center, "thickness": thickness}
def build_volume_rounding(group, geometry, fields, uniform):
def build_volume_rounding(group, geometry, fields, uniform, normal_weight=1):
links = group.links
control = group.nodes.new("NodeGroupInput")
@@ -87,39 +122,36 @@ def build_volume_rounding(group, geometry, fields, uniform):
links.new(normal.outputs[0], blur.inputs["Value"])
local = group.nodes.new("NodeGroupInput")
links.new(local.outputs["Normal Smooth"], blur.inputs["Iterations"])
normalize = group.nodes.new("ShaderNodeVectorMath")
normalize.operation = "NORMALIZE"
links.new(blur.outputs[0], normalize.inputs[0])
normalized = blend_normal(group, normal.outputs[0], blur.outputs[0], normal_weight)
index = group.nodes.new("GeometryNodeInputIndex")
sample = group.nodes.new("GeometryNodeSampleIndex")
sample.data_type, sample.domain = "FLOAT_VECTOR", "POINT"
links.new(surface.outputs[0], sample.inputs["Geometry"])
links.new(normalize.outputs[0], sample.inputs["Value"])
links.new(normalized, sample.inputs["Value"])
links.new(index.outputs[0], sample.inputs["Index"])
thickness = group.nodes.new("GeometryNodeSwitch")
thickness.input_type = "FLOAT"
links.new(uniform, thickness.inputs["Switch"])
links.new(total, thickness.inputs["False"])
links.new(control.outputs["Uniform Thickness"], thickness.inputs["True"])
carrier, captured = capture_fields(
group, geometry, "POINT",
edge_weight=("FLOAT", weight), front_delta=("FLOAT", delta),
thickness=("FLOAT", thickness.outputs[0]), normal=("VECTOR", sample.outputs[0]),
)
rounding = {
"edge_weight": weight, "front_delta": delta,
"thickness": thickness.outputs[0], "normal": sample.outputs[0],
}
displacement = group.nodes.new("ShaderNodeCombineXYZ")
links.new(captured["front_delta"], displacement.inputs["Z"])
links.new(delta, displacement.inputs["Z"])
front = group.nodes.new("GeometryNodeSetPosition")
links.new(carrier, front.inputs["Geometry"])
links.new(geometry, front.inputs["Geometry"])
links.new(displacement.outputs[0], front.inputs["Offset"])
choose = group.nodes.new("GeometryNodeSwitch")
choose.input_type = "GEOMETRY"
links.new(uniform, choose.inputs["Switch"])
links.new(front.outputs[0], choose.inputs["False"])
links.new(carrier, choose.inputs["True"])
return choose.outputs[0], captured
links.new(geometry, choose.inputs["True"])
return choose.outputs[0], rounding
def build_front_fields(group, rim, front_normal):
def build_front_fields(group, rim, front_normal, normal_weight=1):
"""Fit structural amplitude over neighbouring points before creating the shell."""
links = group.links
@@ -162,10 +194,8 @@ def build_front_fields(group, rim, front_normal):
smooth.data_type = "FLOAT_VECTOR"
links.new(normal, smooth.inputs["Value"])
links.new(spread, smooth.inputs["Iterations"])
normalize = group.nodes.new("ShaderNodeVectorMath")
normalize.operation = "NORMALIZE"
links.new(smooth.outputs[0], normalize.inputs[0])
return {"structure": structure, "inflation_normal": normalize.outputs[0]}
normalized = blend_normal(group, normal, smooth.outputs[0], normal_weight)
return {"structure": structure, "inflation_normal": normalized}
def build_inflation_position(group, position, distance, uniform, fields):
@@ -220,7 +250,7 @@ def build_inflation_position(group, position, distance, uniform, fields):
return result.outputs[0]
def build_front_normal(group):
def build_front_normal(group, normal_weight):
links = group.links
normal = group.nodes.new("GeometryNodeInputNormal")
control = group.nodes.new("NodeGroupInput")
@@ -228,10 +258,8 @@ def build_front_normal(group):
blur.data_type = "FLOAT_VECTOR"
links.new(normal.outputs[0], blur.inputs["Value"])
links.new(control.outputs["Normal Smooth"], blur.inputs["Iterations"])
normalize = group.nodes.new("ShaderNodeVectorMath")
normalize.operation = "NORMALIZE"
links.new(blur.outputs[0], normalize.inputs[0])
return evaluate_field(group, normalize.outputs[0], "FLOAT_VECTOR", "POINT")
normalized = blend_normal(group, normal.outputs[0], blur.outputs[0], normal_weight)
return evaluate_field(group, normalized, "FLOAT_VECTOR", "POINT")
def build_shell_position(group, position, front, uniform, fields):
@@ -316,36 +344,49 @@ def build_shell_position(group, position, front, uniform, fields):
def _build_depth_surface(group, geometry, controls):
"""Project the cleaned mesh, capture surface fields, and connect the shell."""
"""Project the mesh and sample front-surface fields while shaping the shell."""
links = group.links
split = controls.outputs["Split Threshold"]
image = controls.outputs["Depth Image"]
scale = controls.outputs["Uniform Scale"]
original = geometry
geometry, corner_camera, cut_vertex = split_depth_surface(
group, geometry, sample_face_camera(group, image), split, scale, triangles=True,
)
geometry = remove_boundary_triangles(group, geometry)
camera = build_surface_camera(group, _position_field(group, image), corner_camera, cut_vertex, split)
camera = smooth_boundary_camera(group, camera)
projection = project_depth_surface(
group, geometry, camera, scale, controls.outputs["Reference Depth"], controls.outputs["Depth Scale"],
group, geometry, camera, scale, controls.outputs["Reference Depth"], controls.outputs["Depth Scale"], split,
)
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.inputs["Balloon"].default_value = False
mode.inputs["Uniform"].default_value = True
uniform = mode.outputs[0]
volume = build_volume_fields(group)
normal = build_front_normal(group)
front = build_front_fields(group, volume["rim"], normal)
shared, fields = capture_fields(
group, projection, "POINT",
center=("FLOAT", volume["center"]), front_normal=("VECTOR", normal),
structure=("FLOAT", front["structure"]), inflation_normal=("VECTOR", front["inflation_normal"]),
)
rounded, rounding = build_volume_rounding(group, shared, {**volume, **fields}, uniform)
fields.update(rounding)
normal_weight = build_normal_weight(group, uniform)
normal = build_front_normal(group, normal_weight)
front = build_front_fields(group, volume["rim"], normal, normal_weight)
rounded, rounding = build_volume_rounding(group, projection, volume, uniform, normal_weight)
thickness = sample_field(group, projection, rounding["thickness"], "FLOAT")
shell = thicken_depth_surface(
group, rounded, fields["thickness"], back_mask=NORMAL_SCALE_ATTRIBUTE_NAME, weld_zero_thickness=True,
group, rounded, thickness, back_mask=NORMAL_SCALE_ATTRIBUTE_NAME, weld_zero_thickness=True,
)
fields = {
name: sample_field(group, projection, value, data_type, index=shell["source_index"])
for name, value, data_type in (
("center", volume["center"], "FLOAT"), ("front_normal", normal, "FLOAT_VECTOR"),
("structure", front["structure"], "FLOAT"), ("inflation_normal", front["inflation_normal"], "FLOAT_VECTOR"),
)
}
fields.update({
name: sample_field(group, projection, value, "FLOAT_VECTOR" if name == "normal" else "FLOAT",
index=shell["source_index"])
for name, value in rounding.items()
})
oriented = build_shell_position(group, shell["position"], shell["front"], uniform, fields)
links.new(oriented, shell["position_input"])
amount = group.nodes.new("GeometryNodeSwitch")
@@ -364,17 +405,20 @@ 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'] = 11
group['anyimage_asset_revision'] = 18
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, 'Depth Scale', 1.0)
float_input(group, 'Split Threshold', 0.0, maximum=1, subtype='FACTOR')
float_input(group, 'Split Threshold', 0.1, maximum=1, subtype='FACTOR')
iterations = interface_socket(group, 'Smooth', 'INPUT', 'NodeSocketInt')
iterations.default_value = 0
iterations.min_value, iterations.max_value = (0, 20)
options = group.interface.new_panel(name='Options', default_closed=True)
boundary_smooth = interface_socket(group, 'Boundary Smooth', 'INPUT', 'NodeSocketInt', options)
boundary_smooth.default_value, boundary_smooth.min_value, boundary_smooth.max_value = 5, 0, 20
boundary_smooth.description = 'Smooth boundary depth and geometry along the original outline and split edges'
float_input(group, 'Cleanup Threshold', 0.0, parent=options)
float_input(group, 'Reference Depth', 1.0, subtype='DISTANCE', parent=options)
float_input(group, 'Edge Round', 0.2, maximum=1, subtype='FACTOR', parent=options)