"""Build the Service Weapon's upgradable resonant rounds and muzzle flare. Run inside Blender 5.x: exec(open("/home/dandovlatov/Documents/cntrl/tools/blender/build_projectile.py").read()) """ import math import os import sys import bpy from mathutils import Euler REPO = "/home/dandovlatov/Documents/cntrl" MODEL_DIR = os.path.join(REPO, "assets", "models") BLEND_DIR = os.path.join(REPO, "assets", "blender") CONCEPT_DIR = os.path.join(REPO, "docs", "concept") for d in (MODEL_DIR, BLEND_DIR, CONCEPT_DIR): os.makedirs(d, exist_ok=True) sys.path.insert(0, os.path.join(REPO, "tools", "blender")) import importlib import actor_kit as K importlib.reload(K) K.clear_scene() P = K.PALETTE P.update({ "shell": "#20242a", "shell_dk": "#12151a", "metal": "#88929c", "amber": "#ffab24", "board_hot": "#fff2c2", "white": "#ffffff", "resonance": "#4ec9b0", }) TO_FORWARD = Euler((math.pi / 2, 0, 0)).to_matrix() def blade(root, name, s, angle, value): outline = [(-0.42 * s, 0.09 * s), (0.02 * s, 0.38 * s), (0.46 * s, 0.16 * s), (0.34 * s, 0.0), (-0.38 * s, 0.0)] obj = K.plate(root, name, outline, -0.018 * s, 0.018 * s, 0.82, value, ao=0.04) ca, sa = math.cos(angle), math.sin(angle) for v in obj.data.vertices: u, w, t = v.co.x, v.co.y, v.co.z v.co = (w * ca - t * sa, w * sa + t * ca, u) obj.data.update() return obj def face_forward(root): for obj in root.children: if obj.type != "MESH": continue for v in obj.data.vertices: v.co = TO_FORWARD @ v.co obj.data.update() def build_round(name, path, s, core, hot, accent, fin_count, rings, spikes): r = K.new_actor(name) K.loft(r, "shell", [ {"z": -0.48 * s, "r": (0.035 * s, 0.035 * s), "n": 10}, {"z": -0.30 * s, "r": (0.115 * s, 0.115 * s), "n": 10, "fold": (0.06, 5)}, {"z": 0.02 * s, "r": (0.145 * s, 0.145 * s), "n": 10, "fold": (0.08, 6)}, {"z": 0.34 * s, "r": (0.105 * s, 0.105 * s), "n": 10}, {"z": 0.66 * s, "r": (0.022 * s, 0.022 * s), "n": 10}, ], P["shell"], ao=0.12) K.loft(r, "collar", [ {"z": -0.22 * s, "r": (0.155 * s, 0.155 * s), "n": 8}, {"z": -0.07 * s, "r": (0.175 * s, 0.175 * s), "n": 8}, ], P["metal"], ao=0.05) K.crystal(r, "energy_core", (0, 0, -0.10 * s), 0.52 * s, core, sides=6, seed=7, ao=0.0) K.crystal(r, "hot_tip", (0, 0, 0.40 * s), 0.26 * s, hot, sides=5, seed=13, ao=0.0) for i, rz in enumerate(rings): K.ring(r, "band_%d" % i, (0, 0, rz * s), 0.165 * s, 0.020 * s, accent, segments=12) for i in range(fin_count): blade(r, "fin_%d" % i, s, 2.0 * math.pi * i / fin_count + 0.35, accent) for i in range(spikes): a = 2.0 * math.pi * (i + 0.5) / spikes K.crystal(r, "spike_%d" % i, (0.16 * s * math.cos(a), 0.16 * s * math.sin(a), -0.24 * s), 0.30 * s, accent, sides=4, seed=20 + i, tilt=(math.radians(72) * math.sin(a), -math.radians(72) * math.cos(a), 0), ao=0.0) face_forward(r) mesh = K.merge_actor(r, name) tris = K.triangle_count(r) K.export_actor(r, name, path) print("[round] %-14s %4d tris %s" % (name, tris, path)) return r, mesh, tris def build_flare(name, path): s = 1.0 r = K.new_actor(name) K.blob(r, "flare_core", (0, 0, 0), (0.17, 0.17, 0.24), P["white"], subdiv=1, warp=0.0, ao=0.0) for i in range(8): a = 2.0 * math.pi * i / 8 K.crystal(r, "ray_%d" % i, (0, 0, 0), 0.60 * s, P["board_hot"] if i % 2 == 0 else P["amber"], sides=4, seed=40 + i, tilt=(math.pi / 2, 0, a + math.pi / 2), ao=0.0) K.loft(r, "throat", [ {"z": 0.0, "r": (0.13, 0.13), "n": 8}, {"z": 0.34, "r": (0.20, 0.20), "n": 8, "fold": (0.10, 4)}, {"z": 0.78, "r": (0.01, 0.01), "n": 8}, ], P["amber"], ao=0.0) face_forward(r) mesh = K.merge_actor(r, name) tris = K.triangle_count(r) K.export_actor(r, name, path) print("[flare] %-14s %4d tris %s" % (name, tris, path)) return r, mesh, tris built = [] for spec in ( ("projectile_1", 0.90, P["amber"], P["board_hot"], P["board"], 3, (0.10,), 0), ("projectile_2", 1.10, P["board"], P["white"], P["resonance"], 4, (-0.04, 0.22), 0), ("projectile_3", 1.35, P["hiss_hot"], P["board_hot"], P["board"], 6, (-0.14, 0.08, 0.28), 6), ): root, mesh, tris = build_round( spec[0], os.path.join(MODEL_DIR, spec[0] + ".glb"), *spec[1:]) assert tris < 1400, "%s over triangle budget" % root.name built.append((root, mesh)) flare_root, flare_mesh, flare_tris = build_flare( "muzzle_flash", os.path.join(MODEL_DIR, "muzzle_flash.glb")) assert flare_tris < 800, "muzzle_flash over triangle budget" built.append((flare_root, flare_mesh)) for i, (root, _) in enumerate(built): root.location = ((i - 1.5) * 1.7, 0.0, 0.0) for child in root.children: child.location = (0, 0, 0) world = bpy.data.worlds.new("ProjectileWorld") bpy.context.scene.world = world world.use_nodes = True bg = next(n for n in world.node_tree.nodes if n.type == "BACKGROUND") bg.inputs["Color"].default_value = (*K.rgb("#0e1418"), 1.0) bg.inputs["Strength"].default_value = 0.7 for name, loc, power, col in ( ("Key", (-3, -6, 5), 1800, "#e8f0e2"), ("Rim", (3, 5, 4), 1200, "#4ec9b0"), ("Fill", (0, -3, 2), 600, "#ff8a1f"), ): data = bpy.data.lights.new(name, "AREA") data.energy = power data.shape = "DISK" data.size = 5 data.color = K.rgb(col) obj = bpy.data.objects.new(name, data) bpy.context.scene.collection.objects.link(obj) obj.location = loc obj.rotation_euler = (K.Vector((0, 0, 0.5)) - K.Vector(loc)).to_track_quat( "-Z", "Y").to_euler() cam_data = bpy.data.cameras.new("ProjectileCam") cam = bpy.data.objects.new("ProjectileCam", cam_data) bpy.context.scene.collection.objects.link(cam) cam.location = (0.0, -7.0, 2.6) cam.rotation_euler = (K.Vector((0, 0, 0.45)) - cam.location).to_track_quat( "-Z", "Y").to_euler() cam_data.lens = 48 bpy.context.scene.camera = cam scene = bpy.context.scene engines = [i.identifier for i in bpy.types.RenderSettings.bl_rna.properties[ "engine"].enum_items] scene.render.engine = ("BLENDER_EEVEE_NEXT" if "BLENDER_EEVEE_NEXT" in engines else engines[0]) scene.render.resolution_x = 1100 scene.render.resolution_y = 700 scene.render.image_settings.file_format = "PNG" scene.render.filepath = os.path.join(CONCEPT_DIR, "projectile_reference.png") bpy.ops.wm.save_as_mainfile(filepath=os.path.join(BLEND_DIR, "fbc_projectile.blend")) print("[done] projectile rounds + flare built, exported and saved")