|
| 1 | +"""Blender 5.2 coverage for import and Geometry Nodes utility workflows.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import bpy |
| 8 | +import pytest |
| 9 | + |
| 10 | +import typst_importer |
| 11 | +from typst_importer.node_groups import ( |
| 12 | + create_follow_curve_node_group, |
| 13 | + modifier_input, |
| 14 | +) |
| 15 | +from typst_importer.operators.path import configure_follow_path_animation |
| 16 | +from typst_importer.operators.textbox_import import ImportFromTextboxAsCurveOperator |
| 17 | +from typst_importer.operators.visibility import toggle_visibility |
| 18 | +from typst_importer.typst_to_svg import deduplicate_materials, typst_express |
| 19 | + |
| 20 | + |
| 21 | +pytestmark = pytest.mark.skipif( |
| 22 | + bpy.app.version < (5, 2, 0), |
| 23 | + reason="These workflows require Blender 5.2 or newer", |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(autouse=True) |
| 28 | +def clean_blender_data(): |
| 29 | + bpy.ops.wm.read_factory_settings(use_empty=True) |
| 30 | + bpy.context.scene.frame_set(1) |
| 31 | + yield |
| 32 | + bpy.ops.wm.read_factory_settings(use_empty=True) |
| 33 | + |
| 34 | + |
| 35 | +def _fcurve_frames(obj: bpy.types.Object, data_path: str) -> list[float]: |
| 36 | + action = obj.animation_data.action |
| 37 | + slot = obj.animation_data.action_slot |
| 38 | + fcurve = next( |
| 39 | + curve |
| 40 | + for layer in action.layers |
| 41 | + for strip in layer.strips |
| 42 | + for curve in strip.channelbag(slot).fcurves |
| 43 | + if curve.data_path == data_path |
| 44 | + ) |
| 45 | + return [point.co.x for point in fcurve.keyframe_points] |
| 46 | + |
| 47 | + |
| 48 | +def test_visibility_uses_typed_modifier_input_and_keyframes(): |
| 49 | + mesh = bpy.data.meshes.new("VisibilityMesh") |
| 50 | + obj = bpy.data.objects.new("VisibilityObject", mesh) |
| 51 | + bpy.context.scene.collection.objects.link(obj) |
| 52 | + |
| 53 | + modifier = toggle_visibility(obj, current_frame=5, make_visible=True) |
| 54 | + input_value, socket = modifier_input(modifier, "Visibility") |
| 55 | + |
| 56 | + assert input_value.type == "VALUE" |
| 57 | + assert input_value.value is False |
| 58 | + data_path = f'modifiers["{modifier.name}"].properties.inputs.{socket.identifier}.value' |
| 59 | + assert _fcurve_frames(obj, data_path) == [4.0, 5.0] |
| 60 | + |
| 61 | + |
| 62 | +def test_follow_path_uses_typed_object_and_factor_inputs(): |
| 63 | + mesh = bpy.data.meshes.new("FollowerMesh") |
| 64 | + follower = bpy.data.objects.new("Follower", mesh) |
| 65 | + bpy.context.scene.collection.objects.link(follower) |
| 66 | + |
| 67 | + curve_data = bpy.data.curves.new("Path", type="CURVE") |
| 68 | + path = bpy.data.objects.new("Path", curve_data) |
| 69 | + bpy.context.scene.collection.objects.link(path) |
| 70 | + spline = curve_data.splines.new("POLY") |
| 71 | + spline.points.add(1) |
| 72 | + spline.points[0].co = (0.0, 0.0, 0.0, 1.0) |
| 73 | + spline.points[1].co = (2.0, 0.0, 0.0, 1.0) |
| 74 | + |
| 75 | + modifier = follower.modifiers.new("FollowPath", type="NODES") |
| 76 | + modifier.node_group = create_follow_curve_node_group() |
| 77 | + configure_follow_path_animation(follower, modifier, path, current_frame=3) |
| 78 | + |
| 79 | + object_input, _ = modifier_input(modifier, "Object") |
| 80 | + factor_input, factor_socket = modifier_input(modifier, "Factor") |
| 81 | + assert object_input.type == "VALUE" |
| 82 | + assert object_input.value == path |
| 83 | + assert factor_input.type == "VALUE" |
| 84 | + assert factor_input.value == pytest.approx(0.0) |
| 85 | + factor_path = ( |
| 86 | + f'modifiers["{modifier.name}"].properties.inputs.' |
| 87 | + f"{factor_socket.identifier}.value" |
| 88 | + ) |
| 89 | + assert _fcurve_frames(follower, factor_path) == [3.0, 13.0] |
| 90 | + |
| 91 | + configure_follow_path_animation(follower, modifier, path, current_frame=8) |
| 92 | + assert _fcurve_frames(follower, factor_path) == [8.0, 18.0] |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.parametrize( |
| 96 | + ("kwargs", "object_type", "fill_mode"), |
| 97 | + [ |
| 98 | + ({"convert_to_mesh": False}, "CURVE", "BOTH"), |
| 99 | + ({}, "MESH", None), |
| 100 | + ( |
| 101 | + {"convert_to_mesh": False, "convert_to_unfilled_path": True}, |
| 102 | + "CURVE", |
| 103 | + "NONE", |
| 104 | + ), |
| 105 | + ], |
| 106 | +) |
| 107 | +def test_typst_import_modes(kwargs, object_type, fill_mode): |
| 108 | + collection = typst_express( |
| 109 | + '#rect(width: 12pt, height: 12pt, fill: rgb("#336699"))', |
| 110 | + name=f"pytest_{object_type}_{fill_mode}", |
| 111 | + **kwargs, |
| 112 | + ) |
| 113 | + |
| 114 | + objects = list(collection.objects) |
| 115 | + assert objects |
| 116 | + assert {obj.type for obj in objects} == {object_type} |
| 117 | + if fill_mode is not None: |
| 118 | + assert all(obj.data.fill_mode == fill_mode for obj in objects) |
| 119 | + |
| 120 | + |
| 121 | +def test_material_deduplication_preserves_unrelated_orphan_data(): |
| 122 | + unrelated_material = bpy.data.materials.new("KeepThisMaterial") |
| 123 | + collection = bpy.data.collections.new("MaterialDedup") |
| 124 | + bpy.context.scene.collection.children.link(collection) |
| 125 | + |
| 126 | + curve = bpy.data.curves.new("ImportedCurve", type="CURVE") |
| 127 | + material = bpy.data.materials.new("ImportedMaterial") |
| 128 | + curve.materials.append(material) |
| 129 | + collection.objects.link(bpy.data.objects.new("ImportedCurve", curve)) |
| 130 | + |
| 131 | + deduplicate_materials(collection) |
| 132 | + |
| 133 | + assert bpy.data.materials.get(unrelated_material.name) == unrelated_material |
| 134 | + |
| 135 | + |
| 136 | +def test_textbox_curve_importer_smoke_test(tmp_path: Path): |
| 137 | + typst_file = tmp_path / "textbox_input.typ" |
| 138 | + typst_file.write_text('#rect(width: 12pt, height: 12pt, fill: rgb("#336699"))') |
| 139 | + |
| 140 | + collection = ImportFromTextboxAsCurveOperator.import_typst( |
| 141 | + None, |
| 142 | + typst_file, |
| 143 | + ) |
| 144 | + |
| 145 | + assert collection.objects |
| 146 | + assert {obj.type for obj in collection.objects} == {"CURVE"} |
| 147 | + |
| 148 | + |
| 149 | +def test_addon_registers_and_unregisters_cleanly(): |
| 150 | + typst_importer.register() |
| 151 | + try: |
| 152 | + assert hasattr(bpy.ops.import_scene, "import_textbox_grease_pencil") |
| 153 | + assert hasattr(bpy.ops.export_scene, "typst_svg") |
| 154 | + finally: |
| 155 | + typst_importer.unregister() |
0 commit comments