Files
AnyImage/tests/test_addon.py
nan 6c8358f92f Refactor image processing and cutout functionalities
- Updated the naming convention for upscaled images to include underscores and file extensions.
- Renamed properties related to cutout pixel size to point spacing for clarity.
- Adjusted alpha region calculations to use a half-alpha threshold for better accuracy.
- Enhanced the handling of foreground mesh masks to consider half-alpha values.
- Introduced new functions for unbounded selections and cutout images, ensuring proper handling of source and selection images.
- Improved test coverage for cutout functionalities, including bounds mapping and alpha preservation.
- Updated tests to reflect changes in naming conventions and functionality adjustments.
2026-08-27 12:01:47 +08:00

3485 lines
132 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import importlib
import json
import sys
import tempfile
import threading
import unittest
from pathlib import Path
from types import ModuleType, SimpleNamespace
from unittest.mock import patch
PROJECT_ROOT = Path(__file__).resolve().parents[1]
SOURCE_ROOT = PROJECT_ROOT / "src"
class AddonTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.remove_modules()
def append_status_bar(draw):
draw._owner = "anyimage"
cls.status_bar_draws.append(draw)
fake_bpy = ModuleType("bpy")
fake_bpy.props = SimpleNamespace(
BoolProperty=lambda **options: options,
EnumProperty=lambda **options: options,
FloatProperty=lambda **options: options,
FloatVectorProperty=lambda **options: options,
IntProperty=lambda **options: options,
PointerProperty=lambda **options: options,
StringProperty=lambda **options: options,
)
fake_bpy.types = SimpleNamespace(
AddonPreferences=object,
Menu=object,
Operator=object,
Panel=object,
PropertyGroup=object,
WorkSpaceTool=object,
Image=type("Image", (), {}),
Scene=type("Scene", (), {}),
STATUSBAR_HT_header=SimpleNamespace(
append=append_status_bar,
remove=lambda draw: cls.status_bar_draws.remove(draw),
),
VIEW3D_MT_object_context_menu=SimpleNamespace(
append=lambda draw: cls.context_menu_draws.append(draw),
prepend=lambda draw: cls.context_menu_draws.insert(0, draw),
remove=lambda draw: cls.context_menu_draws.remove(draw),
),
OUTLINER_MT_object=SimpleNamespace(
append=lambda draw: cls.outliner_menu_draws.append(draw),
prepend=lambda draw: cls.outliner_menu_draws.insert(0, draw),
remove=lambda draw: cls.outliner_menu_draws.remove(draw),
),
)
cls.registered = []
cls.registered_tools = []
cls.status_bar_draws = []
cls.context_menu_draws = []
cls.outliner_menu_draws = []
fake_bpy.utils = SimpleNamespace(
register_class=cls.registered.append,
unregister_class=cls.registered.remove,
register_tool=lambda tool, **options: cls.registered_tools.append(
(tool, options)
),
unregister_tool=lambda tool: cls.registered_tools.remove(
next(item for item in cls.registered_tools if item[0] is tool)
),
)
fake_bpy.context = SimpleNamespace(
preferences=SimpleNamespace(addons={}),
)
fake_bpy.app = SimpleNamespace(online_access=True)
fake_bpy.path = SimpleNamespace(abspath=lambda path: path)
fake_bpy.data = SimpleNamespace(
objects=SimpleNamespace(get=lambda _name: None),
)
sys.modules["bpy"] = fake_bpy
sys.path.insert(0, str(SOURCE_ROOT))
cls.paths_before_import = tuple(sys.path)
cls.addon = importlib.import_module("anyimage")
cls.job_runtime = importlib.import_module("anyimage.runtime")
cls.model_operators = importlib.import_module("anyimage.operators.models")
cls.image_depth = importlib.import_module("anyimage.operators.material")
cls.image_data = importlib.import_module("anyimage.operators.image")
cls.depth_operator = importlib.import_module(
"anyimage.operators.depth_plane.operator"
)
cls.debug = importlib.import_module("anyimage.operators.debug")
cls.depth_build = importlib.import_module(
"anyimage.operators.depth_plane.build"
)
cls.mesh_plane = importlib.import_module("anyimage.operators.mesh_plane")
cls.mesh = importlib.import_module("anyimage.operators.mesh")
cls.image_background = importlib.import_module("anyimage.operators.background")
cls.image_upscale = importlib.import_module("anyimage.operators.upscale")
cls.image_mesh = importlib.import_module("anyimage.operators.surface")
cls.image_edit_common = importlib.import_module(
"anyimage.operators.image_edit.common"
)
cls.image_mask = importlib.import_module("anyimage.operators.image_edit.mask")
cls.image_box = importlib.import_module("anyimage.operators.image_edit.box")
cls.image_lasso = importlib.import_module("anyimage.operators.image_edit.lasso")
cls.image_line = importlib.import_module("anyimage.operators.image_edit.line")
cls.image_polyline = importlib.import_module(
"anyimage.operators.image_edit.polyline"
)
cls.image_perspective = importlib.import_module(
"anyimage.operators.image_edit.perspective"
)
cls.balloon = importlib.import_module("anyimage.operators.balloon.operator")
cls.balloon_geometry = importlib.import_module(
"anyimage.operators.balloon.geometry"
)
cls.balloon_depth = importlib.import_module("anyimage.operators.balloon.depth")
cls.balloon_poisson = importlib.import_module(
"anyimage.operators.balloon.poisson"
)
cls.balloon_teddy = importlib.import_module("anyimage.operators.balloon.teddy")
cls.paths_after_import = tuple(sys.path)
cls.fake_bpy = fake_bpy
@classmethod
def tearDownClass(cls):
sys.path.remove(str(SOURCE_ROOT))
sys.modules.pop("bpy", None)
cls.remove_modules()
@staticmethod
def remove_modules():
for module_name in tuple(sys.modules):
if module_name == "anyimage" or module_name.startswith("anyimage."):
sys.modules.pop(module_name)
def setUp(self):
preferences = SimpleNamespace(
storage_root="",
device="CUDA",
upscale_model="REALESRGAN_GENERAL_WDN_X4V3",
upscale_max_output_size=4096,
)
self.fake_bpy.context.preferences.addons = {
"anyimage": SimpleNamespace(preferences=preferences)
}
def tearDown(self):
self.addon.runtime.close_active()
self.registered.clear()
self.registered_tools.clear()
self.status_bar_draws.clear()
self.context_menu_draws.clear()
self.outliner_menu_draws.clear()
self.fake_bpy.data = SimpleNamespace(
objects=SimpleNamespace(get=lambda _name: None),
)
if hasattr(self.fake_bpy.types.Scene, "anyimage_settings"):
del self.fake_bpy.types.Scene.anyimage_settings
if hasattr(self.fake_bpy.types.Image, "anyimage_upscale_source"):
del self.fake_bpy.types.Image.anyimage_upscale_source
def test_runtime_and_operator_support_modules_have_explicit_boundaries(self):
for name in (
"client.py",
"environment.py",
"storage.py",
"models.py",
"status_bar.py",
):
self.assertFalse((SOURCE_ROOT / "anyimage" / name).exists(), name)
def test_mps_requires_m4_or_newer(self):
properties = self.addon.properties
parse = properties.parse_apple_silicon_generation
self.assertEqual(parse("Apple M2"), 2)
self.assertEqual(parse("Apple M4 Pro"), 4)
self.assertEqual(parse("Apple M10 Max"), 10)
self.assertIsNone(parse("Intel(R) Core(TM) i9"))
self.assertFalse(properties.supports_mps(3))
self.assertTrue(properties.supports_mps(4))
def test_unsupported_mac_hides_manifest_mps_device(self):
properties = self.addon.properties
manifest = {"devices": ["mps", "cpu"]}
with (
patch.object(
self.addon.runtime,
"environment_manifest",
return_value=manifest,
),
patch.object(properties, "supports_mps", return_value=False),
):
self.assertEqual(properties.supported_devices(), ["cpu"])
with (
patch.object(
self.addon.runtime,
"environment_manifest",
return_value=manifest,
),
patch.object(properties, "supports_mps", return_value=True),
):
self.assertEqual(properties.supported_devices(), ["mps", "cpu"])
def test_windows_keeps_default_directml_device(self):
properties = self.addon.properties
with (
patch.object(
self.addon.runtime,
"environment_manifest",
return_value={},
),
patch.object(properties.sys, "platform", "win32"),
):
self.assertEqual(
properties.supported_devices(),
["directml", "cpu"],
)
def test_import_does_not_add_addon_directories_to_sys_path(self):
self.assertEqual(self.paths_after_import, self.paths_before_import)
def test_job_request_is_not_part_of_blendjob(self):
blendjob = importlib.import_module("blendjob")
self.assertFalse(hasattr(blendjob, "JobRequest"))
def test_operator_modules_use_plain_properties(self):
operators = self.addon.operators
for name in (
"AnyImageJobMixin",
"ImageInputJobOperator",
"GeometryImageJobOperator",
"Da3ImageJobOperator",
"Moge2ImageJobOperator",
"BackgroundRequestMixin",
):
self.assertFalse(hasattr(operators, name), name)
for operator in operators.CLASSES:
for name in getattr(operator, "__annotations__", {}):
self.assertFalse(name.startswith("anyimage_"), (operator, name))
def test_concrete_job_operator_is_the_blender_operator(self):
operator = self.addon.operators.GenerateMesh
self.assertEqual(operator.bl_idname, "anyimage.generate_mesh")
self.assertIn(self.job_runtime.JobOperatorBase, operator.__mro__)
self.assertIn(self.fake_bpy.types.Operator, operator.__mro__)
def test_upscale_debug_operator_accepts_each_catalog_model(self):
with tempfile.TemporaryDirectory() as directory:
image_path = Path(directory) / "input.png"
image_path.touch()
settings = SimpleNamespace(
input_path=str(image_path),
device="CPU",
video_frames=3,
)
context = SimpleNamespace(scene=SimpleNamespace(anyimage_settings=settings))
for model_key in self.addon.properties.shared_model_catalog.UPSCALE_MODELS:
operator = self.debug.DebugUpscale()
operator.model = model_key
with (
self.subTest(model=model_key),
patch.object(self.debug, "require_environment"),
patch.object(self.debug, "require_model") as require_model,
):
request = operator.request(context)
require_model.assert_called_once_with(model_key)
self.assertEqual(request["model"], model_key)
self.assertEqual(request["input"], str(image_path))
self.assertIn(self.debug.DebugUpscale, self.addon.CLASSES)
panel = importlib.import_module("anyimage.panel")
self.assertIn(panel.UpscaleDebugPanel, self.addon.CLASSES)
def test_default_request_sends_every_operator_property(self):
class Example(self.job_runtime.JobOperatorBase):
value: object()
enabled: object()
operator = Example()
operator.value = 42
operator.enabled = True
self.assertEqual(
operator.request(object()),
{"value": 42, "enabled": True},
)
def test_overridden_request_purely_replaces_default_properties(self):
class Example(self.job_runtime.JobOperatorBase):
value: object()
def request(self, context):
return {"replacement": context.value}
operator = Example()
operator.value = 42
self.assertEqual(
operator.request(SimpleNamespace(value=7)),
{"replacement": 7},
)
def test_runtime_submits_job_type_and_plain_dict(self):
calls = []
submitted = threading.Event()
class Controller:
def submit(self, job_type, parameters):
calls.append((job_type, parameters))
submitted.set()
return {"job_id": "job", "directory": "."}
def cancel(self, _job_id):
pass
def mark_job_complete(self, _job_id):
pass
class Example(self.job_runtime.JobOperatorBase):
job_type = "example"
value: object()
def controller(self, _runtime):
return Controller()
timers = []
window_manager = SimpleNamespace(
progress_begin=lambda *_args: None,
progress_update=lambda _value: None,
event_timer_add=lambda *_args, **_kwargs: (
timers.append(object()) or timers[-1]
),
event_timer_remove=lambda _timer: None,
modal_handler_add=lambda _operator: None,
)
context = SimpleNamespace(
window_manager=window_manager,
window=object(),
)
operator = Example()
operator.value = 9
self.assertEqual(operator.execute(context), {"RUNNING_MODAL"})
self.assertTrue(submitted.wait(1.0))
self.assertEqual(calls, [("example", {"value": 9})])
def test_runtime_owns_environment_and_storage_paths(self):
runtime = self.addon.runtime
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
with patch.object(runtime, "storage_root_factory", root):
self.assertEqual(runtime.storage_root(), root.resolve())
self.assertEqual(runtime.environment_directory(), root / ".venv")
self.assertEqual(
runtime.install_status_path(), root / "install-status.json"
)
self.assertEqual(runtime.install_log_path(), root / "install.log")
self.assertEqual(runtime.server_log_path(), root / "server.log")
def test_runtime_checks_its_environment_dictionary_hash(self):
runtime = self.addon.runtime
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
python = (
root
/ ".venv"
/ ("Scripts/python.exe" if sys.platform == "win32" else "bin/python")
)
python.parent.mkdir(parents=True)
python.touch()
(root / "manifest.json").write_text(
json.dumps({"environment_hash": runtime.environment_hash}),
encoding="utf-8",
)
with patch.object(runtime, "storage_root_factory", root):
self.assertTrue(runtime.environment_ready())
def test_runtime_builds_the_standard_installer_command(self):
runtime = self.addon.runtime
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
with patch.object(runtime, "storage_root_factory", root):
command = runtime.install_command()
self.assertEqual(command[0], sys.executable)
self.assertEqual(Path(command[1]).name, "environment.py")
self.assertEqual(Path(command[1]).parent.name, "installer")
self.assertIn("--storage-root", command)
self.assertIn("--config", command)
self.assertNotIn("--venv-dir", command)
self.assertNotIn("--models-dir", command)
self.assertNotIn("--stages", command)
def test_runtime_exposes_declared_environment_and_post_install(self):
runtime = self.addon.runtime
self.assertEqual(runtime.environment["python"], "3.12")
self.assertIn("numpy==2.4.2", runtime.environment["packages"])
self.assertFalse(
any(
"blendjob" in package.lower()
for package in runtime.environment["packages"]
)
)
self.assertTrue(callable(runtime.post_install))
def test_anyimage_post_install_is_ordinary_code_using_runtime_request(self):
calls = []
fake_runtime = SimpleNamespace(
request=lambda job_type, parameters: calls.append((job_type, parameters))
)
self.job_runtime.post_install(fake_runtime)
self.assertEqual(
calls,
[
("download-model", {"model": "MOGE2_VITS_NORMAL"}),
("download-model", {"model": "BEN2_BASE"}),
(
"download-model",
{"model": "REALESRGAN_GENERAL_WDN_X4V3"},
),
],
)
def test_anyimage_post_install_names_each_failed_model(self):
calls = []
def request(_job_type, parameters):
calls.append(parameters["model"])
raise RuntimeError("network unavailable")
with self.assertRaisesRegex(
RuntimeError,
(
"MoGe-2 ViT-S Normal: network unavailable; "
"BEN2 Base: network unavailable; "
"Real-ESRGAN General WDN x4v3: network unavailable"
),
):
self.job_runtime.post_install(SimpleNamespace(request=request))
self.assertEqual(
calls,
[
"MOGE2_VITS_NORMAL",
"BEN2_BASE",
"REALESRGAN_GENERAL_WDN_X4V3",
],
)
def test_environment_controller_runs_arbitrary_post_install_callable(self):
runtime_module = importlib.import_module("blendjob.runtime")
runtime = self.addon.runtime
calls = []
started = threading.Event()
release = threading.Event()
def post_install(received):
calls.append(received)
started.set()
release.wait(1.0)
controller = runtime_module.EnvironmentController(runtime)
controller.process = SimpleNamespace(poll=lambda: 0, returncode=0)
with (
tempfile.TemporaryDirectory() as directory,
patch.object(
runtime,
"post_install",
post_install,
),
patch.object(
runtime,
"install_status_path",
return_value=Path(directory) / "missing.json",
),
):
self.assertEqual(controller.status("environment")["state"], "running")
self.assertTrue(started.wait(1.0))
release.set()
controller.post_thread.join(1.0)
status = controller.status("environment")
self.assertEqual(calls, [runtime])
self.assertEqual(status["state"], "succeeded")
def test_environment_controller_reports_post_install_failure(self):
runtime_module = importlib.import_module("blendjob.runtime")
runtime = self.addon.runtime
def post_install(_runtime):
raise RuntimeError("BEN2 download failed")
controller = runtime_module.EnvironmentController(runtime)
controller.process = SimpleNamespace(poll=lambda: 0, returncode=0)
with (
tempfile.TemporaryDirectory() as directory,
patch.object(
runtime,
"post_install",
post_install,
),
patch.object(
runtime,
"install_status_path",
return_value=Path(directory) / "missing.json",
),
):
controller.status("environment")
controller.post_thread.join(1.0)
status = controller.status("environment")
self.assertEqual(status["state"], "failed")
self.assertEqual(status["error"], "BEN2 download failed")
def test_post_install_progress_resets_without_stage_fraction(self):
runtime_module = importlib.import_module("blendjob.runtime")
controller = runtime_module.EnvironmentController(self.addon.runtime)
controller._update_post_progress(
{"job_id": "first", "job_type": "download-model", "progress": 1.0}
)
self.assertEqual(controller.post_status["job_id"], "first")
controller._update_post_progress(
{"job_id": "second", "job_type": "download-model", "progress": 0.1}
)
self.assertEqual(controller.post_status["job_id"], "second")
self.assertEqual(controller.post_status["progress"], 0.1)
self.assertNotIn("stage", controller.post_status)
self.assertNotIn("stages", controller.post_status)
def test_conversion_model_lists_default_to_moge2_vits(self):
properties = importlib.import_module("anyimage.properties")
catalog = (
{
"key": "SMALL",
"family": "da3",
"enabled": True,
"label": "DA3 Small",
"description": "DA3",
"license": "Apache",
},
{
"key": "MOGE2_VITS_NORMAL",
"family": "moge2",
"enabled": True,
"label": "MoGe-2 ViT-S",
"description": "MoGe",
"license": "MIT",
},
)
with patch.object(properties, "model_catalog", return_value=catalog):
depth_items = properties.depth_plane_model_enum_items(None, None)
surface_items = properties.surface_model_enum_items(None, None)
balloon_items = properties.balloon_depth_model_enum_items(None, None)
self.assertEqual(depth_items[0][0], "MOGE2_VITS_NORMAL")
self.assertEqual(surface_items[0][0], "MOGE2_VITS_NORMAL")
self.assertEqual([item[0] for item in balloon_items], ["MOGE2_VITS_NORMAL"])
def test_required_model_operator_requests_both_models(self):
operator = self.addon.operators.DownloadRequiredModels()
with patch.object(self.model_operators, "require_environment"):
parameters = operator.request(None)
self.assertEqual(
parameters,
{
"models": [
"MOGE2_VITS_NORMAL",
"BEN2_BASE",
"REALESRGAN_GENERAL_WDN_X4V3",
]
},
)
def test_ai_status_checks_environment_and_required_model_files(self):
properties = self.addon.properties
with (
patch.object(self.addon.runtime, "environment_ready", return_value=True),
patch.object(
properties,
"model_catalog",
return_value=(
{"key": "MOGE2_VITS_NORMAL", "ready": True},
{"key": "BEN2_BASE", "ready": False},
{
"key": "REALESRGAN_GENERAL_WDN_X4V3",
"ready": True,
},
),
),
):
status = properties.ai_status()
self.assertTrue(status["environment_ready"])
self.assertEqual(status["missing_models"], ("BEN2_BASE",))
self.assertFalse(status["ready"])
self.assertEqual(properties.ai_setup_label(status), "Download Required Models…")
with (
patch.object(self.addon.runtime, "environment_ready", return_value=False),
patch.object(
properties,
"model_catalog",
return_value=tuple(
{"key": key, "ready": True}
for key in properties.REQUIRED_MODEL_KEYS
),
),
):
status = properties.ai_status()
self.assertFalse(status["ready"])
self.assertEqual(
properties.ai_setup_label(status), "Set Up AI Environment…"
)
def test_setup_ai_environment_selects_the_needed_install_step(self):
calls = []
ops = SimpleNamespace(
anyimage=SimpleNamespace(
install_environment=lambda *_args: (
calls.append("official") or {"FINISHED"}
),
install_environment_mirror=lambda *_args: (
calls.append("mirror") or {"FINISHED"}
),
download_required_models=lambda *_args: (
calls.append("models") or {"FINISHED"}
),
)
)
operator = self.model_operators.SetupAIEnvironment()
with patch.object(self.fake_bpy, "ops", ops, create=True):
operator.use_mirror = True
with patch.object(
self.model_operators,
"ai_status",
return_value={
"environment_ready": False,
"missing_models": ("MOGE2_VITS_NORMAL", "BEN2_BASE"),
"ready": False,
},
):
self.assertEqual(operator.execute(None), {"FINISHED"})
with patch.object(
self.model_operators,
"ai_status",
return_value={
"environment_ready": True,
"missing_models": ("BEN2_BASE",),
"ready": False,
},
):
self.assertEqual(operator.execute(None), {"FINISHED"})
self.assertEqual(calls, ["mirror", "models"])
def test_ai_environment_ui_types_are_registered(self):
self.assertIn(self.model_operators.SetupAIEnvironment, self.addon.CLASSES)
self.assertIn(
self.model_operators.OpenAIEnvironmentSettings,
self.addon.CLASSES,
)
panel = importlib.import_module("anyimage.panel")
self.assertFalse(hasattr(panel, "ModelsPanel"))
def test_preferences_draws_upscale_model_download(self):
preference_module = importlib.import_module("anyimage.preferences")
downloads = []
properties = []
class Layout:
enabled = True
def prop(self, _owner, name, **_options):
properties.append(name)
def separator(self, **_options):
return None
def label(self, **_options):
return None
def row(self, **_options):
return self
def operator(self, identifier, **_options):
operator = SimpleNamespace(model="")
if identifier == "anyimage.download_model":
downloads.append(operator)
return operator
preferences = preference_module.AnyImagePreferences()
preferences.layout = Layout()
catalog = tuple(
{
"key": key,
"label": key,
"family": "upscale",
"enabled": True,
"ready": False,
}
for key in (
"REALESRGAN_GENERAL_WDN_X4V3",
"SAFMN_REAL_X4",
"REALESRGAN_X4PLUS",
)
)
with (
patch.object(
self.addon.properties,
"ai_status",
return_value={
"environment_ready": True,
"missing_models": (),
"ready": True,
},
),
patch.object(
self.addon.properties,
"model_catalog",
return_value=catalog,
),
patch.object(self.addon.runtime, "server_busy", return_value=False),
):
preferences.draw(None)
self.assertEqual(
[download.model for download in downloads],
[model["key"] for model in catalog],
)
self.assertIn("upscale_model", properties)
self.assertIn("upscale_max_output_size", properties)
self.assertEqual(
preference_module.AnyImagePreferences.__annotations__["upscale_model"][
"name"
],
"Default Upscale Model",
)
def test_semantic_mesh_operators_are_registered_without_dialog_options(self):
operator = self.mesh.ConvertToPlane
self.assertIn(operator, self.addon.CLASSES)
self.assertIn(self.mesh.ConvertToDepthPlane, self.addon.CLASSES)
self.assertIn(self.mesh.ConvertCutoutShape, self.addon.CLASSES)
self.assertEqual(operator.bl_idname, "anyimage.convert_to_plane")
self.assertNotIn("shadeless", operator.__annotations__)
self.assertNotIn("keep_original", operator.__annotations__)
self.assertNotIn("invoke", operator.__dict__)
self.assertNotIn("draw", operator.__dict__)
self.assertIn("REGISTER", operator.bl_options)
self.assertIn("UNDO", operator.bl_options)
image_empty = SimpleNamespace(
type="EMPTY",
empty_display_type="IMAGE",
data=object(),
)
mesh_plane = SimpleNamespace(
type="MESH",
modifiers=[
SimpleNamespace(node_group=SimpleNamespace(name="O Mesh Plane"))
],
)
plain_mesh = SimpleNamespace(type="MESH", modifiers=[])
self.assertTrue(operator.poll(SimpleNamespace(object=image_empty)))
self.assertFalse(operator.poll(SimpleNamespace(object=mesh_plane)))
self.assertFalse(operator.poll(SimpleNamespace(object=plain_mesh)))
def test_plane_depth_off_accepts_animated_image_without_server(self):
operator = SimpleNamespace(
depth_mode="OFF",
remove_background=False,
shadeless=False,
profile="SURFACE",
mesh_detail=64,
thickness=0.1,
inflation=0.1,
algorithm="POISSON",
keep_original=False,
)
source = SimpleNamespace(data=SimpleNamespace(name="Sequence"))
context = SimpleNamespace(object=source, scene=object())
with (
patch.object(self.mesh, "video_total_frames", return_value=24),
patch.object(
self.mesh,
"image_user_settings",
return_value=(1, 0, 24, True),
),
patch.object(self.mesh, "create_material", return_value="Material"),
patch.object(self.mesh, "_copy_color_image", return_value="Color"),
patch.object(self.mesh, "_build_mesh_object") as build_mesh,
patch.object(
self.mesh,
"invoke_ai_setup_if_needed",
side_effect=AssertionError("local conversion must not start AI setup"),
),
):
result = self.mesh.ConvertToPlane.execute(operator, context)
self.assertEqual(result, {"FINISHED"})
build_mesh.assert_called_once()
def test_cutout_and_balloon_without_refine_build_locally(self):
source = SimpleNamespace(select_set=lambda _selected: None)
context = SimpleNamespace(
view_layer=SimpleNamespace(objects=SimpleNamespace(active=None))
)
for shape in (self.mesh.CUTOUT, self.mesh.BALLOON):
with self.subTest(shape=shape):
operator = SimpleNamespace(
source_object_name="Source",
selection_data="[[0,0],[10,0],[0,10]]",
shape=shape,
mesh_detail=64,
refine_selection=False,
normal_analysis=False,
invert=True,
report=lambda *_args: None,
)
with (
patch.object(
self.mesh.bpy.data.objects,
"get",
return_value=source,
),
patch.object(self.mesh, "is_image_empty", return_value=True),
patch.object(self.mesh, "_build_local_cutout") as build_local,
patch.object(
self.mesh,
"_start_job",
side_effect=AssertionError("local Cutout must not start a Job"),
),
):
result = self.mesh.ConvertCutoutShape.execute(operator, context)
self.assertEqual(result, {"FINISHED"})
build_local.assert_called_once_with(
operator,
context,
source,
shape,
operator.selection_data,
True,
)
def test_refined_cutout_and_balloon_submit_blendjob(self):
source = SimpleNamespace(select_set=lambda _selected: None)
context = SimpleNamespace(
view_layer=SimpleNamespace(objects=SimpleNamespace(active=None))
)
for shape in (self.mesh.CUTOUT, self.mesh.BALLOON):
with self.subTest(shape=shape):
operator = SimpleNamespace(
source_object_name="Source",
selection_data="[[0,0],[10,0],[0,10]]",
shape=shape,
mesh_detail=64,
refine_selection=True,
normal_analysis=False,
invert=False,
report=lambda *_args: None,
)
with (
patch.object(
self.mesh.bpy.data.objects,
"get",
return_value=source,
),
patch.object(self.mesh, "is_image_empty", return_value=True),
patch.object(
self.mesh,
"_build_local_cutout",
side_effect=AssertionError("Refine must run through the Job"),
),
patch.object(
self.mesh, "_start_job", return_value={"FINISHED"}
) as start_job,
):
result = self.mesh.ConvertCutoutShape.execute(operator, context)
self.assertEqual(result, {"FINISHED"})
start_job.assert_called_once_with(
operator,
context,
shape,
operator.selection_data,
True,
False,
False,
)
def test_plain_balloon_job_requests_moge_normal(self):
source = SimpleNamespace(
name="Source",
data=SimpleNamespace(size=(2048, 1024)),
)
context = SimpleNamespace(object=source, scene=SimpleNamespace())
operator = SimpleNamespace(mesh_detail=64, report=lambda *_args: None)
with (
patch.object(self.mesh, "video_total_frames", return_value=1),
patch.object(
self.mesh, "invoke_ai_setup_if_needed", return_value=None
) as setup,
patch.object(
self.mesh,
"prepare_image_input",
return_value=(Path("input.png"), False),
),
patch.object(
self.mesh,
"_depth_settings",
return_value=("MOGE2_VITL", 100.0, 5),
),
patch.object(
self.mesh.bpy.ops.anyimage,
"generate_mesh",
return_value={"FINISHED"},
create=True,
) as generate_mesh,
):
result = self.mesh._start_job(
operator, context, self.mesh.BALLOON, normal_analysis=True
)
self.assertEqual(result, {"FINISHED"})
setup.assert_called_once_with()
self.assertEqual(generate_mesh.call_args.kwargs["shape"], self.mesh.BALLOON)
self.assertEqual(generate_mesh.call_args.kwargs["mesh_detail"], 64)
self.assertEqual(
generate_mesh.call_args.kwargs["analysis_input_path"],
"input.png",
)
self.assertEqual(
generate_mesh.call_args.kwargs["normal_model"],
self.addon.properties.DEFAULT_MOGE2_MODEL_KEY,
)
self.assertEqual(generate_mesh.call_args.kwargs["normal_resolution_level"], 5)
def test_all_cutout_shapes_request_moge_normal_when_enabled(self):
context = SimpleNamespace(
scene=SimpleNamespace(anyimage_settings=SimpleNamespace(video_frames=1))
)
for shape in self.mesh.CUTOUT_SHAPES:
with self.subTest(shape=shape):
operator = self.mesh.GenerateMesh()
operator.input_path = __file__
operator.source_object_name = ""
operator.shape = shape
operator.selection_data = "[[0,0],[10,0],[0,10]]"
operator.model = self.addon.properties.DEFAULT_MOGE2_MODEL_KEY
operator.mesh_detail = 64
operator.process_res = 1
operator.resolution_level = 5
operator.normal_model = self.addon.properties.DEFAULT_MOGE2_MODEL_KEY
operator.normal_resolution_level = 5
operator.normal_analysis = True
operator.refine_selection = False
operator.invert = False
with (
patch.object(self.mesh, "require_environment"),
patch.object(self.mesh, "require_model") as require_model,
patch.object(self.mesh, "production_device", return_value="CPU"),
patch.object(self.mesh, "source_start_frame", return_value=1),
):
parameters = operator.request(context)
require_model.assert_called_once_with(
self.addon.properties.DEFAULT_MOGE2_MODEL_KEY
)
self.assertEqual(
parameters["normal_model"],
self.addon.properties.DEFAULT_MOGE2_MODEL_KEY,
)
self.assertEqual(parameters["normal_resolution_level"], 5)
self.assertEqual(parameters["mesh_detail"], 64)
def test_cutout_job_skips_moge_normal_model_when_disabled(self):
operator = self.mesh.GenerateMesh()
operator.input_path = __file__
operator.source_object_name = ""
operator.shape = self.mesh.CUTOUT
operator.selection_data = "[[0,0],[10,0],[0,10]]"
operator.mesh_detail = 64
operator.normal_analysis = False
operator.refine_selection = False
operator.invert = False
with (
patch.object(self.mesh, "require_environment"),
patch.object(self.mesh, "require_model") as require_model,
patch.object(self.mesh, "production_device", return_value="CPU"),
patch.object(self.mesh, "source_start_frame", return_value=1),
):
parameters = operator.request(SimpleNamespace(scene=SimpleNamespace()))
require_model.assert_not_called()
self.assertFalse(parameters["normal_analysis"])
self.assertNotIn("normal_model", parameters)
self.assertNotIn("normal_resolution_level", parameters)
def test_mesh_plane_redo_keeps_the_original_world_matrix(self):
operator = self.mesh_plane.ConvertToMeshPlane()
operator.shadeless = False
operator.keep_original = True
operator.source_object_name = ""
original_matrix = (
(1.0, 0.0, 0.0, 3.0),
(0.0, 1.0, 0.0, -2.0),
(0.0, 0.0, 1.0, 7.0),
(0.0, 0.0, 0.0, 1.0),
)
source = SimpleNamespace(
name="Source Image",
type="EMPTY",
empty_display_type="IMAGE",
data=object(),
matrix_world=original_matrix,
)
context = SimpleNamespace(object=source)
matrices = []
def capture_build(*_args, **kwargs):
matrices.append(kwargs["matrix_world"])
with (
patch.object(
self.mesh_plane.bpy.data.objects,
"get",
return_value=source,
),
patch.object(
self.mesh_plane,
"restore_matrix",
side_effect=lambda values: tuple(values),
),
patch.object(
self.mesh_plane,
"build_mesh_plane",
side_effect=capture_build,
),
):
self.assertEqual(operator.invoke(context, None), {"FINISHED"})
source.matrix_world = (
(1.0, 0.0, 0.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, 0.0, 0.0, 1.0),
)
self.assertEqual(operator.execute(context), {"FINISHED"})
expected = self.mesh_plane.flatten_matrix(original_matrix)
self.assertEqual(matrices, [expected, expected])
def test_mesh_plane_uses_a_single_quad_and_geometry_nodes(self):
import inspect
mesh_source = inspect.getsource(self.mesh_plane.create_mesh_plane_mesh)
processing_source = inspect.getsource(self.mesh_plane.add_mesh_plane_processing)
self.assertIn("((0, 1, 2, 3),)", mesh_source)
self.assertIn('("Subdivide", max(0, int(subdivisions)))', processing_source)
self.assertIn('("Thickness", max(0.0, float(thickness)))', processing_source)
self.assertIn('("Material", material)', processing_source)
def test_image_menu_keeps_ai_features_clickable_and_offers_models(self):
calls = []
labels = {}
class Layout:
operator_context = ""
enabled = True
def operator(self, identifier, **options):
calls.append((identifier, self.enabled))
labels[identifier] = options.get("text")
return SimpleNamespace()
def separator(self):
calls.append(("separator", self.enabled))
menu = self.addon.AnyImageImageMenu()
menu.layout = Layout()
with patch(
"anyimage.properties.ai_status",
return_value={
"environment_ready": True,
"missing_models": ("BEN2_BASE",),
"ready": False,
},
):
menu.draw(
SimpleNamespace(
object=SimpleNamespace(
type="EMPTY",
empty_display_type="IMAGE",
data=SimpleNamespace(size=(640, 480)),
)
)
)
self.assertEqual(
calls,
[
("anyimage.convert_to_plane", True),
("anyimage.convert_to_depth_plane", True),
("separator", True),
("anyimage.remove_image_background", True),
("anyimage.upscale_image", True),
("separator", True),
("anyimage.setup_ai_environment", True),
],
)
self.assertEqual(
labels["anyimage.setup_ai_environment"],
"Download Required Models…",
)
self.assertEqual(
labels["anyimage.upscale_image"],
"Upscale (640 × 480)",
)
def test_image_menu_opens_environment_settings_when_ai_is_ready(self):
calls = []
class Layout:
operator_context = ""
enabled = True
def operator(self, identifier, **_options):
calls.append((identifier, self.enabled))
return SimpleNamespace()
def separator(self):
calls.append(("separator", self.enabled))
menu = self.addon.AnyImageImageMenu()
menu.layout = Layout()
with patch(
"anyimage.properties.ai_status",
return_value={
"environment_ready": True,
"missing_models": (),
"ready": True,
},
):
menu.draw(None)
self.assertIn(("anyimage.open_ai_environment_settings", True), calls)
self.assertNotIn(("anyimage.setup_ai_environment", True), calls)
def test_upscale_operator_defaults_to_wdn_and_sends_selected_model(self):
operator_type = self.image_upscale.UpscaleImage
self.assertIn(operator_type, self.addon.CLASSES)
self.assertEqual(
operator_type.__annotations__["model"]["default"],
"REALESRGAN_GENERAL_WDN_X4V3",
)
self.assertEqual(
operator_type.__annotations__["max_output_size"]["default"],
4096,
)
self.assertIn(self.job_runtime.JobOperatorBase, operator_type.__mro__)
self.assertEqual(operator_type.bl_options, {"UNDO"})
operator = operator_type()
operator.input_path = "input.png"
operator.source_object_name = "Source"
operator.model = "SAFMN_REAL_X4"
operator.max_output_size = 6144
context = SimpleNamespace(
scene=SimpleNamespace(anyimage_settings=SimpleNamespace(video_frames=7))
)
with (
patch.object(self.image_upscale, "require_environment"),
patch.object(self.image_upscale, "require_model") as require_model,
patch.object(
self.image_upscale, "input_path", return_value=Path("input.png")
),
patch.object(self.image_upscale, "production_device", return_value="CUDA"),
patch.object(self.image_upscale, "source_start_frame", return_value=4),
):
request = operator.request(context)
require_model.assert_called_once_with("SAFMN_REAL_X4")
self.assertEqual(
request,
{
"input": "input.png",
"model": "SAFMN_REAL_X4",
"device": "cuda",
"video_frames": 7,
"start_frame": 4,
"max_output_size": 6144,
},
)
def test_upscale_execute_reads_model_and_size_from_preferences(self):
source_image = SimpleNamespace(
name="Original",
source="FILE",
anyimage_upscale_source=None,
)
source_object = SimpleNamespace(
name="Source",
type="EMPTY",
empty_display_type="IMAGE",
data=source_image,
)
context = SimpleNamespace(
object=source_object,
scene=SimpleNamespace(anyimage_settings=SimpleNamespace(video_frames=1)),
)
operator = self.image_upscale.UpscaleImage()
with (
patch.object(
self.image_upscale,
"configured_upscale_model",
return_value="SAFMN_REAL_X4",
),
patch.object(
self.image_upscale,
"configured_upscale_max_output_size",
return_value=6144,
),
patch.object(
self.image_upscale,
"prepare_image_input",
return_value=(Path("input.png"), False),
),
patch.object(
self.job_runtime.JobOperatorBase,
"execute",
return_value={"RUNNING_MODAL"},
),
):
result = operator.execute(context)
self.assertEqual(result, {"RUNNING_MODAL"})
self.assertEqual(operator.model, "SAFMN_REAL_X4")
self.assertEqual(operator.max_output_size, 6144)
def test_upscale_operator_opens_setup_when_required_ai_is_missing(self):
expected = {"CANCELLED"}
with patch.object(
self.image_upscale,
"invoke_ai_setup_if_needed",
return_value=expected,
) as setup:
result = self.image_upscale.UpscaleImage().invoke(None, None)
setup.assert_called_once_with()
self.assertIs(result, expected)
def test_upscale_operator_runs_immediately_when_ai_is_ready(self):
operator = self.image_upscale.UpscaleImage()
context = object()
expected = {"FINISHED"}
with (
patch.object(
self.image_upscale,
"invoke_ai_setup_if_needed",
return_value=None,
),
patch.object(
operator,
"execute",
return_value=expected,
) as execute,
):
result = operator.invoke(context, None)
execute.assert_called_once_with(context)
self.assertIs(result, expected)
self.assertNotIn("REGISTER", operator.bl_options)
def test_upscale_response_replaces_source_image(self):
original_image = SimpleNamespace(name="Original")
source_object = SimpleNamespace(
type="EMPTY",
empty_display_type="IMAGE",
data=original_image,
)
self.fake_bpy.data = SimpleNamespace(
objects=SimpleNamespace(
get=lambda name: source_object if name == "Source" else None
),
images=SimpleNamespace(
get=lambda name: original_image if name == "Original" else None
),
)
operator = self.image_upscale.UpscaleImage()
operator.source_object_name = "Source"
operator.source_image_name = "Original"
operator.model = "REALESRGAN_GENERAL_WDN_X4V3"
result = SimpleNamespace(directory=Path("result"))
result_image = SimpleNamespace(name="", anyimage_upscale_source=None)
with patch.object(
self.image_upscale,
"create_color_image",
return_value=result_image,
) as create_color:
message = operator.response(None, result)
create_color.assert_called_once_with(
source_object,
result.directory,
frame_prefix="upscale.",
)
self.assertEqual(result_image.name, "Original_upscale.png")
self.assertIs(
result_image.anyimage_upscale_source,
original_image,
)
self.assertEqual(
message,
"Image is upscaled with Real-ESRGAN General WDN x4v3",
)
def test_upscale_response_reports_removed_original_image(self):
source_object = SimpleNamespace(
type="EMPTY",
empty_display_type="IMAGE",
data=object(),
)
self.fake_bpy.data = SimpleNamespace(
objects=SimpleNamespace(get=lambda _name: source_object),
images=SimpleNamespace(get=lambda _name: None),
)
operator = self.image_upscale.UpscaleImage()
operator.source_object_name = "Source"
operator.source_image_name = "Removed"
with self.assertRaisesRegex(
RuntimeError,
"original source image is no longer available",
):
operator.response(None, SimpleNamespace(directory=Path("result")))
def test_depth_analysis_follows_the_oldest_upscale_source(self):
original = SimpleNamespace(name="Original")
first_upscale = SimpleNamespace(
name="First",
anyimage_upscale_source=original,
)
second_upscale = SimpleNamespace(
name="Second",
anyimage_upscale_source=first_upscale,
)
self.assertIs(
self.image_data.analysis_source_image(second_upscale),
original,
)
def test_repeated_upscale_uses_original_image_and_reports_warning(self):
original_image = SimpleNamespace(name="Original", source="FILE")
current_image = SimpleNamespace(
source="FILE",
anyimage_upscale_source=original_image,
)
source_object = SimpleNamespace(
name="Source",
type="EMPTY",
empty_display_type="IMAGE",
data=current_image,
)
context = SimpleNamespace(
object=source_object,
scene=SimpleNamespace(anyimage_settings=SimpleNamespace(video_frames=1)),
)
reports = []
operator = self.image_upscale.UpscaleImage()
operator.report = lambda levels, message: reports.append((levels, message))
with (
patch.object(
self.image_upscale,
"prepare_image_input",
return_value=(Path("original.png"), False),
) as prepare,
patch.object(
self.job_runtime.JobOperatorBase,
"execute",
return_value={"RUNNING_MODAL"},
),
):
result = operator.execute(context)
self.assertEqual(result, {"RUNNING_MODAL"})
prepare.assert_called_once_with(original_image, context.scene)
self.assertEqual(operator.source_image_name, "Original")
self.assertIn("rebuilding from the original", reports[0][1])
def test_packed_image_export_does_not_use_scene_render_transform(self):
with tempfile.TemporaryDirectory() as directory:
output_directory = Path(directory)
calls = []
class Image:
file_format = "JPEG"
def save(self, **options):
calls.append(options)
Path(options["filepath"]).write_bytes(b"raw image")
image = Image()
scene = SimpleNamespace(
view_settings=SimpleNamespace(look="AgX - Medium High Contrast")
)
with patch.object(
self.image_data.tempfile,
"mkdtemp",
return_value=str(output_directory),
):
output = self.image_data.save_packed_image(image, scene)
self.assertEqual(output, output_directory / "input.png")
self.assertEqual(
calls,
[{"filepath": str(output), "save_copy": True}],
)
self.assertEqual(image.file_format, "JPEG")
def test_color_result_inherits_source_color_space(self):
with tempfile.TemporaryDirectory() as directory:
output_directory = Path(directory)
(output_directory / "image.0001.png").touch()
source_image = SimpleNamespace(
name="source.png",
source="FILE",
use_fake_user=False,
colorspace_settings=SimpleNamespace(name="ACEScg"),
)
class ResultImage:
def __init__(self):
self.alpha_mode = ""
self.colorspace_settings = SimpleNamespace(name="sRGB")
self.name = ""
def pack(self):
pass
result_image = ResultImage()
source_object = SimpleNamespace(
name="Source",
type="EMPTY",
empty_display_type="IMAGE",
data=source_image,
)
self.fake_bpy.data = SimpleNamespace(
images=SimpleNamespace(
load=lambda *_args, **_options: result_image,
remove=lambda *_args, **_options: None,
)
)
created = self.image_data.create_color_image(
source_object,
output_directory,
)
self.assertIs(created, result_image)
self.assertEqual(result_image.colorspace_settings.name, "ACEScg")
self.assertEqual(result_image.name, "source_color.png")
self.assertIs(source_object.data, result_image)
def test_background_result_replaces_only_an_unshared_image(self):
for remaining_users, removed_source, result_name in (
(0, True, "Source.png"),
(1, False, "Source_color.png"),
):
with self.subTest(remaining_users=remaining_users):
source_image = SimpleNamespace(
name="Source.png",
use_fake_user=False,
users=remaining_users,
)
source_object = SimpleNamespace(
name="Source",
type="EMPTY",
empty_display_type="IMAGE",
data=source_image,
)
result_image = SimpleNamespace(name="Source_color.png")
removed = []
self.fake_bpy.data = SimpleNamespace(
objects={"Source": source_object},
images=SimpleNamespace(
remove=lambda image: removed.append(image),
),
)
def create_result(target, _directory):
source_image.use_fake_user = True
target.data = result_image
return result_image
with (
patch.object(
self.image_data,
"create_color_image",
side_effect=create_result,
),
patch.object(self.image_background, "push_undo") as push_undo,
):
self.image_background.apply_background_response(
SimpleNamespace(directory=Path("result")),
"Source",
)
self.assertIs(source_object.data, result_image)
self.assertFalse(source_image.use_fake_user)
self.assertEqual(removed, [source_image] if removed_source else [])
self.assertEqual(result_image.name, result_name)
push_undo.assert_called_once_with("Remove Background")
def test_balloon_contour_is_smooth_and_evenly_resampled(self):
import numpy as np
contour = self.balloon_geometry.smooth_lasso_contour(
((0, 0), (3, 0), (4, 0), (4, 2), (0, 2)),
0.25,
)
closed = np.vstack((contour, contour[0]))
lengths = np.linalg.norm(np.diff(closed, axis=0), axis=1)
self.assertGreater(len(contour), 20)
self.assertGreater(self.balloon_geometry.polygon_area(contour), 0.0)
self.assertLess(float(lengths.std() / lengths.mean()), 0.15)
def test_balloon_contour_smooths_pixel_stairs_before_coarse_resampling(self):
import numpy as np
staircase = [(0.0, 0.0)]
for value in range(16):
staircase.append((value + 1.0, float(value)))
staircase.append((value + 1.0, value + 1.0))
staircase.append((0.0, 16.0))
smooth = self.balloon_geometry.smooth_lasso_contour(staircase, 2.0)
def diagonal_noise(points):
points = np.asarray(points)
selected = (
(points[:, 0] > 2.0)
& (points[:, 0] < 14.0)
& (np.abs(points[:, 1] - points[:, 0]) < 2.0)
)
return float(np.std(points[selected, 1] - points[selected, 0]))
self.assertLess(diagonal_noise(smooth), 0.05)
def test_cutout_polygon_mask_keeps_nonzero_winding_overlap(self):
polygon = (
(1, 1),
(7, 1),
(7, 7),
(1, 7),
(1, 1),
(4, 1),
(10, 1),
(10, 7),
(4, 7),
(4, 1),
(1, 1),
)
even_odd = self.image_mask.polygon_mask((12, 9), polygon)
nonzero = self.image_mask.polygon_mask((12, 9), polygon, "NONZERO")
self.assertFalse(even_odd[3, 5])
self.assertTrue(nonzero[3, 5])
def preview_area(fill_rule):
triangles = self.image_edit_common.scanline_fill_triangles(
polygon, fill_rule
)
return sum(
abs(
self.image_edit_common._triangle_cross(
triangles[index],
triangles[index + 1],
triangles[index + 2],
)
)
* 0.5
for index in range(0, len(triangles), 3)
)
self.assertAlmostEqual(preview_area('EVEN_ODD'), 36.0)
self.assertAlmostEqual(preview_area('NONZERO'), 54.0)
def test_image_mask_preserves_source_rgb_outside_the_selection(self):
import numpy as np
top_down = np.full((3, 3, 4), (0.2, 0.4, 0.6, 1.0), dtype=np.float32)
source = np.flipud(top_down).ravel()
selection = self.image_mask.polygon_mask((3, 3), ((0, 0), (3, 0), (0, 3)))
pixels, bounds = self.image_mask.edit_image_mask_pixels(
source,
(3, 3),
selection,
)
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
result = np.flipud(np.asarray(pixels).reshape((height, width, 4)))
self.assertEqual(bounds, (0, 0, 2, 2))
np.testing.assert_allclose(result[1, 1], (0.2, 0.4, 0.6, 0.0))
self.assertEqual(float(result[0, 0, 3]), 1.0)
def test_image_mask_preserves_hidden_rgb_from_source_transparency(self):
import numpy as np
top_down = np.full((2, 2, 4), (0.2, 0.4, 0.6, 1.0), dtype=np.float32)
top_down[0, 0, 3] = 0.0
pixels, _bounds = self.image_mask.edit_image_mask_pixels(
np.flipud(top_down).ravel(),
(2, 2),
np.ones((2, 2), dtype=np.float32),
)
result = np.flipud(np.asarray(pixels).reshape((2, 2, 4)))
np.testing.assert_allclose(result[0, 0], (0.2, 0.4, 0.6, 0.0))
def test_image_mask_invert_runs_after_refinement(self):
import numpy as np
top_down = np.full((2, 2, 4), (0.2, 0.4, 0.6, 1.0), dtype=np.float32)
refined = np.asarray(((1.0, 0.25), (0.0, 1.0)), dtype=np.float32)
pixels, bounds = self.image_mask.edit_image_mask_pixels(
np.flipud(top_down).ravel(),
(2, 2),
refined,
invert=True,
)
result = np.flipud(np.asarray(pixels).reshape((2, 2, 4)))
self.assertEqual(bounds, (0, 0, 2, 2))
self.assertEqual(float(result[0, 0, 3]), 0.0)
self.assertAlmostEqual(float(result[0, 1, 3]), 0.75)
np.testing.assert_allclose(result[0, 0], (0.2, 0.4, 0.6, 0.0))
def test_image_mask_rejects_a_fully_transparent_result(self):
import numpy as np
with self.assertRaisesRegex(ValueError, "no visible"):
self.image_mask.edit_image_mask_pixels(
np.ones(2 * 2 * 4, dtype=np.float32),
(2, 2),
np.zeros((2, 2), dtype=np.float32),
)
def test_expected_empty_image_edits_are_reported_as_warnings(self):
reports = []
operator = SimpleNamespace(
report=lambda level, message: reports.append((level, message))
)
self.image_edit_common.report_image_edit_exception(
operator,
self.image_edit_common.ImageEditWarning(
"The edit contains no visible image pixels"
),
)
self.image_edit_common.report_image_edit_exception(
operator,
ValueError("Unexpected invalid data"),
)
self.assertEqual(
reports,
[
(
{"WARNING"},
"The edit contains no visible image pixels",
),
({"ERROR"}, "Unexpected invalid data"),
],
)
def test_image_mask_crops_to_visible_alpha_without_padding(self):
import numpy as np
rgba = np.full((4, 5, 4), (0.8, 0.7, 0.6, 0.0), dtype=np.float32)
rgba[1, 2] = (0.2, 0.4, 0.6, 1.0)
rgba[2, 3] = (0.2, 0.4, 0.6, 1.0)
pixels, bounds = self.image_edit_common.crop_pixels_to_alpha(
np.flipud(rgba).ravel(),
(10, 20, 15, 24),
)
result = np.flipud(np.asarray(pixels).reshape((2, 2, 4)))
self.assertEqual(bounds, (12, 21, 14, 23))
self.assertEqual(result.shape, (2, 2, 4))
np.testing.assert_allclose(result[0, 1], (0.8, 0.7, 0.6, 0.0))
def test_image_mask_restores_refined_alpha_to_source_coordinates(self):
import numpy as np
top_down = np.zeros((2, 2, 4), dtype=np.float32)
top_down[:, :, 3] = ((1.0, 0.5), (0.0, 1.0))
mask = self.image_mask.refined_mask_from_pixels(
np.flipud(top_down).ravel(),
(2, 2),
(1, 1, 3, 3),
(4, 4),
)
expected = np.zeros((4, 4), dtype=np.float32)
expected[1:3, 1:3] = top_down[:, :, 3]
np.testing.assert_allclose(mask, expected)
def test_image_edit_replaces_an_unshared_image_without_leaving_a_copy(self):
class Pixels:
values = None
def foreach_set(self, values):
self.values = tuple(values)
source_image = SimpleNamespace(
name="Source.png",
size=(4, 2),
colorspace_settings=SimpleNamespace(name="sRGB"),
use_fake_user=False,
users=0,
)
source_object = SimpleNamespace(
type="EMPTY",
empty_display_type="IMAGE",
data=source_image,
empty_display_size=1.0,
empty_image_offset=(0.0, 0.0),
)
result_image = SimpleNamespace(
name="",
alpha_mode="",
colorspace_settings=SimpleNamespace(name=""),
pixels=Pixels(),
pack=lambda: packed.append(True),
update=lambda: updated.append(True),
)
packed = []
updated = []
removed = []
self.fake_bpy.data = SimpleNamespace(
objects=[source_object],
images=SimpleNamespace(
new=lambda *_args, **_options: result_image,
remove=lambda image, **_options: removed.append(image),
),
)
pixels = tuple(float(index) for index in range(16))
with patch.object(
self.image_edit_common,
"empty_display_alignment",
return_value=(2.0, (0.25, -0.25)),
):
result = self.image_edit_common.replace_empty_image_pixels(
source_object, pixels, (2, 2), (0, 0, 2, 2)
)
self.assertIs(result, result_image)
self.assertIs(source_object.data, result_image)
self.assertEqual(removed, [source_image])
self.assertEqual(result_image.name, "Source.png")
self.assertEqual(result_image.pixels.values, pixels)
self.assertEqual(packed, [True])
self.assertEqual(updated, [True])
self.assertEqual(source_object.empty_display_size, 2.0)
self.assertEqual(source_object.empty_image_offset, (0.25, -0.25))
def test_image_edit_forks_an_image_shared_by_multiple_empties(self):
class Pixels:
values = None
def foreach_set(self, values):
self.values = tuple(values)
class ResultImage:
colorspace_settings = SimpleNamespace(name="")
pixels = Pixels()
alpha_mode = ""
def pack(self):
pass
def update(self):
pass
source_image = SimpleNamespace(
name="Source.png",
size=(4, 2),
colorspace_settings=SimpleNamespace(name="ACEScg"),
use_fake_user=False,
users=1,
)
source_object = SimpleNamespace(
type="EMPTY",
empty_display_type="IMAGE",
data=source_image,
empty_display_size=1.0,
empty_image_offset=(0.0, 0.0),
)
sibling = SimpleNamespace(
type="EMPTY",
empty_display_type="IMAGE",
data=source_image,
)
result_image = ResultImage()
created = []
def new_image(*args, **options):
created.append((args, options))
return result_image
self.fake_bpy.data = SimpleNamespace(
objects=[source_object, sibling],
images=SimpleNamespace(
new=new_image,
remove=lambda *_args, **_options: None,
),
)
pixels = tuple(float(index) for index in range(16))
with patch.object(
self.image_edit_common,
"empty_display_alignment",
return_value=(2.0, (0.25, -0.25)),
):
result = self.image_edit_common.replace_empty_image_pixels(
source_object, pixels, (2, 2), (0, 0, 2, 2)
)
self.assertIs(result, result_image)
self.assertIs(source_object.data, result_image)
self.assertIs(sibling.data, source_image)
self.assertEqual(len(created), 1)
self.assertEqual(result_image.colorspace_settings.name, "ACEScg")
self.assertEqual(result_image.pixels.values, pixels)
def test_image_edit_tools_and_operators_are_registered(self):
import inspect
operator = self.image_mask.EditImageMask
self.assertIn(operator, self.addon.CLASSES)
self.assertIn(self.image_mask.RefineImageMask, self.addon.CLASSES)
self.assertIn(self.image_mask.ActivateImageEditTool, self.addon.CLASSES)
tool_cases = (
(self.image_box.ImageBoxTool, "BOX", "ops.sculpt.border_mask"),
(self.image_lasso.ImageLassoTool, "LASSO", "ops.sculpt.lasso_mask"),
(self.image_line.ImageLineTool, "LINE", "ops.sculpt.line_mask"),
(
self.image_polyline.ImagePolylineTool,
"POLYLINE",
"ops.sculpt.polyline_mask",
),
)
for tool, gesture, icon in tool_cases:
self.assertNotIn(tool, self.addon.CLASSES)
self.assertEqual(tool.bl_operator, operator.bl_idname)
self.assertEqual(tool.bl_icon, icon)
self.assertEqual(tool.bl_keymap[0][2]["properties"], [("gesture", gesture)])
self.assertNotIn("bl_cursor", tool.__dict__)
self.assertIn("draw_settings", self.image_box.ImageBoxTool.__dict__)
self.assertIn("draw_settings", self.image_lasso.ImageLassoTool.__dict__)
self.assertNotIn("draw_settings", self.image_line.ImageLineTool.__dict__)
self.assertNotIn(
"draw_settings", self.image_polyline.ImagePolylineTool.__dict__
)
self.assertEqual(operator.__annotations__["gesture"]["default"], "BOX")
self.assertIn("invert", operator.__annotations__)
self.assertNotIn("continuous", operator.__annotations__)
self.assertNotIn("padding", operator.__annotations__)
settings = self.addon.properties.AnyImageSettings.__annotations__
self.assertFalse(settings["image_edit_refine_selection"]["default"])
self.assertEqual(settings["cutout_point_spacing"]["default"], 32)
self.assertEqual(settings["cutout_point_spacing"]["min"], 4)
self.assertEqual(settings["cutout_point_spacing"]["max"], 32)
self.assertFalse(settings["cutout_normal_analysis"]["default"])
self.assertNotIn("image_extract_continuous", settings)
self.assertNotIn("image_extract_padding", settings)
self.assertNotIn("image_extract_remove_background", settings)
source_picker = inspect.getsource(
self.image_edit_common.resolve_image_edit_click
)
self.assertIn("bpy.ops.view3d.select(", source_picker)
self.assertIn("Click an Image Empty", source_picker)
self.assertIn("_restore_selection(", source_picker)
modal_source = inspect.getsource(operator.modal)
self.assertIn('event.type == "F"', modal_source)
self.assertIn("self.invert = not self.invert", modal_source)
self.assertIn("math.dist(point, self._path[-1]) >= 2.0", modal_source)
mask_source = inspect.getsource(self.image_mask.polygon_mask)
self.assertNotIn("np.indices", mask_source)
self.assertIn("range(top, bottom, 128)", mask_source)
def test_image_line_and_polyline_disable_refine_selection(self):
settings = SimpleNamespace(image_edit_refine_selection=True)
context = SimpleNamespace(scene=SimpleNamespace(anyimage_settings=settings))
with patch.object(self.image_edit_common, "ai_ready", return_value=True):
for gesture in ("LINE", "POLYLINE"):
operator = SimpleNamespace(
gesture=gesture,
refine_selection=True,
)
self.image_edit_common.apply_image_mask_settings(operator, context)
self.assertFalse(operator.refine_selection)
operator = SimpleNamespace(gesture="BOX", refine_selection=False)
self.image_edit_common.apply_image_mask_settings(operator, context)
self.assertTrue(operator.refine_selection)
def test_image_line_click_is_cancelled_before_projection(self):
import inspect
modal_source = inspect.getsource(self.image_mask.EditImageMask.modal)
projection_source = inspect.getsource(
self.image_mask.image_line_half_plane_polygon
)
self.assertIn("math.dist(self._path[0], self._cursor) < 2.0", modal_source)
self.assertIn("if len(line) != 2", projection_source)
def test_image_area_click_is_cancelled_without_a_warning(self):
import inspect
completion_source = inspect.getsource(self.image_mask.EditImageMask._complete)
self.assertIn("if len(path) < 3", completion_source)
self.assertIn('return {"CANCELLED"}', completion_source)
self.assertNotIn("Draw a larger image region", completion_source)
def test_image_polyline_closes_from_start_or_enter(self):
import inspect
modal_source = inspect.getsource(self.image_mask.EditImageMask._modal_polyline)
draw_source = inspect.getsource(self.image_mask.EditImageMask._draw_selection)
self.assertIn("return self._confirm_polyline(context)", modal_source)
self.assertNotIn("DOUBLE_CLICK", modal_source)
self.assertIn('event.type in {"RET", "NUMPAD_ENTER"}', modal_source)
self.assertIn("_draw_polyline_close_indicator()", draw_source)
def test_image_edit_first_click_selects_another_image_without_editing(self):
selected = []
def image(name):
return SimpleNamespace(
name=name,
type="EMPTY",
empty_display_type="IMAGE",
data=object(),
select_set=lambda value: selected.append((name, value)),
)
active = image("Active")
clicked = image("Clicked")
context = SimpleNamespace(
active_object=active,
selected_objects=(active,),
view_layer=SimpleNamespace(objects=SimpleNamespace(active=active)),
)
event = SimpleNamespace(mouse_region_x=10, mouse_region_y=20)
previous_ops = getattr(self.fake_bpy, "ops", None)
previous_objects = self.fake_bpy.data.objects
def select(**_kwargs):
context.active_object = clicked
self.fake_bpy.ops = SimpleNamespace(
view3d=SimpleNamespace(select=select),
object=SimpleNamespace(select_all=lambda **_kwargs: None),
)
self.fake_bpy.data.objects = {
active.name: active,
clicked.name: clicked,
}
try:
source, should_edit = self.image_edit_common.resolve_image_edit_click(
context, event, lambda *_args: None
)
finally:
self.fake_bpy.data.objects = previous_objects
if previous_ops is None:
del self.fake_bpy.ops
else:
self.fake_bpy.ops = previous_ops
self.assertIs(source, clicked)
self.assertFalse(should_edit)
self.assertEqual(selected, [])
def test_image_edit_first_click_on_non_image_keeps_active_image(self):
selection_calls = []
active = SimpleNamespace(
name="Active",
type="EMPTY",
empty_display_type="IMAGE",
data=object(),
select_set=lambda value: selection_calls.append(value),
)
clicked = SimpleNamespace(name="Mesh", type="MESH", data=object())
view_objects = SimpleNamespace(active=active)
context = SimpleNamespace(
active_object=active,
selected_objects=(active,),
view_layer=SimpleNamespace(objects=view_objects),
)
event = SimpleNamespace(mouse_region_x=10, mouse_region_y=20)
previous_ops = getattr(self.fake_bpy, "ops", None)
previous_objects = self.fake_bpy.data.objects
def select(**_kwargs):
context.active_object = clicked
self.fake_bpy.ops = SimpleNamespace(
view3d=SimpleNamespace(select=select),
object=SimpleNamespace(
select_all=lambda **_kwargs: selection_calls.append(False)
),
)
self.fake_bpy.data.objects = {active.name: active, clicked.name: clicked}
try:
source, should_edit = self.image_edit_common.resolve_image_edit_click(
context, event, lambda *_args: None
)
finally:
self.fake_bpy.data.objects = previous_objects
if previous_ops is None:
del self.fake_bpy.ops
else:
self.fake_bpy.ops = previous_ops
self.assertIs(source, active)
self.assertTrue(should_edit)
self.assertIs(view_objects.active, active)
self.assertEqual(selection_calls, [False, True])
def test_image_perspective_rectifies_identity_pixels(self):
import numpy as np
top_down = np.arange(3 * 4 * 4, dtype=np.float32).reshape((3, 4, 4))
top_down /= float(top_down.max())
top_down[:, :, 3] = 1.0
result = self.image_perspective.warp_perspective_pixels(
np.flipud(top_down).ravel(),
(4, 3),
((0, 0), (4, 0), (4, 3), (0, 3)),
(4, 3),
)
np.testing.assert_allclose(
np.flipud(np.asarray(result).reshape((3, 4, 4))),
top_down,
atol=1e-6,
)
def test_image_perspective_tightly_crops_alpha(self):
import numpy as np
top_down = np.zeros((4, 4, 4), dtype=np.float32)
top_down[1:3, 1:3] = (0.2, 0.4, 0.6, 1.0)
pixels, output_size, placement_bounds = (
self.image_perspective.extract_perspective_pixels(
np.flipud(top_down).ravel(),
(4, 4),
((0, 0), (4, 0), (4, 4), (0, 4)),
1.0,
)
)
result = np.flipud(np.asarray(pixels).reshape((2, 2, 4)))
self.assertEqual(output_size, (2, 2))
self.assertEqual(placement_bounds, (1.0, 1.0, 3.0, 3.0))
self.assertTrue(np.all(result[:, :, 3] == 1.0))
def test_image_perspective_rejects_non_convex_corners(self):
with self.assertRaisesRegex(ValueError, "convex"):
self.image_perspective.validate_perspective_quad(
((0, 0), (10, 0), (4, 4), (0, 10))
)
def test_image_perspective_corner_order_is_click_order_independent(self):
import itertools
import numpy as np
corners = ((10, 20), (80, 15), (90, 70), (5, 75))
ordered = self.image_perspective.canonical_perspective_quad(corners)
for permutation in itertools.permutations(corners):
np.testing.assert_allclose(
self.image_perspective.canonical_perspective_quad(permutation),
ordered,
)
def test_image_perspective_detects_source_image_overlap(self):
overlaps = self.image_perspective.perspective_quad_overlaps_image
self.assertFalse(overlaps(((20, 20), (30, 20), (30, 30), (20, 30)), (10, 10)))
self.assertTrue(overlaps(((5, 5), (15, 5), (15, 15), (5, 15)), (10, 10)))
self.assertTrue(overlaps(((-5, -5), (15, -5), (15, 15), (-5, 15)), (10, 10)))
def test_image_mask_fill_triangulates_a_concave_polygon_without_overlap(self):
polygon = (
(0, 0),
(4, 0),
(4, 4),
(3, 4),
(3, 1),
(1, 1),
(1, 4),
(0, 4),
)
triangles = self.image_edit_common.triangulated_polygon_vertices(polygon)
area = sum(
abs(
self.image_edit_common._triangle_cross(
triangles[index],
triangles[index + 1],
triangles[index + 2],
)
)
* 0.5
for index in range(0, len(triangles), 3)
)
self.assertEqual(len(triangles), 18)
self.assertAlmostEqual(area, 10.0)
def test_image_mask_fill_uses_blender_geometry_without_stopping_the_draw(self):
mathutils = ModuleType('mathutils')
mathutils.Vector = lambda values: values
mathutils.geometry = SimpleNamespace(
tessellate_polygon=lambda _loops: (
((0.0, 0.0, 0.0), (2.0, 0.0, 0.0), (0.0, 2.0, 0.0)),
)
)
with patch.dict(sys.modules, {'mathutils': mathutils}):
triangles = self.image_edit_common.triangulated_polygon_vertices(
((0, 0), (2, 0), (0, 2))
)
self.assertEqual(
triangles,
((0.0, 0.0), (2.0, 0.0), (0.0, 2.0)),
)
def test_image_mask_fill_falls_back_when_blender_tessellation_rejects_path(self):
mathutils = ModuleType('mathutils')
mathutils.Vector = lambda values: values
mathutils.geometry = SimpleNamespace(
tessellate_polygon=lambda _loops: (_ for _ in ()).throw(TypeError())
)
with patch.dict(sys.modules, {'mathutils': mathutils}):
triangles = self.image_edit_common.triangulated_polygon_vertices(
((0, 0), (2, 0), (0, 2))
)
self.assertEqual(len(triangles), 3)
def test_image_mask_preview_fills_a_self_intersecting_lasso(self):
polygon = ((0, 0), (4, 4), (0, 4), (4, 0))
triangles = self.image_edit_common.preview_fill_triangles(
polygon, 'NONZERO'
)
area = sum(
abs(
self.image_edit_common._triangle_cross(
triangles[index],
triangles[index + 1],
triangles[index + 2],
)
)
* 0.5
for index in range(0, len(triangles), 3)
)
self.assertTrue(triangles)
self.assertAlmostEqual(area, 8.0)
def test_image_mask_preview_detects_only_actual_self_intersections(self):
self.assertTrue(
self.image_edit_common.polygon_self_intersects(
((0, 0), (4, 4), (0, 4), (4, 0))
)
)
self.assertFalse(
self.image_edit_common.polygon_self_intersects(
((0, 0), (4, 0), (2, 2), (4, 4), (0, 4))
)
)
def test_image_perspective_limits_output_pixel_count(self):
width, height = self.image_perspective.perspective_output_size(
((0, 0), (10000, 0), (10000, 10000), (0, 10000)),
1.0,
)
self.assertLessEqual(width * height, self.image_perspective.MAX_OUTPUT_PIXELS)
def test_image_perspective_mouse_adjusts_preview_aspect_exponentially(self):
adjust = self.image_perspective.aspect_ratio_from_mouse
self.assertAlmostEqual(adjust(1.5, 240.0), 3.0)
self.assertAlmostEqual(adjust(1.5, -240.0), 0.75)
self.assertEqual(
adjust(1.0, 100000.0),
self.image_perspective.MAX_INTERACTIVE_ASPECT,
)
self.assertEqual(
self.image_perspective.snapped_aspect_ratio(1.7),
(16.0 / 9.0, "16 : 9"),
)
def test_image_perspective_preview_is_centered_at_requested_aspect(self):
left, bottom, width, height = self.image_perspective.preview_draw_bounds(
(1000, 800), 2.0
)
self.assertAlmostEqual(width / height, 2.0)
self.assertAlmostEqual(left + width * 0.5, 500.0)
self.assertAlmostEqual(bottom + height * 0.5, 400.0)
def test_image_perspective_uses_quad_edges_for_initial_aspect(self):
ratio = self.image_perspective.perspective_quad_aspect(
((0, 0), (8, 0), (8, 4), (0, 4))
)
self.assertAlmostEqual(ratio, 2.0)
def test_image_perspective_ignores_an_already_removed_preview(self):
operator = self.image_perspective.EditImagePerspective()
operator._preview_image = object()
def remove(*_args, **_options):
raise ReferenceError("StructRNA of type Image has been removed")
self.fake_bpy.data = SimpleNamespace(images=SimpleNamespace(remove=remove))
operator._remove_preview()
self.assertIsNone(operator._preview_image)
def test_image_perspective_maps_crop_to_source_plane_bounds(self):
bounds = self.image_perspective.perspective_placement_bounds(
((10, 20), (30, 20), (30, 40), (10, 40)),
(100, 50),
(-10, -5, 110, 55),
)
self.assertEqual(bounds, (8.0, 18.0, 32.0, 42.0))
def test_image_perspective_tool_uses_four_clicks_and_deform_behavior(self):
import inspect
operator = self.image_perspective.EditImagePerspective
tool = self.image_perspective.ImagePerspectiveTool
settings = self.addon.properties.AnyImageSettings.__annotations__
self.assertIn(operator, self.addon.CLASSES)
self.assertNotIn(tool, self.addon.CLASSES)
self.assertEqual(tool.bl_operator, operator.bl_idname)
self.assertEqual(tool.bl_icon, "ops.sculpt.line_project")
self.assertNotIn("bl_cursor", tool.__dict__)
self.assertNotIn("draw_settings", tool.__dict__)
self.assertNotIn("image_perspective_aspect_mode", settings)
self.assertNotIn("image_perspective_aspect_ratio", settings)
self.assertNotIn("aspect_mode", operator.__annotations__)
self.assertIn("aspect_ratio", operator.__annotations__)
self.assertNotIn("refine_selection", operator.__annotations__)
self.assertNotIn("continuous", operator.__annotations__)
self.assertNotIn("padding", operator.__annotations__)
modal_source = inspect.getsource(operator.modal)
invoke_source = inspect.getsource(operator.invoke)
execute_source = inspect.getsource(operator.execute)
draw_source = inspect.getsource(operator._draw_overlay)
preview_source = inspect.getsource(operator._draw_preview)
finish_source = inspect.getsource(operator._finish)
self.assertIn("if len(self._points) < 4", modal_source)
self.assertIn("return self.execute(context)", modal_source)
self.assertIn('self._phase = "ASPECT"', modal_source)
self.assertIn("create_perspective_preview_image(", modal_source)
self.assertIn("self._update_aspect(context, event)", modal_source)
self.assertIn("context.window.cursor_warp(", modal_source)
self.assertIn("perspective_quad_aspect(polygon)", modal_source)
self.assertIn("active_view3d_tool_id(context)", modal_source)
self.assertIn("drawing_in_region(", modal_source)
self.assertNotIn("context.region_data.is_perspective", invoke_source)
self.assertIn("resolve_image_edit_click(", invoke_source)
self.assertIn("extract_perspective_pixels(", execute_source)
self.assertIn("create_perspective_result(", execute_source)
self.assertIn("draw_polygon_fill(vertices)", draw_source)
self.assertIn("self._draw_preview()", draw_source)
self.assertIn("draw_texture_2d(", preview_source)
self.assertIn("draw_preview_frame(", preview_source)
self.assertIn("draw_aspect_hud(", preview_source)
self.assertIn("event_timer_remove", finish_source)
self.assertIn("self._remove_preview()", finish_source)
self.assertNotIn('"RET"', modal_source)
def test_image_box_builds_a_rectangular_screen_path(self):
self.assertEqual(
self.image_mask.box_screen_path((2, 3), (8, 9)),
((2, 3), (8, 3), (8, 9), (2, 9)),
)
self.assertEqual(
self.image_mask.box_screen_path((2, 3), (3, 9)),
(),
)
def test_image_edit_selection_uses_white_dashed_segments(self):
segments = self.image_edit_common.dashed_line_segments(
((0, 0), (10, 0)), dash_length=3.0, gap_length=2.0
)
self.assertEqual(
segments,
((0.0, 0.0), (3.0, 0.0), (5.0, 0.0), (8.0, 0.0)),
)
def test_image_lasso_preview_uses_fill_and_a_closed_dashed_outline(self):
polygon = ((1.0, 2.0), (8.0, 3.0), (4.0, 9.0))
operator = SimpleNamespace(
gesture='LASSO',
invert=False,
_path=[polygon[0]],
_area_pointer=1,
_region_pointer=2,
preview_fill_rule='NONZERO',
_fill_polygon=None,
_fill_triangles=(),
_outline_polygon=None,
_outline_segments=(),
_screen_polygon=lambda: polygon,
)
with (
patch.object(self.image_mask, 'drawing_in_region', return_value=True),
patch.object(
self.image_mask,
'dashed_line_segments',
return_value=('segments',),
) as build_outline,
patch.object(
self.image_mask,
'preview_fill_triangles',
return_value=('triangles',),
) as build_fill,
patch.object(self.image_mask, 'draw_polygon_fill') as draw_fill,
patch.object(self.image_mask, 'draw_dashed_line') as draw_outline,
):
self.image_mask.ImageMaskGesture._draw_selection(operator)
self.image_mask.ImageMaskGesture._draw_selection(operator)
build_fill.assert_called_once_with(polygon, 'NONZERO')
build_outline.assert_called_once_with((*polygon, polygon[0]))
self.assertEqual(draw_fill.call_count, 2)
draw_fill.assert_called_with(
polygon,
('triangles',),
(1.0, 1.0, 1.0, 0.16),
)
self.assertEqual(draw_outline.call_count, 2)
draw_outline.assert_called_with((), segments=('segments',))
def test_image_lasso_does_not_add_stationary_or_nearly_collinear_points(self):
path = [(0.0, 0.0)]
self.assertFalse(self.image_mask.append_lasso_point(path, (0.5, 0.5)))
self.assertEqual(path, [(0.0, 0.0)])
self.assertTrue(self.image_mask.append_lasso_point(path, (3.0, 0.0)))
self.assertTrue(self.image_mask.append_lasso_point(path, (6.0, 0.2)))
self.assertEqual(len(path), 2)
self.assertEqual(path[-1], (6.0, 0.2))
self.assertTrue(self.image_mask.append_lasso_point(path, (6.0, 4.0)))
self.assertEqual(len(path), 3)
def test_image_edit_activation_defaults_to_box(self):
import inspect
source = inspect.getsource(self.image_mask.ActivateImageEditTool.execute)
self.assertIn('name="anyimage.image_box"', source)
def test_image_line_snaps_and_keeps_the_previewed_half_plane(self):
import math
endpoint = self.image_mask.snapped_line_endpoint((0, 0), (10, 1), True)
self.assertAlmostEqual(math.degrees(math.atan2(endpoint[1], endpoint[0])), 5.0)
preview = self.image_mask.line_half_plane_polygon((0, 5), (10, 5), (10, 10))
image = self.image_mask.image_line_half_plane_polygon(
((0, 5), (10, 5)), (10, 10)
)
self.assertEqual(self.image_edit_common.polygon_bounds(preview), (0, 5, 10, 10))
self.assertEqual(self.image_edit_common.polygon_bounds(image), (0, 0, 10, 5))
def test_balloon_interior_points_have_uniform_spacing(self):
import numpy as np
contour = np.asarray(((0, 0), (4, 0), (4, 4), (0, 4)), dtype=float)
points = self.balloon_geometry.uniform_interior_points(contour, 0.5)
distances = np.linalg.norm(points[:, None] - points[None, :], axis=2)
np.fill_diagonal(distances, np.inf)
nearest = distances.min(axis=1)
self.assertGreater(len(points), 20)
self.assertAlmostEqual(float(np.median(nearest)), 0.5, delta=0.02)
self.assertGreater(float(nearest.min()), 0.49)
def test_balloon_default_sampling_stays_coarse(self):
import numpy as np
polygon = np.asarray(
((100, 100), (900, 100), (950, 500), (800, 900), (200, 900), (50, 500)),
dtype=float,
)
contour, interior, _spacing = self.balloon_geometry.prepare_planar_points(
polygon,
(1000, 1000),
(0.0, 1.0, 0.0, 1.0),
)
self.assertLess(len(contour), 150)
self.assertLess(len(interior), 400)
def test_balloon_sampling_uses_world_distance(self):
import numpy as np
polygon = np.asarray(
((0, 0), (1000, 0), (1000, 1000), (0, 1000)),
dtype=float,
)
unit_contour, unit_interior, unit_spacing = (
self.balloon_geometry.prepare_planar_points(
polygon,
(1000, 1000),
(0.0, 1.0, 0.0, 1.0),
)
)
scaled_contour, scaled_interior, scaled_spacing = (
self.balloon_geometry.prepare_planar_points(
polygon,
(1000, 1000),
(0.0, 2.0, 0.0, 2.0),
world_metric=((0.25, 0.0), (0.0, 0.25)),
)
)
self.assertEqual(unit_spacing, 0.1)
self.assertEqual(scaled_spacing, 0.1)
self.assertEqual(len(unit_contour), len(scaled_contour))
self.assertEqual(len(unit_interior), len(scaled_interior))
def test_poisson_balloon_is_zero_on_boundary_and_positive_inside(self):
import numpy as np
points = np.asarray(
((0, 0), (2, 0), (2, 2), (0, 2), (1, 1)),
dtype=float,
)
faces = ((0, 1, 4), (1, 2, 4), (2, 3, 4), (3, 0, 4))
boundary = np.asarray((True, True, True, True, False))
heights = self.balloon_poisson.poisson_balloon_heights(
points, faces, boundary, 0.45
)
self.assertTrue((heights[:4] == 0.0).all())
self.assertGreater(heights[4], 0.0)
def test_poisson_balloon_scales_in_world_units(self):
import numpy as np
points = np.asarray(
((0, 0), (2, 0), (2, 2), (0, 2), (1, 1)),
dtype=float,
)
faces = ((0, 1, 4), (1, 2, 4), (2, 3, 4), (3, 0, 4))
boundary = np.asarray((True, True, True, True, False))
original = self.balloon_poisson.poisson_balloon_heights(
points, faces, boundary, 0.45
)
scaled = self.balloon_poisson.poisson_balloon_heights(
points * 2.0, faces, boundary, 0.45
)
self.assertAlmostEqual(scaled[4], original[4] * 2.0, places=6)
def test_poisson_balloon_makes_wide_regions_thicker(self):
import numpy as np
faces = ((0, 1, 4), (1, 2, 4), (2, 3, 4), (3, 0, 4))
boundary = np.asarray((True, True, True, True, False))
wide = np.asarray(
((0, 0), (4, 0), (4, 2), (0, 2), (2, 1)),
dtype=float,
)
narrow = np.asarray(
((0, 0), (4, 0), (4, 0.6), (0, 0.6), (2, 0.3)),
dtype=float,
)
wide_height = self.balloon_poisson.poisson_balloon_heights(
wide, faces, boundary, 0.45
)[4]
narrow_height = self.balloon_poisson.poisson_balloon_heights(
narrow, faces, boundary, 0.45
)[4]
self.assertGreater(wide_height, narrow_height)
def test_balloon_keeps_overlapping_regions_disconnected(self):
import numpy as np
regions = (
(
((20, 0), (90, 0), (90, 70), (20, 70)),
((50, 20), (50, 50), (70, 50), (70, 20)),
),
(
((0, 20), (40, 20), (40, 45), (0, 45)),
),
)
vertices, faces, uv = self.balloon_geometry.triangulate_balloon(
regions,
(100, 100),
(0.0, 1.0, 0.0, 1.0),
spacing=0.1,
world_metric=((1.0, 0.0), (0.0, 1.0)),
)
centers = uv[np.asarray(faces)].mean(axis=1)
in_hole = (
(centers[:, 0] > 0.55)
& (centers[:, 0] < 0.65)
& (centers[:, 1] > 0.57)
& (centers[:, 1] < 0.73)
)
in_left_region = (
(centers[:, 0] > 0.02)
& (centers[:, 0] < 0.12)
& (centers[:, 1] > 0.55)
& (centers[:, 1] < 0.8)
)
heights = np.asarray(vertices)[:, 2]
self.assertFalse(in_hole.any())
self.assertTrue(in_left_region.any())
self.assertGreater(float(heights.max()), 0.0)
self.assertLess(float(heights.min()), 0.0)
adjacency = {}
for face in faces:
for vertex in face:
adjacency.setdefault(int(vertex), set())
for start, end in zip(face, np.roll(face, -1)):
adjacency[int(start)].add(int(end))
adjacency[int(end)].add(int(start))
remaining = set(adjacency)
component_count = 0
while remaining:
component_count += 1
pending = [remaining.pop()]
while pending:
vertex = pending.pop()
connected = adjacency[vertex] & remaining
remaining.difference_update(connected)
pending.extend(connected)
self.assertEqual(component_count, 2)
def test_poisson_balloon_supports_resolution_002(self):
import numpy as np
spacing = 0.02
size = 51
axis = np.arange(size, dtype=float) * spacing
points = np.asarray(
[(x, y) for y in axis for x in axis],
dtype=float,
)
faces = []
for row in range(size - 1):
for column in range(size - 1):
bottom_left = row * size + column
bottom_right = bottom_left + 1
top_left = bottom_left + size
top_right = top_left + 1
faces.append((bottom_left, bottom_right, top_right))
faces.append((bottom_left, top_right, top_left))
boundary = np.zeros(len(points), dtype=bool)
boundary[:size] = True
boundary[-size:] = True
boundary[::size] = True
boundary[size - 1 :: size] = True
heights = self.balloon_poisson.poisson_balloon_heights(
points,
faces,
boundary,
0.45,
)
self.assertTrue(np.isfinite(heights).all())
self.assertTrue((heights[boundary] == 0.0).all())
self.assertGreater(heights[(size * size) // 2], 0.0)
def test_poisson_solver_has_no_absolute_scale_cutoff(self):
import numpy as np
points = np.asarray(
((0, 0), (2, 0), (2, 2), (0, 2), (1, 1)),
dtype=float,
)
faces = ((0, 1, 4), (1, 2, 4), (2, 3, 4), (3, 0, 4))
boundary = np.asarray((True, True, True, True, False))
original = self.balloon_poisson.poisson_balloon_heights(
points,
faces,
boundary,
0.45,
)
tiny = self.balloon_poisson.poisson_balloon_heights(
points * 1e-5,
faces,
boundary,
0.45,
)
self.assertAlmostEqual(tiny[4], original[4] * 1e-5, places=12)
def test_teddy_balloon_uses_chordal_axis_radius(self):
import numpy as np
points = np.asarray(
((0, 0), (4, 0), (4, 2), (0, 2), (2, 1)),
dtype=float,
)
faces = ((0, 1, 4), (1, 2, 4), (2, 3, 4), (3, 0, 4))
boundary = np.asarray((True, True, True, True, False))
nodes = np.asarray(((1, 1), (3, 1)), dtype=float)
with patch.object(
self.balloon_teddy,
"chordal_axis",
return_value=(nodes, [(0, 1)], np.asarray((1.0, 1.0))),
):
heights = self.balloon_teddy.teddy_balloon_heights(
points, faces, points[:4], boundary, 0.45
)
self.assertTrue((heights[:4] == 0.0).all())
self.assertAlmostEqual(heights[4], 0.45, places=6)
def test_balloon_dispatches_distinct_height_algorithms(self):
import numpy as np
points = np.asarray(((0, 0),), dtype=float)
boundary = np.asarray((True,))
with (
patch.object(
self.balloon_geometry,
"poisson_balloon_heights",
return_value=np.asarray((1.0,)),
) as poisson,
patch.object(
self.balloon_geometry,
"teddy_balloon_heights",
return_value=np.asarray((2.0,)),
) as teddy,
):
poisson_result = self.balloon_geometry.balloon_heights(
"POISSON", points, (), points, boundary, 0.45
)
teddy_result = self.balloon_geometry.balloon_heights(
"TEDDY", points, (), points, boundary, 0.45
)
self.assertEqual(poisson_result[0], 1.0)
self.assertEqual(teddy_result[0], 2.0)
poisson.assert_called_once()
teddy.assert_called_once()
def test_balloon_geometry_has_a_shared_closed_seam(self):
from collections import Counter
import numpy as np
planar = np.asarray(
((0, 0), (2, 0), (2, 2), (0, 2), (1, 1)),
dtype=float,
)
faces_2d = ((0, 1, 4), (1, 2, 4), (2, 3, 4), (3, 0, 4))
boundary = np.asarray((True, True, True, True, False))
heights = np.asarray((0, 0, 0, 0, 1), dtype=float)
vertices, faces, uv = self.balloon_geometry.build_closed_geometry(
planar,
faces_2d,
boundary,
heights,
(0.0, 2.0, 0.0, 2.0),
)
edges = Counter()
for face in faces:
for start, end in zip(face, face[1:] + face[:1]):
edges[tuple(sorted((start, end)))] += 1
self.assertEqual(len(vertices), 6)
self.assertEqual(len(vertices), len(uv))
self.assertEqual(uv.dtype, np.float32)
self.assertTrue(edges)
self.assertTrue(all(users == 2 for users in edges.values()))
def test_balloon_tool_operators_are_not_registered(self):
self.assertNotIn(self.balloon.ExtractBalloon, self.addon.CLASSES)
self.assertNotIn(self.balloon.ActivateBalloonTool, self.addon.CLASSES)
self.assertNotIn(self.balloon.BalloonTool, self.addon.CLASSES)
self.assertIn(self.mesh.GenerateMesh, self.addon.CLASSES)
def test_balloon_ai_analysis_is_registered_and_forwarded(self):
import inspect
self.assertNotIn(self.balloon.AnalyzeBalloon, self.addon.CLASSES)
operator = self.balloon.ExtractBalloon
self.assertIn("contour_analysis", operator.__annotations__)
self.assertIn("depth_analysis", operator.__annotations__)
self.assertIn("depth_model", operator.__annotations__)
settings = self.addon.properties.AnyImageSettings.__annotations__
for name in (
"balloon_contour_analysis",
"balloon_depth_analysis",
):
self.assertIn(name, settings)
self.assertFalse(settings[name]["default"])
self.assertIn("balloon_depth_model", settings)
self.assertEqual(settings["balloon_contour_analysis"]["name"], "Contour")
self.assertEqual(settings["balloon_depth_analysis"]["name"], "Depth")
execute_source = inspect.getsource(operator.execute)
invoke_source = inspect.getsource(operator.invoke)
request_source = inspect.getsource(self.balloon.AnalyzeBalloon.request)
response_source = inspect.getsource(self.balloon.AnalyzeBalloon.response)
tool_source = inspect.getsource(self.balloon.BalloonTool.draw_settings)
self.assertIn("bpy.ops.anyimage.analyze_balloon", execute_source)
self.assertIn("ready = ai_ready()", invoke_source)
self.assertIn("status = ai_status()", tool_source)
self.assertIn('"anyimage.setup_ai_environment"', tool_source)
self.assertIn("ai_setup_label(status)", tool_source)
self.assertIn("prepare_image_input(", execute_source)
self.assertIn(
"settings.balloon_contour_analysis",
invoke_source,
)
self.assertIn(
"settings.balloon_depth_analysis",
invoke_source,
)
self.assertIn("self.depth_model = settings.balloon_depth_model", invoke_source)
self.assertIn("require_model(BEN2_MODEL_KEY)", request_source)
self.assertIn(
"require_model(self.depth_model)",
request_source,
)
self.assertIn('"polygon": polygon', request_source)
self.assertIn('result.file("contour")', response_source)
self.assertIn('result.file("depth")', response_source)
self.assertIn("depth_data=depth_data", response_source)
self.assertIn('push_undo("Extract Balloon")', response_source)
self.assertIn('"balloon_contour_analysis"', tool_source)
self.assertIn('"balloon_depth_analysis"', tool_source)
self.assertIn('"balloon_depth_model"', tool_source)
def test_balloon_analysis_requires_only_enabled_models(self):
operator = self.balloon.AnalyzeBalloon()
operator.input_path = "input.png"
operator.selection_data = "[[0,0],[2,0],[0,2]]"
operator.resolution_level = 5
operator.depth_model = "MOGE2_VITB_NORMAL"
required = []
with (
patch.object(self.balloon, "require_environment"),
patch.object(
self.balloon,
"require_model",
side_effect=required.append,
),
patch.object(
self.balloon,
"production_device",
return_value="CUDA",
),
):
operator.contour_analysis = True
operator.depth_analysis = False
contour_request = operator.request(None)
operator.contour_analysis = False
operator.depth_analysis = True
depth_request = operator.request(None)
self.assertEqual(
required,
[
self.addon.properties.BEN2_MODEL_KEY,
"MOGE2_VITB_NORMAL",
],
)
self.assertTrue(contour_request["contour_analysis"])
self.assertFalse(contour_request["depth_analysis"])
self.assertFalse(depth_request["contour_analysis"])
self.assertTrue(depth_request["depth_analysis"])
self.assertEqual(depth_request["depth_model"], "MOGE2_VITB_NORMAL")
def test_balloon_depth_fields_and_shape_nodes(self):
import inspect
import numpy as np
depth_data = {
"depth_signal": np.asarray(
((1.0, -2.0), (0.5, 0.0)),
dtype=np.float32,
),
"baseline_vector": np.zeros(3, dtype=np.float32),
"valid": np.ones((2, 2), dtype=bool),
"origin": (0, 0),
"crop_size": (2, 2),
"source_size": (2, 2),
}
vertices = (
(0.0, 0.0, 2.0),
(0.0, 0.0, 2.0),
(0.0, 0.0, -2.0),
(0.0, 0.0, -2.0),
)
uv = ((0.0, 1.0), (1.0, 1.0), (0.0, 0.0), (1.0, 0.0))
values, local_plane = self.balloon_depth.balloon_depth_fields(
vertices,
uv,
depth_data,
)
np.testing.assert_allclose(values, (1.5, -4.5, 0.5, -0.5))
self.assertAlmostEqual(float(np.median(values)), 0.0)
np.testing.assert_allclose(local_plane, (0.0, 0.0, 0.0))
self.assertEqual(
self.balloon.orient_balloon_vertices(((1.0, 2.0, 3.0),)),
[(-3.0, -1.0, 2.0)],
)
restored = np.asarray(self.balloon.BALLOON_AXIS_TO_SOURCE) @ np.asarray(
(-3.0, -1.0, 2.0, 1.0)
)
np.testing.assert_allclose(restored, (1.0, 2.0, 3.0, 1.0))
self.assertEqual(
self.balloon.orient_balloon_base_plane((0.2, 0.3, 0.4)),
(0.2, -0.3, -0.4),
)
source = inspect.getsource(self.balloon_depth.balloon_shape_node_group)
controller_source = inspect.getsource(
self.balloon_depth.create_balloon_base_plane
)
modifier_source = inspect.getsource(
self.balloon_depth.add_balloon_shape_modifier
)
self.assertIn('"Thickness"', source)
self.assertIn('"Depth Strength"', source)
self.assertIn('"Depth Strength": 1.0', modifier_source)
self.assertNotIn('"Far Clamp"', source)
self.assertIn('"Double Sided Depth"', source)
self.assertIn('"Base Plane"', source)
self.assertIn('nodes.new("GeometryNodeObjectInfo")', source)
self.assertIn("controller.parent = balloon", controller_source)
self.assertIn(
'controller.empty_display_type = "SINGLE_ARROW"', controller_source
)
self.assertIn('profile_factor.operation = "ABSOLUTE"', source)
self.assertIn('profile_statistics.data_type = "FLOAT"', source)
self.assertIn('profile_max_safe.operation = "MAXIMUM"', source)
self.assertIn('profile_normalized.operation = "DIVIDE"', source)
self.assertIn('profile_smooth.interpolation_type = "SMOOTHERSTEP"', source)
self.assertIn('profile_active.operation = "MULTIPLY"', source)
self.assertIn('effective_normal.input_type = "VECTOR"', source)
self.assertNotIn("o_balloon_depth_mix", source)
self.assertIn('front.operation = "LESS_THAN"', source)
self.assertIn('depth_residual.operation = "ADD"', source)
self.assertIn("depth_calibration.inputs[1].default_value = 0.5", source)
self.assertIn('corrected_depth.operation = "ADD"', source)
self.assertIn(
'profile_statistics.outputs["Max"], corrected_depth.inputs[1]',
source,
)
self.assertNotIn("baseline_thickness", source)
self.assertIn('separate.outputs["X"], base_scale.inputs[0]', source)
self.assertIn('double_choice.outputs["Output"], combine.inputs["X"]', source)
self.assertIn('depth_mix = nodes.new("ShaderNodeMix")', source)
self.assertIn('depth_mix.data_type = "FLOAT"', source)
self.assertIn("depth_mix.clamp_factor = True", source)
self.assertIn('base_magnitude.outputs[0], depth_mix.inputs["A"]', source)
self.assertIn('depth_scale.outputs[0], depth_mix.inputs["B"]', source)
self.assertNotIn("profile_inverse", source)
self.assertNotIn("thickness_component", source)
self.assertNotIn("depth_component", source)
self.assertNotIn("blended_magnitude", source)
self.assertIn('plane_offset.operation = "DOT_PRODUCT"', source)
self.assertIn("controller.location = (0.0, 0.0, 0.0)", controller_source)
self.assertNotIn("clamp_distance", source)
self.assertNotIn("safety_clamped", source)
self.assertNotIn("crossing_choice", source)
self.assertIn('front_choice.input_type = "FLOAT"', source)
self.assertIn('double_choice.input_type = "FLOAT"', source)
self.assertNotIn("double_inverse", source)
self.assertNotIn("depth_limited", source)
self.assertNotIn("side_gate", source)
self.assertIn('base_frame.label = "Base Thickness"', source)
self.assertIn('plane_frame.label = "Middle Plane"', source)
self.assertIn('mix_frame.label = "Depth Mix"', source)
self.assertIn('output_frame.label = "Side Output"', source)
self.assertIn('side.operation = "SIGN"', source)
self.assertIn("BALLOON_DEPTH_ATTRIBUTE", source)
self.assertEqual(
self.balloon_depth.BALLOON_DEPTH_ATTRIBUTE,
"o_balloon_depth",
)
def test_balloon_redo_properties_use_mode_and_meter_resolution(self):
import inspect
spacing, inflation = self.balloon.balloon_geometry_parameters(2.0, 0.2)
scene = SimpleNamespace(
unit_settings=SimpleNamespace(scale_length=0.01),
)
self.assertEqual(spacing, 0.2)
self.assertEqual(inflation, 0.9)
self.assertEqual(
self.balloon.meters_to_world_distance(scene, 0.1),
10.0,
)
operator = self.balloon.ExtractBalloon
self.assertEqual(operator.bl_options, {"REGISTER", "UNDO"})
self.assertIn("mode", operator.__annotations__)
self.assertIn("thickness", operator.__annotations__)
self.assertIn("resolution", operator.__annotations__)
self.assertIn("reuse_material", operator.__annotations__)
self.assertIn("source_matrix_data", operator.__annotations__)
settings = self.addon.properties.AnyImageSettings.__annotations__
self.assertIn("balloon_mode", settings)
self.assertIn("balloon_thickness", settings)
self.assertIn("balloon_resolution", settings)
self.assertIn("balloon_reuse_material", settings)
self.assertIn("balloon_thickness_radial_zoom", settings)
self.assertIn("balloon_resolution_radial_zoom", settings)
self.assertEqual(
settings["balloon_thickness_radial_zoom"]["default"],
(100.0, 100.0),
)
self.assertEqual(
settings["balloon_resolution_radial_zoom"]["default"],
(1000.0, 1000.0),
)
resolution_source = inspect.getsource(
self.addon.properties.balloon_resolution_property
)
thickness_source = inspect.getsource(
self.addon.properties.balloon_thickness_property
)
self.assertIn('name="Resolution (m)"', resolution_source)
self.assertNotIn("subtype=", resolution_source)
self.assertNotIn("unit=", resolution_source)
self.assertIn("soft_max=5.0", thickness_source)
self.assertNotIn(" max=", thickness_source)
def test_balloon_workspace_tool_uses_one_top_level_operator_per_stroke(self):
import inspect
execute_source = inspect.getsource(self.balloon.ExtractBalloon.execute)
invoke_source = inspect.getsource(self.balloon.ExtractBalloon.invoke)
modal_source = inspect.getsource(self.balloon.ExtractBalloon.modal)
self.assertIn("json.loads(self.selection_data)", execute_source)
self.assertIn("deserialize_matrix(self.source_matrix_data)", execute_source)
self.assertIn("extract_balloon_object(", execute_source)
self.assertIn("select_result=False", execute_source)
self.assertIn("reuse_material=self.reuse_material", execute_source)
self.assertIn("meters_to_world_distance(", execute_source)
self.assertIn("serialize_matrix(source_object.matrix_world)", invoke_source)
self.assertIn("settings.balloon_thickness", invoke_source)
self.assertIn("settings.balloon_resolution", invoke_source)
self.assertIn("settings.balloon_reuse_material", invoke_source)
self.assertIn("return self.execute(context)", modal_source)
self.assertIn('return {"RUNNING_MODAL"}', modal_source)
self.assertIn('return {"PASS_THROUGH"}', modal_source)
self.assertNotIn("bpy.ops.anyimage.extract_balloon", modal_source)
tool = self.balloon.BalloonTool
self.assertEqual(tool.bl_space_type, "VIEW_3D")
self.assertEqual(tool.bl_context_mode, "OBJECT")
self.assertEqual(tool.bl_operator, self.balloon.ExtractBalloon.bl_idname)
self.assertEqual(tool.bl_icon, "ops.mesh.polybuild_hover")
self.assertEqual(tool.bl_cursor, "PAINT_BRUSH")
keymap_operators = tuple(item[0] for item in tool.bl_keymap)
self.assertEqual(keymap_operators[0], self.balloon.ExtractBalloon.bl_idname)
self.assertEqual(
keymap_operators[1:],
("wm.radial_control", "wm.radial_control"),
)
self.assertEqual(
tool.bl_keymap[1][2]["properties"],
[
(
"data_path_primary",
"scene.anyimage_settings.balloon_thickness",
),
(
"zoom_path",
"scene.anyimage_settings.balloon_thickness_radial_zoom",
),
],
)
self.assertEqual(
tool.bl_keymap[2][2]["properties"],
[
(
"data_path_primary",
"scene.anyimage_settings.balloon_resolution",
),
(
"zoom_path",
"scene.anyimage_settings.balloon_resolution_radial_zoom",
),
],
)
def test_balloon_draw_handlers_are_filtered_to_the_invoking_viewport(self):
import inspect
source = inspect.getsource(self.balloon.drawing_in_region)
operator_source = inspect.getsource(self.balloon.ExtractBalloon)
self.assertIn("area.as_pointer() == area_pointer", source)
self.assertIn("region.as_pointer() == region_pointer", source)
self.assertIn("drawing_in_region(", operator_source)
self.assertNotIn("wireframe_opacity", operator_source)
self.assertNotIn("shading.type", operator_source)
def test_balloon_matrix_serialization_keeps_all_components(self):
import json
matrix = tuple(
tuple(row * 4 + column for column in range(4)) for row in range(4)
)
values = json.loads(self.balloon.serialize_matrix(matrix))
self.assertEqual(values, list(range(16)))
def test_extract_balloon_preserves_the_source_object(self):
import inspect
source = inspect.getsource(self.balloon.extract_balloon_object)
self.assertNotIn("objects.remove(source_object", source)
self.assertIn("balloon_name = source_name", source)
self.assertIn(
"selected_object = balloon if select_result else source_object", source
)
self.assertIn(
"balloon.matrix_world = orient_balloon_world_matrix(matrix_world)",
source,
)
self.assertIn("create_material(", source)
self.assertIn("material_name = material_data_name(source_object)", source)
self.assertIn(
"normal_required=uses_normal",
source,
)
self.assertIn("mark_balloon_material(material, image, normal_image)", source)
self.assertIn("merge_balloon_normal_image(normal_image, update_normal)", source)
self.assertIn("if reuse_material", source)
self.assertIn("color_image = image.copy()", source)
self.assertIn("color_image.name = color_name", source)
self.assertIn(
"material_name,\n color_image,\n 0.0,",
source,
)
self.assertIn(
"surface_group_name=IMAGE_MATERIAL_NODE_GROUP_NAME",
source,
)
self.assertIn("owns_material", source)
self.assertIn("owns_color_image", source)
self.assertIn("balloon_depth_fields(", source)
self.assertIn("assign_balloon_shape_attributes(", source)
self.assertIn("inflation=INFLATION", source)
self.assertIn("create_balloon_base_plane(", source)
self.assertIn("add_balloon_shape_modifier(", source)
self.assertNotIn("depth_strength", source)
self.assertIn("IMAGE_MATERIAL_NODE_GROUP_NAME", source)
self.assertIn("normal_path=", source)
self.assertEqual(
self.image_depth.TEXTURE_MATERIAL_NODE_GROUP_NAME,
"O Image Layer",
)
def test_balloon_material_reuse_uses_explicit_source_markers(self):
class MarkedMaterial(dict):
def __init__(self, name, **properties):
super().__init__(properties)
self.name = name
color_image = SimpleNamespace(name="cow.color")
other_image = SimpleNamespace(name="other")
marked = MarkedMaterial(
"color-material",
anyimage_balloon_material=True,
anyimage_source_image=color_image,
)
marked_other = MarkedMaterial(
"other-material",
anyimage_balloon_material=True,
anyimage_source_image=other_image,
)
normal_image = SimpleNamespace(name="cow.normal")
marked_normal = MarkedMaterial(
"normal-material",
anyimage_balloon_material=True,
anyimage_source_image=color_image,
anyimage_balloon_normal_image=normal_image,
)
self.fake_bpy.data = SimpleNamespace(
materials=[marked_other, marked_normal, marked],
images=[color_image],
)
self.assertIs(
self.balloon.reusable_balloon_material(color_image),
marked,
)
self.assertIs(
self.balloon.reusable_balloon_material(
color_image,
normal_required=True,
),
marked_normal,
)
self.assertIs(
self.balloon.reusable_color_image("cow.color"),
color_image,
)
def test_surface_and_depth_plane_material_names_use_image_data(self):
import inspect
surface_source = inspect.getsource(self.image_mesh.build_image_mesh)
original_depth_source = inspect.getsource(
self.depth_build.build_image_depth_plane
)
depth_source = inspect.getsource(self.depth_build.build_depth_plane)
expected = "material_data_name(source_object)"
self.assertIn(expected, surface_source)
self.assertIn(expected, original_depth_source)
self.assertIn(expected, depth_source)
def test_image_operators_stay_clickable_until_server_is_busy(self):
context = SimpleNamespace(
object=SimpleNamespace(data=SimpleNamespace()),
)
operators = (
(self.depth_operator, self.depth_operator.ConvertToDepthPlane),
(self.image_mesh, self.image_mesh.ConvertToSurface),
(self.image_background, self.image_background.RemoveImageBackground),
)
for busy, expected in ((False, True), (True, False)):
with (
self.subTest(busy=busy),
patch.object(
self.addon.runtime,
"server_busy",
return_value=busy,
),
):
for module, operator in operators:
with (
self.subTest(operator=operator.__name__),
patch.object(
module,
"is_image_empty",
return_value=True,
),
patch.object(
module,
"video_total_frames",
return_value=1,
),
):
self.assertEqual(operator.poll(context), expected)
def test_ui_reads_required_model_files_without_starting_server(self):
properties = importlib.import_module("anyimage.properties")
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
moge = root / "models" / "moge-2-vits-normal-onnx"
ben2 = root / "models" / "BEN2-ONNX" / "onnx"
wdn = root / "models" / "realesr-general-wdn-x4v3"
moge.mkdir(parents=True)
ben2.mkdir(parents=True)
wdn.mkdir(parents=True)
(moge / "model.onnx").touch()
(ben2 / "model_fp16.onnx").touch()
(wdn / "realesr-general-wdn-x4v3.onnx").touch()
with (
patch.object(
self.addon.runtime,
"storage_root",
return_value=root,
),
patch.object(
self.addon.runtime,
"resource",
side_effect=AssertionError("UI must not start the Server"),
),
):
self.assertTrue(properties.required_models_ready())
def test_runtime_request_uses_request_response_shape(self):
result = object()
responses = []
with patch.object(
self.addon.runtime.server,
"request",
return_value=result,
) as request:
returned = self.addon.runtime.request(
"example",
{"value": 3},
response=responses.append,
)
self.assertIs(returned, result)
request.assert_called_once_with(
"example",
{"value": 3},
response=responses.append,
progress=None,
cancel_check=None,
)
def test_server_request_returns_job_result_and_calls_response(self):
controller_type = importlib.import_module(
"blendjob.controller"
).ServerController
controller = controller_type.__new__(controller_type)
statuses = iter(
(
{"state": "running", "progress": 0.5},
{
"state": "succeeded",
"progress": 1.0,
"result": {"value": 7},
},
)
)
controller.submit = lambda _job_type, _parameters: {
"job_id": "job",
"directory": ".",
}
controller.status = lambda _job_id: next(statuses)
controller.mark_job_complete = lambda _job_id: None
responses = []
result = controller.request(
"example",
{"input": 3},
response=responses.append,
poll_interval=0.01,
)
self.assertEqual(result.value, {"value": 7})
self.assertEqual(responses, [result])
def test_runtime_keeps_server_as_an_entrypoint_string(self):
path, attribute = self.addon.runtime.server_entrypoint.rsplit(":", 1)
self.assertEqual(Path(path).name, "__init__.py")
self.assertEqual(Path(path).parent.name, "server")
self.assertEqual(attribute, "server")
def test_runtime_registers_system_operators_and_status_bar(self):
self.assertIn(
self.addon.clipboard_image.PasteClipboardImage,
self.addon.CLASSES,
)
self.assertIn(
self.addon.clipboard_image.TrackNativeCopy,
self.addon.CLASSES,
)
existing_context_draw = object()
existing_outliner_draw = object()
self.context_menu_draws.append(existing_context_draw)
self.outliner_menu_draws.append(existing_outliner_draw)
self.addon.register()
system_classes = self.addon.runtime.operator_classes()
self.assertTrue(set(system_classes) <= set(self.registered))
self.assertEqual(
self.registered_tools,
[
(
self.image_box.ImageBoxTool,
{
"group": True,
"separator": True,
},
),
(
self.image_lasso.ImageLassoTool,
{
"after": {self.image_box.ImageBoxTool.bl_idname},
"separator": False,
},
),
(
self.image_line.ImageLineTool,
{
"after": {self.image_lasso.ImageLassoTool.bl_idname},
"separator": False,
},
),
(
self.image_polyline.ImagePolylineTool,
{
"after": {self.image_line.ImageLineTool.bl_idname},
"separator": False,
},
),
(
self.image_perspective.ImagePerspectiveTool,
{
"separator": False,
},
),
(
self.addon.CutoutTool,
{
"after": {
self.image_perspective.ImagePerspectiveTool.bl_idname
},
"separator": True,
},
),
],
)
self.assertEqual(len(self.status_bar_draws), 1)
self.assertEqual(self.status_bar_draws[0]._owner, "anyimage")
self.assertIs(
self.context_menu_draws[0],
self.addon.draw_image_context_menu,
)
self.assertIs(
self.outliner_menu_draws[0],
self.addon.draw_image_context_menu,
)
self.assertTrue(hasattr(self.fake_bpy.types.Image, "anyimage_upscale_source"))
self.addon.unregister()
self.assertEqual(self.registered, [])
self.assertEqual(self.registered_tools, [])
self.assertEqual(self.status_bar_draws, [])
self.assertEqual(self.context_menu_draws, [existing_context_draw])
self.assertEqual(self.outliner_menu_draws, [existing_outliner_draw])
self.assertFalse(hasattr(self.fake_bpy.types.Image, "anyimage_upscale_source"))
def test_blender_image_exports_do_not_use_scene_render_color_management(self):
source_root = PROJECT_ROOT / "src" / "anyimage"
offenders = []
for path in source_root.rglob("*.py"):
if ".save_render(" in path.read_text(encoding="utf-8"):
offenders.append(path.relative_to(source_root))
self.assertEqual(offenders, [])
def test_status_bar_reads_runtime_state(self):
calls = []
class Layout:
def row(self, **_options):
return self
def progress(self, **options):
calls.append(("progress", options))
def operator(self, identifier, **_options):
calls.append(("operator", identifier))
runtime = self.addon.runtime
runtime.active_job = object()
runtime.progress = 0.4
runtime.message = "Working"
runtime._draw_status_bar(SimpleNamespace(layout=Layout()), None)
runtime.active_job = None
self.assertEqual(calls[0][1]["factor"], 0.4)
self.assertEqual(calls[0][1]["text"], "Working")
self.assertEqual(calls[1], ("operator", "anyimage.cancel_job"))
def test_moge_depth_request_is_written_on_the_operator(self):
operator = self.depth_operator.GenerateMoge2Depth()
operator.input_path = __file__
operator.source_object_name = ""
operator.model = "MOGE2_VITS_NORMAL"
operator.resolution_level = 6
operator.remove_background = False
settings = SimpleNamespace(video_frames=12)
context = SimpleNamespace(scene=SimpleNamespace(anyimage_settings=settings))
with (
patch.object(self.depth_operator, "require_environment"),
patch.object(self.depth_operator, "require_model"),
):
parameters = operator.request(context)
self.assertEqual(parameters["input"], __file__)
self.assertEqual(parameters["model"], "MOGE2_VITS_NORMAL")
self.assertEqual(parameters["resolution_level"], 6)
self.assertNotIn("job_type", parameters)
def test_moge_depth_response_calls_an_ordinary_function(self):
operator = self.depth_operator.GenerateMoge2Depth()
operator.source_object_name = "Reference"
operator.remove_background = False
operator.delete_source = True
operator.shadeless = False
result = object()
context = object()
with patch.object(self.depth_operator, "build_moge2_depth_response") as build:
operator.response(context, result)
build.assert_called_once_with(
context,
result,
source_object_name="Reference",
remove_background=False,
delete_source=True,
shadeless=False,
)
def test_entry_operator_dispatches_plain_properties(self):
operator = self.depth_operator.ConvertToDepthPlane()
operator.model = "MOGE2_VITS_NORMAL"
operator.resolution_level = 7
operator.keep_original = False
operator.shadeless = True
operator.remove_background = False
source_object = SimpleNamespace(
name="Reference",
data=SimpleNamespace(source="FILE"),
)
settings = SimpleNamespace(video_frames=1)
context = SimpleNamespace(
object=source_object,
scene=SimpleNamespace(anyimage_settings=settings),
)
calls = []
self.fake_bpy.ops = SimpleNamespace(
anyimage=SimpleNamespace(
generate_moge2_depth=lambda **options: (
calls.append(options) or {"RUNNING_MODAL"}
)
)
)
with patch.object(
self.depth_operator,
"prepare_image_analysis_inputs",
return_value=(
Path(__file__),
False,
Path(__file__),
False,
),
):
self.assertEqual(operator.execute(context), {"FINISHED"})
self.assertEqual(calls[0]["resolution_level"], 7)
self.assertEqual(calls[0]["source_object_name"], "Reference")
self.assertFalse(any(key.startswith("anyimage_") for key in calls[0]))
if __name__ == "__main__":
unittest.main()