diff --git a/tools/blender/aetheria_ships/__init__.py b/tools/blender/aetheria_ships/__init__.py index 7f2583fd2..d5ed7465e 100644 --- a/tools/blender/aetheria_ships/__init__.py +++ b/tools/blender/aetheria_ships/__init__.py @@ -560,6 +560,16 @@ def _world_mesh(obj, depsgraph, matrix): return points.reshape(-1, 3) @ linear[:3, :3].T + linear[:3, 3], triangles.reshape(-1, 3) +def _triangles_xy(depsgraph, placements): + """The top-down triangles of the evaluated meshes, each through its matrix: what the author sees from above. + placements is [(object, matrix)].""" + triangles = [] + for obj, matrix in placements: + points, indices = _world_mesh(obj, depsgraph, matrix) + triangles += points[indices][:, :, :2].tolist() + return triangles + + def _grid_current(collection, state): """Whether the Grid can be drawn: grid_origin was computed for the buffer's own size.""" origin = collection.get("aetheria.grid_origin") @@ -619,20 +629,21 @@ def _redraw_grid(collection, state): def _rasterise(context, collection, state): """Proposes the hull's cells from the frame meshes, top-down in Ship Root's frame, into the layout buffer (its - hardpoints stay) and stores the grid's placement with the size it was computed for. Writes no file. The only writer - of aetheria.grid_origin.""" + hardpoints stay) and stores the grid's placement with the size it was computed for. Writes no file.""" root, meshes = _frame_inputs(context, collection, state) - depsgraph = context.evaluated_depsgraph_get() to_root = root.matrix_world.inverted() - triangles = [] - for obj in meshes: - points, indices = _world_mesh(obj, depsgraph, to_root @ obj.matrix_world) - triangles += points[indices][:, :, :2].tolist() - width, height, cells, origin = hull_grid.rasterise(triangles) + width, height, cells, origin = hull_grid.rasterise(_triangles_xy( + context.evaluated_depsgraph_get(), [(obj, to_root @ obj.matrix_world) for obj in meshes])) + _place_grid(collection, state, width, height, cells, origin) + return width, height + + +def _place_grid(collection, state, width, height, cells, origin): + """Puts a rasterised hull into the layout buffer (its hardpoints stay), stores the grid's placement with the size it + was computed for and redraws the Grid. The only writer of aetheria.grid_origin.""" _set_cells(state, width, height, cells) collection["aetheria.grid_origin"] = [float(origin[0]), float(origin[1]), width, height] _redraw_grid(collection, state) - return width, height class AETHERIA_OT_rasterise_hull(bpy.types.Operator): @@ -678,8 +689,10 @@ class AETHERIA_OT_flip_nose(bpy.types.Operator): def _new_ship(context, ship_id, name, reference, length): """Sets the active collection up as a pending ship: binds it, moves its meshes into Source under a Ship Root, turns - them nose to -Y, sizes them to length cells, fills the layout buffer and rasterises. Every check comes first and - nothing is written to disk: the first Save creates the file.""" + them nose to -Y, sizes them to length cells, fills the layout buffer and draws the Grid. The meshes keep their data: + the placement is the objects' own matrix, so modifiers act as the author saw them and the cells are the ones checked + here. Every check and the rasterising come first; after the bind nothing can refuse. Nothing is written to disk: the + first Save creates the file.""" if not valid_ship_id(ship_id): raise ValueError("The ship ID must use lower-case letters, digits, dots, underscores or hyphens, must not end " "in a dot, and must not be a Windows device name") @@ -696,8 +709,6 @@ def _new_ship(context, ship_id, name, reference, length): raise ValueError("The ship collection holds no mesh objects to make a ship from") if len(_meshes(collection.all_objects)) != len(meshes): raise ValueError("The ship collection's meshes must sit directly in it, not in child collections") - if any(obj.data.users > 1 for obj in meshes): - raise ValueError("Make the hull meshes single-user before New Ship (Object > Relations > Make Single User)") worlds = {obj: obj.matrix_world.copy() for obj in meshes} depsgraph = context.evaluated_depsgraph_get() points = numpy.vstack([_world_mesh(obj, depsgraph, worlds[obj])[0] for obj in meshes]) @@ -710,11 +721,7 @@ def _new_ship(context, ship_id, name, reference, length): if extent_x > extent_y: transform = transform @ Matrix.Rotation(math.pi / 2, 4, "Z") placed = {obj: transform @ worlds[obj] for obj in meshes} - triangles = [] - for obj in meshes: - placed_points, indices = _world_mesh(obj, depsgraph, placed[obj]) - triangles += placed_points[indices][:, :, :2].tolist() - width, height, cells, _ = hull_grid.rasterise(triangles) + width, height, cells, origin = hull_grid.rasterise(_triangles_xy(depsgraph, [(obj, placed[obj]) for obj in meshes])) _bind_collection(collection, str(path), ship_id, [name, reference]) source = _child_or_new(collection, SOURCE, SOURCE) root = bpy.data.objects.new(SHIP_ROOT, None) @@ -723,22 +730,15 @@ def _new_ship(context, ship_id, name, reference, length): for obj in meshes: collection.objects.unlink(obj) source.objects.link(obj) - obj.data.transform(placed[obj].to_3x3().to_4x4()) - if placed[obj].determinant() < 0: - obj.data.flip_normals() obj.parent = root obj.matrix_parent_inverse = Matrix.Identity(4) - obj.rotation_mode = "XYZ" - obj.rotation_euler = (0.0, 0.0, 0.0) - obj.scale = (1.0, 1.0, 1.0) - obj.location = placed[obj].to_translation() + obj.matrix_world = placed[obj] state = context.scene.aetheria_layout state.ship_id = ship_id state.ship_cc = bpy.path.abspath(collection["aetheria.ship_cc"]) state.revision = "" state.hardpoints.clear() - _set_cells(state, width, height, cells) - _rasterise(context, collection, state) + _place_grid(collection, state, width, height, cells, origin) class AETHERIA_OT_new_ship(bpy.types.Operator): diff --git a/tools/blender/tests/smoke_frame.py b/tools/blender/tests/smoke_frame.py index 9eb8e6a44..8672aa1b2 100644 --- a/tools/blender/tests/smoke_frame.py +++ b/tools/blender/tests/smoke_frame.py @@ -213,9 +213,10 @@ def main(): source = child(collection, "Source") require(source is not None and sphere.users_collection == (source,), "The sphere is not in Source alone") require(sphere.parent == root, "The sphere is not parented to Ship Root") - require(tuple(sphere.scale) == (1, 1, 1) and all(abs(a) < 1e-9 for a in sphere.rotation_euler), - "Rotation and scale were not applied to the sphere") - dx, dy, _ = sphere.dimensions + require(max(vertex.co.length for vertex in sphere.data.vertices) < 0.5 + 1e-6 and sphere.scale.x > 1, + "New Ship baked the placement into the mesh instead of the object's own matrix") + world = [sphere.matrix_world @ vertex.co for vertex in sphere.data.vertices] + dx, dy = (max(v[i] for v in world) - min(v[i] for v in world) for i in (0, 1)) print(f"DIMENSIONS {dx:.3f} x {dy:.3f}") require(abs(dy - 20.0) < 0.4 and dx < dy, f"The sphere's long extent is not 20 m on Y: {dx} x {dy}") width, height, cells, hardpoints = buffer(scene) @@ -247,6 +248,7 @@ def main(): result, message = new_ship(scene) require(result == {"FINISHED"}, f"New Ship after undo was refused: {message}") root = bpy.data.objects["Ship Root"] + placed_scale, placed_rotation = tuple(sphere.scale), tuple(sphere.rotation_euler) print("PASS 1 new ship ok") # Pass 2: a Save that fails in create or after it leaves nothing behind and tells the author nothing of the input; @@ -284,7 +286,7 @@ def main(): before_origin = list(collection["aetheria.grid_origin"]) select(bpy.data.objects["Source Sphere"]) bpy.ops.ed.undo_push(message="before scale") - bpy.data.objects["Source Sphere"].scale = (0.5, 1, 1) + bpy.data.objects["Source Sphere"].scale = (placed_scale[0] / 2, placed_scale[1], placed_scale[2]) result, message = run(bpy.ops.aetheria.rasterise_hull) require(result == {"FINISHED"}, f"Rasterise was refused: {message}") after = buffer(scene) @@ -332,9 +334,10 @@ def main(): collection = bpy.data.collections[COLLECTION] root = bpy.data.objects["Ship Root"] sphere = bpy.data.objects["Source Sphere"] - sphere.scale = (1, 1, 1) + sphere.scale = placed_scale centre = sphere.matrix_world.translation render = new_object(collection, "Hull", bpy.data.meshes["Sphere"].copy(), location=tuple(sphere.location)) + render.scale, render.rotation_euler = placed_scale, placed_rotation nub = new_object(collection, "Loose Nub", box("Nub", 4.0, 3.0, 0.5), location=(centre.x, centre.y + 12.0, 0)) select(render) snapshot = (buffer(bpy.context.scene), list(collection["aetheria.grid_origin"]), @@ -457,6 +460,8 @@ def main(): result, message = run(bpy.ops.aetheria.package_ship) require(result == {"FINISHED"}, f"Package after the failed export was refused: {scene.aetheria_package_report}") print("PASS 6 package ok") + mirror_pass() + print("PASS 6b mirror ok") guards_pass(aetheria_ships) print("PASS 7 guards ok") undo_pass() @@ -475,6 +480,35 @@ def ship_from_box(scene, name, ship_id, location): return collection, hull +def mirror_pass(): + """A half hull with a Mirror modifier, its long axis on X, gets the cells of the hull the author sees: the same + cells as the hand-mirrored solid, 10 long.""" + scene = bpy.context.scene + + def ship_cells(name, corners_y, modifier): + collection = bpy.data.collections.new(name) + scene.collection.children.link(collection) + mesh = bpy.data.meshes.new(name) + corners = [(x, y, z) for x in (0.0, 6.0) for y in corners_y for z in (-0.5, 0.5)] + mesh.from_pydata(corners, [], [(0, 1, 3, 2), (4, 6, 7, 5), (0, 4, 5, 1), (2, 3, 7, 6), (0, 2, 6, 4), (1, 5, 7, 3)]) + hull = new_object(collection, name + " Hull", mesh) + if modifier: + hull.modifiers.new("Mirror", "MIRROR").use_axis = (False, True, False) + select(hull) + result, message = new_ship(scene, "smoke.mirror", name, "Djinni", 10) + require(result == {"FINISHED"}, f"New Ship for {name} was refused: {message}") + made = buffer(bpy.context.scene)[:3] + result, message = run(bpy.ops.aetheria.rasterise_hull) # reads the objects as New Ship left them + require(result == {"FINISHED"} and buffer(bpy.context.scene)[:3] == made, + f"{name}: the cells New Ship drafted are not the cells of the hull it left: {message}") + return made + + half = ship_cells("Mirror Half", (0.0, 1.0), True) + solid = ship_cells("Mirror Solid", (-1.0, 1.0), False) + require(half[1] == 10, f"The mirrored half hull is {half[1]} cells long, not 10") + require(half == solid, "A Mirror-modified half hull does not get the cells of the hand-mirrored solid") + + def guards_pass(aetheria_ships): scene = bpy.context.scene path_a, path_b = str(MODS / GUARD_A / "ship.cc"), str(MODS / GUARD_B / "ship.cc")