- Implement tests for image layer functionality including alpha curve and depth layer behavior. - Create tests for image plane evaluation and modifier input handling. - Add tests to verify the saved asset inventory and its persistence. - Introduce tests for the documentation command to ensure correct project version deployment. - Validate the extension manifest structure and required permissions. - Develop tests for the packaging process, ensuring correct platform handling and dependency management. - Enhance image depth plane group functionality with new split threshold input and surface processing.
105 lines
4.7 KiB
Python
105 lines
4.7 KiB
Python
import bpy
|
|
import numpy as np
|
|
import pytest
|
|
|
|
|
|
from tests.support.materials import image_layer
|
|
from tools.nodes.groups.image_depth_layer import build_image_depth_layer_group
|
|
|
|
|
|
@pytest.mark.parametrize("image_layer", ["definition", "asset"], indirect=True)
|
|
def test_alpha_curve_preserves_endpoints_and_suppresses_soft_edges(image_layer):
|
|
inputs = {
|
|
item.name: item for item in image_layer.interface.items_tree
|
|
if item.item_type == "SOCKET" and item.in_out == "INPUT"
|
|
}
|
|
assert list(inputs) == [
|
|
"Color", "Alpha", "Normal", "Bump Scale", "Alpha Fix",
|
|
"Normal Scale", "Object Space",
|
|
]
|
|
panels = [item for item in image_layer.interface.items_tree if item.item_type == "PANEL"]
|
|
assert len(panels) == 1
|
|
assert panels[0].name == "Options" and panels[0].default_closed
|
|
assert [item.name for item in panels[0].interface_items] == ["Alpha Fix", "Normal Scale", "Object Space"]
|
|
strength = inputs["Alpha Fix"]
|
|
assert (strength.default_value, strength.min_value, strength.max_value) == (0, 0, 1)
|
|
assert strength.subtype == "FACTOR"
|
|
output = next(node for node in image_layer.nodes if node.type == "GROUP_OUTPUT")
|
|
curve = output.inputs["Alpha"].links[0].from_node
|
|
assert curve.type == "CURVE_FLOAT"
|
|
assert curve.inputs["Value"].links[0].from_socket.name == "Alpha"
|
|
assert curve.inputs["Factor"].links[0].from_socket.name == "Alpha Fix"
|
|
mapping = curve.mapping
|
|
np.testing.assert_allclose(
|
|
[point.location[:] for point in mapping.curves[0].points],
|
|
[(0, 0), (0.8, 0), (0.925, 0.925), (1, 1)],
|
|
atol=1e-6,
|
|
)
|
|
samples = np.array([
|
|
mapping.evaluate(mapping.curves[0], float(alpha))
|
|
for alpha in np.linspace(0, 1, 1001)
|
|
])
|
|
np.testing.assert_allclose(samples[:801], 0, atol=3e-4)
|
|
assert samples[-1] == pytest.approx(1)
|
|
assert np.all(np.diff(samples) >= -1e-6)
|
|
assert np.all((samples >= 0) & (samples <= 1))
|
|
assert mapping.evaluate(mapping.curves[0], 0.85) == pytest.approx(0.1593, abs=1e-3)
|
|
assert mapping.evaluate(mapping.curves[0], 0.925) == pytest.approx(0.925, abs=5e-4)
|
|
assert all(socket.is_linked or socket.hide for node in image_layer.nodes for socket in node.outputs)
|
|
|
|
|
|
def test_depth_layer_keeps_foreground_alpha_threshold(image_layer):
|
|
depth = build_image_depth_layer_group()
|
|
assert "Alpha Fix" not in {
|
|
item.name for item in depth.interface.items_tree if item.item_type == "SOCKET"
|
|
}
|
|
output = next(node for node in depth.nodes if node.type == "GROUP_OUTPUT")
|
|
threshold = output.inputs["Alpha"].links[0].from_node
|
|
assert threshold.operation == "GREATER_THAN"
|
|
assert threshold.inputs[1].default_value == 0.5
|
|
assert threshold.inputs[0].links[0].from_socket.name == "Alpha"
|
|
|
|
|
|
def test_alpha_adjustment_shader_blends_curve_strength(image_layer, tmp_path):
|
|
scene = bpy.context.scene
|
|
scene.render.engine = "CYCLES"
|
|
scene.cycles.device = "CPU"
|
|
scene.cycles.samples = 1
|
|
scene.cycles.use_denoising = False
|
|
scene.render.resolution_x = 96
|
|
scene.render.resolution_y = 32
|
|
scene.render.resolution_percentage = 100
|
|
scene.render.image_settings.file_format = "OPEN_EXR"
|
|
scene.render.image_settings.color_depth = "32"
|
|
scene.render.filepath = str(tmp_path / "alpha-strength.exr")
|
|
bpy.ops.object.camera_add(location=(0, 0, 5))
|
|
camera = bpy.context.object
|
|
camera.data.type = "ORTHO"
|
|
camera.data.ortho_scale = 3
|
|
scene.camera = camera
|
|
for index, factor in enumerate((0, 0.5, 1)):
|
|
bpy.ops.mesh.primitive_plane_add(size=1, location=(index - 1, 0, 0))
|
|
result = bpy.data.materials.new(f"Alpha {factor}")
|
|
result.use_nodes = True
|
|
nodes = result.node_tree.nodes
|
|
nodes.clear()
|
|
layer = nodes.new("ShaderNodeGroup")
|
|
layer.node_tree = image_layer
|
|
layer.inputs["Alpha"].default_value = 0.9
|
|
layer.inputs["Alpha Fix"].default_value = factor
|
|
emission = nodes.new("ShaderNodeEmission")
|
|
output = nodes.new("ShaderNodeOutputMaterial")
|
|
result.node_tree.links.new(layer.outputs["Alpha"], emission.inputs["Color"])
|
|
result.node_tree.links.new(emission.outputs[0], output.inputs["Surface"])
|
|
bpy.context.object.data.materials.append(result)
|
|
bpy.ops.render.render(write_still=True)
|
|
rendered = bpy.data.images.load(scene.render.filepath, check_existing=False)
|
|
try:
|
|
pixels = np.array(rendered.pixels[:]).reshape(32, 96, 4)
|
|
values = pixels[16, [16, 48, 80], 0]
|
|
assert values[0] == pytest.approx(0.9, abs=1e-4)
|
|
assert values[2] == pytest.approx(0.77396, abs=1e-3)
|
|
assert values[1] == pytest.approx((values[0] + values[2]) / 2, abs=1e-4)
|
|
finally:
|
|
bpy.data.images.remove(rendered)
|