"""Tidewall's item models and icons (A4), built in headless Blender. blender --background --factory-startup --python tools/models/items.py -- [--only=id,id] [--no-icons] Writes, for every item the game defines (every weapon including the boathook, every consumable and every ammo type): assets/models/items/.obj the model, with .mtl naming its materials assets/models/items/.json its grip, support point and bounds assets/icons/items/.png a 128 px icon, rendered on the CPU with Cycles Every model is built here from primitives, nothing else. OBJ rather than glTF because the Debian Blender's glTF exporter needs numpy, which isn't installed. Conventions, in Godot's axes after export (Blender +Z up becomes +Y up, Blender -Y becomes +Z): - The origin is the grip: where the right hand closes. Long weapons also have an empty named "support" where the left hand goes. - The muzzle or working end points along +Z (Blender -Y), the way a figure faces. - Each mesh's material is named after the Palette colour it takes (PLANK, SALVAGE_IRON, GILT and so on); the client recolours them from Palette, so palette.gd stays the one place colours live. The icons read the same values from palette.gd. - The tier treatment's parts have materials named TRIM2_ (rope whipping, from tier 2) and TRIM3_ (brass bands, from tier 3). The client shows them by tier, and draws tier 0 weathered. Everything is deterministic: no randomness, fixed render seed. """ import json import math import os import re import sys import bpy from mathutils import Vector ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) MODEL_DIR = os.path.join(ROOT, "assets", "models", "items") ICON_DIR = os.path.join(ROOT, "assets", "icons", "items") PALETTE_PATH = os.path.join(ROOT, "src", "client", "style", "palette.gd") ICON_SIZE = 128 def read_palette(): colours = {} pattern = re.compile(r"^const ([A-Z_]+): Color = Color\(([0-9.]+), ([0-9.]+), ([0-9.]+)\)") with open(PALETTE_PATH) as source: for line in source: found = pattern.match(line) if found: colours[found.group(1)] = tuple(float(found.group(i)) for i in range(2, 5)) return colours PALETTE = read_palette() def srgb_to_linear(value): return value / 12.92 if value <= 0.04045 else ((value + 0.055) / 1.055) ** 2.4 # --- Scene and materials -------------------------------------------------------- def clear_scene(): bpy.ops.wm.read_factory_settings(use_empty=True) def material(name): found = bpy.data.materials.get(name) if found is not None: return found made = bpy.data.materials.new(name) made.use_nodes = True rgb = PALETTE[name.split("_", 1)[1] if name.startswith("TRIM") else name] shader = made.node_tree.nodes.get("Principled BSDF") shader.inputs["Base Color"].default_value = (srgb_to_linear(rgb[0]), srgb_to_linear(rgb[1]), srgb_to_linear(rgb[2]), 1.0) shader.inputs["Roughness"].default_value = 0.8 made.diffuse_color = (srgb_to_linear(rgb[0]), srgb_to_linear(rgb[1]), srgb_to_linear(rgb[2]), 1.0) return made def finish(obj, colour, name=None, bevel=0.0): obj.data.materials.clear() obj.data.materials.append(material(colour)) if name: obj.name = name if bevel > 0.0: mod = obj.modifiers.new("bevel", "BEVEL") mod.width = bevel mod.segments = 1 mod.limit_method = "ANGLE" for poly in obj.data.polygons: poly.use_smooth = False return obj # Builders take sizes in metres and positions in Blender space: +Y is the item's back, # so the working end lies toward -Y. def box(size, at, colour, name=None, rot=(0.0, 0.0, 0.0), bevel=0.004): bpy.ops.mesh.primitive_cube_add(size=1.0, location=at, rotation=rot) obj = bpy.context.active_object obj.scale = size bpy.ops.object.transform_apply(scale=True) return finish(obj, colour, name, bevel) def tube(radius, length, at, colour, axis="Y", name=None, sides=10, radius2=None, rot_extra=(0.0, 0.0, 0.0)): rotation = {"X": (0.0, math.pi / 2, 0.0), "Y": (math.pi / 2, 0.0, 0.0), "Z": (0.0, 0.0, 0.0)}[axis] rotation = tuple(a + b for a, b in zip(rotation, rot_extra)) if radius2 is None: bpy.ops.mesh.primitive_cylinder_add(vertices=sides, radius=radius, depth=length, location=at, rotation=rotation) else: bpy.ops.mesh.primitive_cone_add(vertices=sides, radius1=radius, radius2=radius2, depth=length, location=at, rotation=rotation) return finish(bpy.context.active_object, colour, name) def ball(radius, at, colour, name=None, rings=6, segments=10): bpy.ops.mesh.primitive_uv_sphere_add(segments=segments, ring_count=rings, radius=radius, location=at) return finish(bpy.context.active_object, colour, name) def ring(radius, thickness, at, colour, axis="Y", name=None): rotation = {"X": (0.0, math.pi / 2, 0.0), "Y": (math.pi / 2, 0.0, 0.0), "Z": (0.0, 0.0, 0.0)}[axis] bpy.ops.mesh.primitive_torus_add(major_radius=radius, minor_radius=thickness, major_segments=14, minor_segments=5, location=at, rotation=rotation) return finish(bpy.context.active_object, colour, name) SUPPORT = {} def support(at): SUPPORT["at"] = at def join(parts, name): """Marks parts as a tier's treatment: "trim_2" or "trim_3".""" level = name.split("_")[1] for part in parts: colour = part.data.materials[0].name part.data.materials.clear() part.data.materials.append(material("TRIM%s_%s" % (level, colour))) part.name = name return parts[0] def bands(positions, radius, colour, name): parts = [ring(radius, 0.008, (0.0, y, z), colour) for (y, z) in positions] return join(parts, name) def stock(length, at_y, colour, drop=0.04): """A shoulder stock behind the grip, sloping down.""" return box((0.045, length, 0.09), (0.0, at_y + length / 2, -0.03 - drop), colour, rot=(math.radians(-6), 0.0, 0.0)) # --- Weapons -------------------------------------------------------------------- def boathook(): tube(0.018, 1.7, (0.0, -0.45, 0.0), "DRIFTWOOD", sides=8) tube(0.022, 0.12, (0.0, -1.32, 0.0), "SALVAGE_IRON", sides=8) tube(0.012, 0.22, (0.0, -1.46, 0.0), "SALVAGE_IRON", radius2=0.002, sides=6) # The hook: a curved tine sweeping back from the head. tube(0.011, 0.16, (0.0, -1.31, 0.08), "SALVAGE_IRON", axis="Z", sides=6) tube(0.010, 0.10, (0.0, -1.27, 0.15), "SALVAGE_IRON", rot_extra=(math.radians(30), 0.0, 0.0), sides=6, radius2=0.002) join([tube(0.021, 0.06, (0.0, y, 0.0), "CANVAS", sides=8) for y in (0.05, -0.55)], "trim_2") join([tube(0.024, 0.03, (0.0, y, 0.0), "GILT", sides=8) for y in (-1.22, 0.38)], "trim_3") support((0.0, -0.6, 0.0)) def fowling_piece(): stock(0.42, 0.04, "PLANK") box((0.05, 0.5, 0.06), (0.0, -0.18, 0.02), "PLANK") tube(0.03, 0.75, (0.0, -0.62, 0.07), "SALVAGE_IRON", sides=10) # The bell mouth that gives the scattergun its outline. tube(0.03, 0.14, (0.0, -1.05, 0.07), "SALVAGE_IRON", radius2=0.065, sides=12) box((0.02, 0.06, 0.05), (0.0, 0.0, -0.04), "TAR") join([ring(0.034, 0.008, (0.0, y, 0.07), "CANVAS") for y in (-0.38, -0.5)], "trim_2") bands([(-0.3, 0.07), (-0.7, 0.07), (-0.95, 0.07)], 0.034, "GILT", "trim_3") support((0.0, -0.42, 0.0)) def dock_carbine(): stock(0.36, 0.04, "BARK") box((0.045, 0.42, 0.06), (0.0, -0.16, 0.015), "BARK") tube(0.017, 0.62, (0.0, -0.55, 0.055), "SALVAGE_IRON", sides=8) # Lever loop under the grip. ring(0.04, 0.007, (0.0, 0.02, -0.07), "SALVAGE_IRON", axis="X") box((0.03, 0.12, 0.04), (0.0, -0.04, 0.05), "TAR") join([ring(0.022, 0.007, (0.0, y, 0.055), "CANVAS") for y in (-0.42, -0.6)], "trim_2") bands([(-0.3, 0.05), (-0.78, 0.055)], 0.024, "GILT", "trim_3") support((0.0, -0.36, 0.0)) def signal_revolver(): box((0.04, 0.07, 0.13), (0.0, 0.02, -0.05), "BARK", rot=(math.radians(-15), 0.0, 0.0)) tube(0.03, 0.08, (0.0, -0.04, 0.04), "SALVAGE_IRON", sides=8) # A short, fat flare barrel, painted. tube(0.028, 0.2, (0.0, -0.18, 0.045), "OCHRE_RED", sides=12) tube(0.032, 0.02, (0.0, -0.285, 0.045), "SALVAGE_IRON", sides=12) box((0.01, 0.04, 0.03), (0.0, 0.03, 0.06), "TAR") join([tube(0.042, 0.04, (0.0, 0.02, -0.06), "CANVAS", axis="Z", sides=8)], "trim_2") bands([(-0.1, 0.045), (-0.26, 0.045)], 0.031, "GILT", "trim_3") def rivet_repeater(): box((0.07, 0.45, 0.1), (0.0, -0.12, 0.02), "SALVAGE_IRON") box((0.045, 0.1, 0.13), (0.0, 0.03, -0.07), "TAR", rot=(math.radians(-10), 0.0, 0.0)) tube(0.02, 0.3, (0.0, -0.5, 0.04), "SALVAGE_IRON", sides=8) # The rivet drum on top: the repeater's outline. tube(0.075, 0.06, (0.0, -0.1, 0.12), "TAR", axis="X", sides=14) ball(0.012, (0.0, -0.1, 0.12), "SALVAGE_IRON") for y in (-0.25, -0.02, 0.06): ball(0.01, (0.037, y, 0.05), "SALVAGE_IRON", rings=4, segments=6) stock(0.22, 0.08, "TAR", drop=0.0) join([ring(0.03, 0.008, (0.0, -0.45, 0.04), "CANVAS")], "trim_2") bands([(-0.55, 0.04), (-0.33, 0.04)], 0.026, "GILT", "trim_3") support((0.0, -0.3, -0.02)) def whalers_longgun(): stock(0.45, 0.04, "PLANK") box((0.05, 0.62, 0.065), (0.0, -0.24, 0.02), "PLANK") # A long octagonal barrel. tube(0.024, 1.1, (0.0, -0.95, 0.065), "SALVAGE_IRON", sides=8) tube(0.03, 0.08, (0.0, -1.46, 0.065), "SALVAGE_IRON", sides=8) # A long brass sight tube, a whaler's spotting glass. tube(0.016, 0.34, (0.0, -0.18, 0.13), "TAR", sides=8) box((0.012, 0.03, 0.04), (0.0, -0.08, 0.1), "TAR") box((0.012, 0.03, 0.04), (0.0, -0.3, 0.1), "TAR") join([ring(0.028, 0.008, (0.0, y, 0.065), "CANVAS") for y in (-0.6, -0.75, -0.9)], "trim_2") bands([(-0.5, 0.065), (-1.2, 0.065), (-1.4, 0.065)], 0.03, "GILT", "trim_3") support((0.0, -0.55, 0.0)) def line_thrower(): # A wide launch tube with the line's canister slung under it. tube(0.055, 0.7, (0.0, -0.3, 0.06), "SEA_PAINT", sides=12) tube(0.06, 0.03, (0.0, -0.66, 0.06), "SALVAGE_IRON", sides=12) tube(0.06, 0.16, (0.0, -0.25, -0.07), "CANVAS", axis="X", sides=12) ring(0.045, 0.012, (0.0, -0.25, -0.07), "TAR", axis="X") box((0.04, 0.08, 0.12), (0.0, 0.02, -0.05), "BARK", rot=(math.radians(-12), 0.0, 0.0)) box((0.04, 0.2, 0.05), (0.0, 0.14, 0.03), "BARK") join([ring(0.058, 0.008, (0.0, y, 0.06), "CANVAS") for y in (-0.05, -0.5)], "trim_2") bands([(-0.15, 0.06), (-0.6, 0.06)], 0.06, "GILT", "trim_3") support((0.0, -0.4, -0.02)) def harpoon_gun(): tube(0.04, 0.55, (0.0, -0.25, 0.06), "SEA_PAINT", sides=10) # The harpoon standing out of the muzzle, barbed. tube(0.01, 0.4, (0.0, -0.65, 0.06), "SALVAGE_IRON", sides=6) tube(0.03, 0.12, (0.0, -0.9, 0.06), "SALVAGE_IRON", radius2=0.0, sides=6) for side in (-1, 1): box((0.008, 0.07, 0.012), (side * 0.025, -0.83, 0.06), "SALVAGE_IRON", rot=(0.0, 0.0, side * math.radians(25)), bevel=0.0) # The spring housing and grip. tube(0.05, 0.12, (0.0, 0.05, 0.06), "SALVAGE_IRON", sides=10) box((0.04, 0.08, 0.12), (0.0, 0.03, -0.05), "BARK", rot=(math.radians(-12), 0.0, 0.0)) stock(0.25, 0.1, "BARK", drop=0.0) join([ring(0.043, 0.008, (0.0, y, 0.06), "CANVAS") for y in (-0.1, -0.4)], "trim_2") bands([(-0.2, 0.06), (-0.5, 0.06)], 0.044, "GILT", "trim_3") support((0.0, -0.32, 0.0)) def keg(scale=1.0): tube(0.13 * scale, 0.3 * scale, (0.0, 0.0, 0.0), "PLANK", axis="Z", sides=12) tube(0.145 * scale, 0.12 * scale, (0.0, 0.0, 0.0), "PLANK", axis="Z", sides=12) for z in (-0.12, 0.0, 0.12): ring(0.14 * scale if z == 0.0 else 0.133 * scale, 0.008, (0.0, 0.0, z * scale), "TAR", axis="Z") def powder_keg(): keg() box((0.012, 0.16, 0.012), (0.08, 0.0, 0.07), "OCHRE_RED", rot=(0.0, 0.0, math.radians(45)), bevel=0.0) box((0.012, 0.16, 0.012), (0.08, 0.0, 0.07), "OCHRE_RED", rot=(0.0, 0.0, math.radians(-45)), bevel=0.0) tube(0.006, 0.12, (0.0, 0.0, 0.2), "CANVAS", axis="Z", sides=6) join([ring(0.137, 0.01, (0.0, 0.0, 0.06), "CANVAS", axis="Z")], "trim_2") join([ring(0.137, 0.01, (0.0, 0.0, -0.06), "GILT", axis="Z")], "trim_3") # --- Consumables ---------------------------------------------------------------- def sailcloth_bandage(): tube(0.05, 0.09, (0.0, 0.0, 0.0), "CANVAS", axis="X", sides=12) tube(0.02, 0.092, (0.0, 0.0, 0.0), "PLANK_BLEACHED", axis="X", sides=8) box((0.09, 0.08, 0.004), (0.0, -0.08, -0.046), "CANVAS", bevel=0.0) ring(0.051, 0.004, (0.0, 0.0, 0.0), "OCHRE_RED", axis="X") def brine_tonic(): tube(0.04, 0.12, (0.0, 0.0, 0.0), "SEA_PAINT", axis="Z", sides=10) tube(0.04, 0.04, (0.0, 0.0, 0.08), "SEA_PAINT", axis="Z", radius2=0.014, sides=10) tube(0.014, 0.04, (0.0, 0.0, 0.12), "SEA_PAINT", axis="Z", sides=8) tube(0.016, 0.03, (0.0, 0.0, 0.15), "CRATE_WOOD", axis="Z", sides=8) box((0.05, 0.002, 0.05), (0.0, -0.04, 0.0), "PAPER", bevel=0.0) def cork_vest(): for row in range(3): for col in (-1, 1): box((0.11, 0.06, 0.08), (col * 0.07, 0.0, 0.1 - row * 0.09), "TANNED_SAIL", bevel=0.01) for col in (-1, 1): box((0.03, 0.065, 0.26), (col * 0.07, 0.01, 0.06), "CANVAS", bevel=0.0) box((0.03, 0.1, 0.02), (col * 0.07, 0.03, 0.2), "CANVAS", bevel=0.0) def tar_patch(): box((0.14, 0.14, 0.012), (0.0, 0.0, 0.0), "TAR", bevel=0.003) box((0.11, 0.11, 0.004), (0.0, 0.0, 0.008), "TAR_WEATHERED", bevel=0.0) tube(0.035, 0.05, (0.06, -0.06, 0.03), "SALVAGE_IRON", axis="Z", sides=10) # --- Ammo ----------------------------------------------------------------------- def birdshot(): ball(0.06, (0.0, 0.0, 0.0), "CANVAS", rings=6, segments=10) tube(0.02, 0.05, (0.0, 0.0, 0.065), "CANVAS", axis="Z", sides=8, radius2=0.03) ring(0.02, 0.005, (0.0, 0.0, 0.05), "TAR", axis="Z") def cartridges(): for i in range(5): tube(0.012, 0.09, ((i - 2) * 0.025, 0.0, 0.0), "PAPER", axis="Z", sides=8) ball(0.011, ((i - 2) * 0.025, 0.0, 0.05), "SALVAGE_IRON", rings=4, segments=6) box((0.13, 0.03, 0.012), (0.0, 0.0, -0.01), "TAR", bevel=0.0) def heavy_balls(): for (x, y, z) in ((-0.035, 0.0, 0.0), (0.035, 0.0, 0.0), (0.0, 0.0, 0.055)): ball(0.035, (x, y, z), "SALVAGE_IRON", rings=6, segments=10) box((0.14, 0.08, 0.01), (0.0, 0.0, -0.038), "PLANK", bevel=0.0) def lines(): for z in range(4): ring(0.07, 0.012, (0.0, 0.0, z * 0.022), "CANVAS", axis="Z") tube(0.008, 0.12, (0.08, -0.04, 0.04), "CANVAS", rot_extra=(0.0, 0.0, 0.5), sides=6) def harpoons(): for x in (-0.03, 0.03): tube(0.008, 0.34, (x, 0.0, 0.0), "SALVAGE_IRON", sides=6) tube(0.02, 0.07, (x, -0.2, 0.0), "SALVAGE_IRON", radius2=0.0, sides=6) ring(0.04, 0.008, (0.0, 0.05, 0.0), "CANVAS") ITEMS = { "boathook": boathook, "fowling_piece": fowling_piece, "dock_carbine": dock_carbine, "signal_revolver": signal_revolver, "rivet_repeater": rivet_repeater, "whalers_longgun": whalers_longgun, "line_thrower": line_thrower, "harpoon_gun": harpoon_gun, "powder_keg": powder_keg, "sailcloth_bandage": sailcloth_bandage, "brine_tonic": brine_tonic, "cork_vest": cork_vest, "tar_patch": tar_patch, "birdshot": birdshot, "cartridges": cartridges, "heavy_balls": heavy_balls, "lines": lines, "harpoons": harpoons, } # --- Export and icons ----------------------------------------------------------- def godot_axes(point): """Blender (x, y, z) to Godot's (x, z, -y), as the OBJ export turns them.""" return [round(point[0], 4), round(point[2], 4), round(-point[1], 4)] def export(item_id): path = os.path.join(MODEL_DIR, item_id + ".obj") bpy.ops.wm.obj_export( filepath=path, export_selected_objects=False, apply_modifiers=True, export_uv=False, export_normals=True, export_materials=True, export_triangulated_mesh=True, forward_axis="NEGATIVE_Z", up_axis="Y", export_object_groups=False, export_material_groups=False, path_mode="STRIP", ) low, high = mesh_bounds() info = { "grip": [0.0, 0.0, 0.0], "support": godot_axes(SUPPORT["at"]) if "at" in SUPPORT else None, "low": godot_axes((low.x, high.y, low.z)), "high": godot_axes((high.x, low.y, high.z)), "generator": "tools/models/items.py", } with open(os.path.join(MODEL_DIR, item_id + ".json"), "w") as out: json.dump(info, out, indent="\t", sort_keys=True) out.write("\n") def mesh_bounds(): points = [] for obj in bpy.context.scene.objects: if obj.type != "MESH": continue for corner in obj.bound_box: points.append(obj.matrix_world @ Vector(corner)) low = Vector((min(p.x for p in points), min(p.y for p in points), min(p.z for p in points))) high = Vector((max(p.x for p in points), max(p.y for p in points), max(p.z for p in points))) return low, high def render_icon(item_id): scene = bpy.context.scene # Icons show the plain item: tiers are told by the HUD's pips. for obj in list(scene.objects): if obj.type == "MESH" and obj.data.materials and obj.data.materials[0].name.startswith("TRIM"): bpy.data.objects.remove(obj) low, high = mesh_bounds() centre = (low + high) / 2 size = max((high - low).length, 0.05) # A three-quarter view from the right side and a little above, like a printed # catalogue plate. direction = Vector((1.0, 0.35, 0.55)).normalized() bpy.ops.object.camera_add(location=centre + direction * size * 3.0) camera = bpy.context.active_object camera.data.type = "ORTHO" camera.data.ortho_scale = size * 1.05 look = centre - camera.location camera.rotation_euler = look.to_track_quat("-Z", "Y").to_euler() scene.camera = camera bpy.ops.object.light_add(type="SUN", location=centre + Vector((2.0, 1.0, 3.0))) sun = bpy.context.active_object sun.data.energy = 3.5 sun.rotation_euler = (centre - sun.location).to_track_quat("-Z", "Y").to_euler() world = bpy.data.worlds.new("icon_world") world.use_nodes = True world.node_tree.nodes["Background"].inputs["Color"].default_value = (0.35, 0.33, 0.30, 1.0) world.node_tree.nodes["Background"].inputs["Strength"].default_value = 0.6 scene.world = world scene.render.engine = "CYCLES" scene.cycles.device = "CPU" scene.cycles.samples = 24 scene.cycles.seed = 1 scene.cycles.use_denoising = False scene.render.film_transparent = True scene.render.resolution_x = ICON_SIZE scene.render.resolution_y = ICON_SIZE scene.render.resolution_percentage = 100 scene.render.image_settings.file_format = "PNG" scene.render.image_settings.color_mode = "RGBA" scene.render.threads_mode = "FIXED" scene.render.threads = 2 scene.view_settings.view_transform = "Standard" scene.render.filepath = os.path.join(ICON_DIR, item_id + ".png") bpy.ops.render.render(write_still=True) def main(): args = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [] only = None icons = True for arg in args: if arg.startswith("--only="): only = arg.split("=", 1)[1].split(",") elif arg == "--no-icons": icons = False os.makedirs(MODEL_DIR, exist_ok=True) os.makedirs(ICON_DIR, exist_ok=True) for item_id, build in ITEMS.items(): if only and item_id not in only: continue clear_scene() SUPPORT.clear() build() export(item_id) if icons: render_icon(item_id) print("item %s done" % item_id) main()