Add image selection refinement functionality with BEN2 integration

- Implemented viewport.py for handling 3D view projections, gestures, and overlay drawing.
- Created refine_image_selection.py to manage the refinement of gesture masks and output image bounds.
- Added selection module with refine_selection.py to process and crop images based on alpha channel visibility.
- Introduced __init__.py for selection module to expose refine_selection function.
This commit is contained in:
2026-08-31 22:10:05 +08:00
parent 14d63001d2
commit c97b4a3e41
76 changed files with 2127 additions and 1137 deletions

View File

@@ -1,12 +1,12 @@
"""Cutout Region validation and Shape-menu interaction helpers."""
"""Cutout Selection validation and Shape-menu interaction helpers."""
from ...preferences import (
MAX_CUTOUT_MESH_RESOLUTION,
MIN_CUTOUT_MESH_RESOLUTION,
)
from ...properties import ai_ready
from ...common.image_data import image_rgba
from ...common.image_region import rasterize_region_path
from ...common.image import image_rgba
from ...common.selection import rasterize_selection_path
from .geometry import CUTOUT_ALPHA_THRESHOLD
from .shape import CUTOUT_SHAPE_LABELS
@@ -17,16 +17,23 @@ CUTOUT_SHAPE_BUTTONS = (
("DEPTH_SURFACE", "SURFACE_NCURVE"),
)
CUTOUT_MESH_DETAIL_SPACING = {
"LOW": 32,
"MEDIUM": 16,
"HIGH": 8,
"ULTRA": 4,
}
def validate_cutout_region(
def validate_cutout_selection(
source_object,
region_path,
selection_path,
):
image = source_object.data
rgba = image_rgba(image)
rasterize_region_path(
rasterize_selection_path(
tuple(image.size),
region_path,
selection_path,
source_alpha=rgba[:, :, 3],
alpha_threshold=CUTOUT_ALPHA_THRESHOLD,
antialias=True,
@@ -42,26 +49,30 @@ def cutout_mesh_resolution(image_size, point_spacing):
)
def cutout_mesh_detail_spacing(mesh_detail):
return CUTOUT_MESH_DETAIL_SPACING[str(mesh_detail)]
def _pie_button(
pie,
shape,
label,
icon,
source_name,
region_path_json,
refine_region,
selection_path_json,
refine_selection,
generate_normal,
mesh_resolution,
):
operator = pie.operator(
"anyimage.cutout_region_to_shape",
"anyimage.cutout_selection_to_shape",
text=label,
icon=icon,
)
operator.source_object_name = source_name
operator.region_path_json = region_path_json
operator.selection_path_json = selection_path_json
operator.shape = shape
operator.refine_region = refine_region
operator.refine_selection = refine_selection
operator.generate_normal = generate_normal
operator.mesh_resolution = mesh_resolution
@@ -70,15 +81,15 @@ def open_shape_pie(
context,
event,
source_object,
region_path,
refine_region=False,
selection_path,
refine_selection=False,
):
source_name = source_object.name
region_path_json = region_path.to_json()
selection_path_json = selection_path.to_json()
settings = context.scene.anyimage_settings
point_spacing = settings.cutout_point_spacing
point_spacing = cutout_mesh_detail_spacing(settings.cutout_mesh_detail)
ready = ai_ready()
refine_region = ready and refine_region
refine_selection = ready and refine_selection
generate_normal = ready and bool(
getattr(settings, "cutout_generate_normal", False)
)
@@ -94,8 +105,8 @@ def open_shape_pie(
CUTOUT_SHAPE_LABELS[shape],
icon,
source_name,
region_path_json,
refine_region,
selection_path_json,
refine_selection,
generate_normal,
mesh_resolution,
)