Skip to content
1        10        20        30        40        50        60        70        80        90        100       110       120       130       140       150       1601"""Rig and animate the procedural heroine models for Torment.23The heroine scripts build a pile of separate rigid mesh parts, which makes them4ideal for lightweight object-to-bone parenting rather than skinning.  This5module:67* pulls head/hair/face parts toward the neck so the silhouettes read as adult8  proportions instead of chibi figurines;9* creates a compact humanoid armature (hips, spine, chest, neck, head, arms and10  legs);11* parents every part to the matching bone while preserving its world transform;12* writes Idle, Attack and Hit actions and exports them as glTF clips.1314The game's renderer plays those clips with AnimationMixer.  Keeping the rig15object-based means no skin weights, no vertex-group bookkeeping, and the16existing procedural material/part names stay intact.17"""1819import math20import re2122import bpy23from mathutils import Vector2425# Explicit joint positions in Blender space (Z up, model faces -Y).26ANATOMY = {27    "Aislinn": {28        "hips": ((0, 0, 0.88), (0, 0, 1.04)),29        "spine": ((0, 0, 1.04), (0, 0, 1.22)),30        "chest": ((0, 0, 1.22), (0, 0, 1.34)),31        "neck": ((0, 0, 1.34), (0, 0, 1.4)),32        "head": ((0, 0, 1.4), (0, 0, 1.72)),33        "upper_arm.L": ((0.195, 0, 1.245), (0.29, -0.08, 1.055)),34        "forearm.L": ((0.29, -0.08, 1.055), (0.07, -0.3, 1.15)),35        "hand.L": ((0.07, -0.3, 1.15), (0.07, -0.37, 1.15)),36        "upper_arm.R": ((-0.195, 0, 1.245), (-0.29, -0.08, 1.055)),37        "forearm.R": ((-0.29, -0.08, 1.055), (-0.07, -0.3, 1.15)),38        "hand.R": ((-0.07, -0.3, 1.15), (-0.07, -0.37, 1.15)),39        "thigh.L": ((-0.072, -0.005, 0.88), (-0.076, -0.016, 0.46)),40        "calf.L": ((-0.076, -0.016, 0.46), (-0.076, -0.03, 0.1)),41        "foot.L": ((-0.076, -0.03, 0.1), (-0.076, -0.15, 0.03)),42        "thigh.R": ((0.072, -0.005, 0.88), (0.076, -0.016, 0.46)),43        "calf.R": ((0.076, -0.016, 0.46), (0.076, -0.03, 0.1)),44        "foot.R": ((0.076, -0.03, 0.1), (0.076, -0.15, 0.03)),45    },46    "Finella": {47        "hips": ((0, 0, 0.86), (0, 0, 1.02)),48        "spine": ((0, 0, 1.02), (0, 0, 1.2)),49        "chest": ((0, 0, 1.2), (0, 0, 1.345)),50        "neck": ((0, 0, 1.345), (0, 0, 1.41)),51        "head": ((0, 0, 1.41), (0, 0, 1.74)),52        "upper_arm.R": ((-0.2, -0.01, 1.25), (-0.32, -0.15, 1.08)),53        "forearm.R": ((-0.32, -0.15, 1.08), (-0.3, -0.36, 1.04)),54        "hand.R": ((-0.3, -0.36, 1.04), (-0.3, -0.42, 1.03)),55        "upper_arm.L": ((0.2, 0, 1.25), (0.33, 0.06, 1.03)),56        "forearm.L": ((0.33, 0.06, 1.03), (0.275, -0.075, 0.885)),57        "hand.L": ((0.275, -0.075, 0.885), (0.27, -0.13, 0.88)),58        "thigh.R": ((0.082, 0, 0.86), (0.092, -0.11, 0.48)),59        "calf.R": ((0.092, -0.11, 0.48), (0.104, -0.2, 0.1)),60        "foot.R": ((0.104, -0.2, 0.1), (0.104, -0.32, 0.04)),61        "thigh.L": ((-0.082, 0.01, 0.86), (-0.1, 0.09, 0.48)),62        "calf.L": ((-0.1, 0.09, 0.48), (-0.112, 0.18, 0.09)),63        "foot.L": ((-0.112, 0.18, 0.09), (-0.112, 0.06, 0.03)),64    },65    "Atlanta": {66        "hips": ((0, 0, 0.9), (0, 0, 1.08)),67        "spine": ((0, 0, 1.08), (0, 0, 1.26)),68        "chest": ((0, 0, 1.26), (0, 0, 1.41)),69        "neck": ((0, 0, 1.41), (0, 0, 1.47)),70        "head": ((0, 0, 1.47), (0, 0, 1.8)),71        "upper_arm.L": ((0.225, 0, 1.33), (0.36, -0.11, 1.16)),72        "forearm.L": ((0.36, -0.11, 1.16), (0.483, -0.278, 1.02)),73        "hand.L": ((0.483, -0.278, 1.02), (0.49, -0.34, 1.02)),74        "upper_arm.R": ((-0.225, 0, 1.33), (-0.285, 0.01, 1.1)),75        "forearm.R": ((-0.285, 0.01, 1.1), (-0.235, -0.09, 0.93)),76        "hand.R": ((-0.235, -0.09, 0.93), (-0.235, -0.15, 0.92)),77        "thigh.R": ((0.086, 0, 0.9), (0.096, -0.09, 0.49)),78        "calf.R": ((0.096, -0.09, 0.49), (0.108, -0.15, 0.1)),79        "foot.R": ((0.108, -0.15, 0.1), (0.108, -0.27, 0.04)),80        "thigh.L": ((-0.086, 0.01, 0.9), (-0.1, 0.03, 0.49)),81        "calf.L": ((-0.1, 0.03, 0.49), (-0.112, 0.06, 0.1)),82        "foot.L": ((-0.112, 0.06, 0.1), (-0.112, -0.06, 0.04)),83    },84}8586# Head wear and facial detail follow the head; shoulders, hands and quivers do not.87HEAD_RE = re.compile(88    r"(Head|Hair|Brow|Eye|Iris|Sclera|Pupil|Lash|Lip|Chin|Ear|Jaw|Cheek|Nose|Bang|Braid|Crown|Antler|Circlet|Stag|Face|Freckle|Catchlight|Hood|Back Lock|Loose Strand)",89    re.I,90)91HEAD_SKIP_RE = re.compile(r"(Arrow|Quiver|Hand|Neck|Neckline|Mantle|Shoulder|Sash)", re.I)929394def is_head(name):95    return bool(HEAD_RE.search(name)) and not HEAD_SKIP_RE.search(name)969798def side_of(name, default="L"):99    match = re.search(r"\b([LR])\b", name)100    return match.group(1) if match else default101102103def bone_for(name, character):104    lower = name.lower()105    if "leading dagger" in lower:106        return "hand.R"107    if "reverse dagger" in lower:108        return "hand.L"109    if "hand arrow" in lower:110        return "hand.R"111    if "quiver" in lower:112        return "chest"113    if "recurve bow" in lower or "bowstring" in lower or "bow grip" in lower or "bow tip" in lower or "bow charm" in lower:114        return "hand.L"115    if is_head(name):116        return "head"117    if re.search(r"\bneck\b", lower):118        return "neck"119    side = side_of(name, "L")120    if "upper arm" in lower or ("sleeve" in lower and "tail" not in lower and "drape" not in lower):121        return f"upper_arm.{side}"122    if "forearm" in lower or "bracer" in lower or "sleeve tail" in lower or "drape" in lower or " arm" in lower:123        return f"forearm.{side}"124    if "hand" in lower:125        return f"hand.{side}"126    if "thigh" in lower:127        return f"thigh.{side}"128    if "calf" in lower or "knee guard" in lower or "boot shaft" in lower or "boot cuff" in lower:129        return f"calf.{side}"130    if "foot" in lower or "toe" in lower or "boot lace" in lower or lower.endswith(" boot") or " boot " in lower:131        return f"foot.{side}"132    if any(word in lower for word in ("belt", "buckle", "pouch", "sheath", "hip knife", "skirt", "sash", "dress")):133        return "hips"134    if any(word in lower for word in ("cloak", "cape", "cowl", "bodice", "bust", "tunic", "neckline", "baldric", "mantle", "clasp", "underbust")):135        return "chest"136    if any(word in lower for word in ("water", "mist", "mote", "fog", "blood")):137        return "chest"138    return "chest"139140141def shrink_head(parts, neck_z, scale=0.82):142    """Scale head/hair vertices around the neck, leaving the body untouched."""143    pivot = Vector((0, 0, neck_z))144    for obj in parts:145        if obj.type != "MESH" or not is_head(obj.name):146            continue147        matrix = obj.matrix_world.copy()148        inverse = matrix.inverted()149        for vertex in obj.data.vertices:150            world = matrix @ vertex.co151            vertex.co = inverse @ (pivot + (world - pivot) * scale)152        obj.data.update()153154155def create_armature(character):156    anatomy = ANATOMY[character]157    data = bpy.data.armatures.new(f"{character} Armature")158    armature = bpy.data.objects.new(f"{character} Armature", data)159    bpy.context.collection.objects.link(armature)160    bpy.context.view_layer.objects.active = armature161    bpy.ops.object.mode_set(mode="EDIT")162163    parents = {164        "hips": "root",165        "spine": "hips",166        "chest": "spine",167        "neck": "chest",168        "head": "neck",169        "upper_arm.L": "chest",170        "forearm.L": "upper_arm.L",171        "hand.L": "forearm.L",172        "upper_arm.R": "chest",173        "forearm.R": "upper_arm.R",174        "hand.R": "forearm.R",175        "thigh.L": "hips",176        "calf.L": "thigh.L",177        "foot.L": "calf.L",178        "thigh.R": "hips",179        "calf.R": "thigh.R",180        "foot.R": "calf.R",181    }182183    root = data.edit_bones.new("root")184    root.head = (0, 0, 0)185    root.tail = (0, 0, 0.2)186    for name, (head, tail) in anatomy.items():187        bone = data.edit_bones.new(name)188        bone.head = head189        bone.tail = tail190        parent = parents.get(name)191        if parent:192            bone.parent = data.edit_bones[parent]193194    bpy.ops.object.mode_set(mode="OBJECT")195    return armature196197198def parent_parts(root, armature):199    for part in list(root.children):200        if part.type != "MESH":201            continue202        world = part.matrix_world.copy()203        bone = bone_for(part.name, root.name)204        part.parent = armature205        part.parent_type = "BONE"206        part.parent_bone = bone207        part.matrix_world = world208209210def key_pose(armature, action, frame, pose):211    armature.animation_data.action = action212    bpy.context.scene.frame_set(frame)213    for name, rotation in pose.items():214        bone = armature.pose.bones.get(name)215        if not bone:216            continue217        bone.rotation_mode = "XYZ"218        bone.rotation_euler = rotation219        bone.keyframe_insert("rotation_euler", frame=frame)220221222def make_action(armature, name, frames):223    action = bpy.data.actions.new(name)224    action.use_fake_user = True225    for frame, pose in frames:226        key_pose(armature, action, frame, pose)227    return action228229230def animate(armature, character):231    armature.animation_data_create()232    bpy.context.scene.render.fps = 24233234    idle_pose = {235        "chest": (0, 0, 0),236        "neck": (0, 0, 0),237        "head": (0, 0, 0),238        "upper_arm.L": (0, 0, 0),239        "upper_arm.R": (0, 0, 0),240    }241    idle_mid = {242        "chest": (0.025, 0, 0.02),243        "neck": (-0.012, 0, -0.01),244        "head": (0.015, 0, -0.03),245        "upper_arm.L": (0.03, 0, 0.025),246        "upper_arm.R": (-0.028, 0, -0.02),247    }248    make_action(armature, "Idle", [(1, idle_pose), (30, idle_mid), (60, idle_pose)])249250    walk_a = {251        "thigh.L": (0.32, 0, 0),252        "calf.L": (-0.22, 0, 0),253        "thigh.R": (-0.32, 0, 0),254        "calf.R": (0.22, 0, 0),255        "upper_arm.L": (0.12, 0, 0),256        "upper_arm.R": (-0.12, 0, 0),257        "chest": (0, 0, 0.03),258        "head": (0, 0, -0.03),259    }260    walk_b = {261        "thigh.L": (-0.32, 0, 0),262        "calf.L": (0.22, 0, 0),263        "thigh.R": (0.32, 0, 0),264        "calf.R": (-0.22, 0, 0),265        "upper_arm.L": (-0.12, 0, 0),266        "upper_arm.R": (0.12, 0, 0),267        "chest": (0, 0, -0.03),268        "head": (0, 0, 0.03),269    }270    make_action(armature, "Walk", [(1, walk_a), (8, walk_b), (16, walk_a)])271272    if character == "Finella":273        attack = {274            "chest": (-0.08, 0, -0.1),275            "head": (-0.04, 0, -0.08),276            "upper_arm.R": (-0.85, 0.25, 0.1),277            "forearm.R": (-0.45, 0, 0),278            "upper_arm.L": (0.1, 0, 0),279        }280    elif character == "Atlanta":281        attack = {282            "chest": (-0.06, 0.12, 0),283            "head": (-0.03, 0.08, 0),284            "upper_arm.L": (-0.38, 0.1, 0),285            "forearm.L": (-0.22, 0, 0),286            "upper_arm.R": (0.05, 0, 0),287        }288    else:289        attack = {290            "chest": (-0.1, 0, 0),291            "head": (-0.05, 0, 0),292            "upper_arm.L": (-0.6, 0, 0.1),293            "forearm.L": (-0.35, 0, 0),294            "upper_arm.R": (-0.6, 0, -0.1),295            "forearm.R": (-0.35, 0, 0),296        }297    rest = {name: (0, 0, 0) for name in attack}298    make_action(armature, "Attack", [(1, rest), (7, attack), (16, rest)])299300    hit = {301        "chest": (0.14, 0, 0),302        "neck": (0.1, 0, 0),303        "head": (0.12, 0, 0),304        "upper_arm.L": (0.12, 0, 0),305        "upper_arm.R": (0.12, 0, 0),306    }307    hit_rest = {name: (0, 0, 0) for name in hit}308    make_action(armature, "Hit", [(1, hit_rest), (4, hit), (12, hit_rest)])309310311def rig_character(root, character):312    anatomy = ANATOMY[character]313    neck_z = anatomy["neck"][1][2]314    shrink_head(list(root.children), neck_z)315    armature = create_armature(character)316    parent_parts(root, armature)317    armature.parent = root318    animate(armature, character)319    return armature