"""Pose the user's gloved FPS arms naturally on the V4 sniper rifle (right-handed shooter). Run inside Blender (scene "Reference Sculpt V4"), e.g. via the Blender MCP: exec(open(r'C:\\roblox\\hunter\\blender\\pose_hands_natural.py', encoding='utf-8').read()) Rifle space: barrel points +X, up is +Z, shooter's right is -Y. Hand bone axes (both hands): Y = wrist -> fingers, +Z = index side. Palm faces -X on the right hand and +X on the left hand, so fingers curl with +Z rotation on the right hand and -Z rotation on the left. """ import math import bpy from mathutils import Euler, Matrix, Vector # ---------------------------------------------------------------- parameters ARM_SCALE = 1.4 # hand length ~19% of the rifle, close to real proportions SHOULDERS = Vector((-4.22, 0.92, 0.3)) # centre between the shoulders: rifle height, right shoulder behind the stock BLADE_DEG = 40 # bladed stance: left shoulder forward so the support arm reaches the handguard EYE_PITCH_DEG = -9 # first-person camera looks slightly down EYE_YAW_DEG = -5 # and slightly right, toward the rifle EYE = Vector((-5.6, 1.4, 1.2)) # first-person camera: ~0.9 above the shoulders, left of the rifle # Measured with ray casts: grip front edge at z=-0.7 is x=-2.66, sides at y=+-0.257, # the grip leans along GRIP_AXIS. Handguard bottom z=-0.2, right face y=-0.41..-0.43. GRIP_AXIS = Vector((0.33, 0.0, 0.94)) # grip leans back at the bottom RIGHT_KNUCKLE = Vector((-2.58, -0.40, -0.72)) # middle-finger knuckle just off the grip's front-right edge LEFT_KNUCKLE = Vector((0.60, -0.45, -0.33)) # middle-finger knuckle at the handguard's bottom-right edge LEFT_PALM_NORMAL = Vector((0.0, 0.25, 0.97)) # left palm faces up (slightly left), under the handguard RIGHT_HAND_TILT = (0.0, 0.0, 0.0) # extra hand rotation, degrees about world X/Y/Z (set by fit_grip.py) LEFT_HAND_TILT = (0.0, 0.0, 0.0) RIGHT_ELBOW_POLE = Vector((-0.3, -2.0, -0.6)) # right elbow out to the side, slightly down LEFT_ELBOW_POLE = Vector((0.0, 0.9, -2.5)) # left elbow low and left, forearm rises to the handguard LEFT_FINGER_ANGLE_DEG = 35 # 90 = fingers straight across to the right side, 0 = straight forward # degrees per joint, base -> tip. Right fingers: knuckle bends ~90 to run across the # grip's front face, then a second ~90 bend brings them back along its left side. RIGHT_CURL = { 'index': (8, 0, 5, 5), # straight, along the right side of the trigger guard 'middle': (85, 5, 85, 20), 'ring': (85, 5, 85, 20), 'pink': (85, 10, 80, 20), 'thumb': (75, -40, -30), # base swings behind the grip (through the thumbhole), tip turns forward on the left side } RIGHT_INDEX_LIFT_DEG = 22 # raise the index from the grip angle to horizontal # Left fingers: knuckle bends up so the fingers lie flat on the handguard's right face. LEFT_CURL = { 'index': (40, 35, 25, 10), # relaxed wrap: fingers rise along the right face, tips curl in 'middle': (45, 35, 25, 10), 'ring': (50, 35, 25, 10), 'pink': (55, 35, 25, 10), 'thumb': (15, 0, 0), # along the handguard's left side, pointing forward } # ---------------------------------------------------------------------------- scene = bpy.data.scenes['Reference Sculpt V4'] rig = bpy.data.objects['Armature'] placement = rig.parent def update(): bpy.context.view_layer.update() def place_rig(): placement.scale = (0.16 * ARM_SCALE,) * 3 placement.rotation_euler = (0, 0, math.radians(90 - BLADE_DEG)) placement.location = (SHOULDERS.x, SHOULDERS.y, SHOULDERS.z - 4.665 * ARM_SCALE) rig.animation_data_clear() update() def set_world(name, pos, x=None, y=None, z=None): """Put a pose bone at a world position (and world orientation if axes are given).""" mw = rig.matrix_world pb = rig.pose.bones[name] for c in pb.constraints: c.mute = True # Child Of to the old controller fights manual placement if x is not None: rot = mw.to_3x3().normalized().inverted() @ Matrix((x.normalized(), y.normalized(), z.normalized())).transposed() m = rot.to_4x4() else: m = pb.matrix.to_3x3().normalized().to_4x4() m.translation = mw.inverted() @ pos pb.matrix = m update() def curl(side, sign, table): for finger, angles in table.items(): if finger == 'thumb': chain = [f'thumb_L.{side}', f'thumb_L.002.{side}', f'thumb_L.001.{side}'] else: chain = [f'{finger}_L.{side}', f'{finger}_L.003.{side}', f'{finger}_L.002.{side}', f'{finger}_L.001.{side}'] for bone, deg in zip(chain, angles): pb = rig.pose.bones.get(bone) if pb: pb.rotation_mode = 'XYZ' pb.rotation_euler = (0, 0, sign * math.radians(deg)) def knuckle_offset(side): """Wrist -> middle-finger knuckle, in world units, measured from the rig.""" b = rig.data.bones local = b[f'hand_L.{side}'].matrix_local.inverted() @ b[f'middle_L.{side}'].head_local return local * rig.matrix_world.to_scale().x def place_hand(side, knuckle, x, y, z): """Place the hand so its middle knuckle lands on `knuckle` with hand axes x/y/z.""" k = knuckle_offset(side) wrist = knuckle - (k.x * x + k.y * y + k.z * z) set_world(f'hand_IK_L.{side}', wrist, x, y, z) return wrist def pose_hands(): # Right hand: back of the hand on the grip's right side, knuckle line along the grip axis, # knuckles at the front edge so the fingers wrap across the front strap to the left side. zr = GRIP_AXIS.normalized() xr = Vector((0, -1, 0)) # palm (-X) faces +Y, into the grip yr = zr.cross(xr) wrist_r = place_hand('R', RIGHT_KNUCKLE, xr, yr, zr) # Left hand: palm up under the handguard, knuckles at its bottom-right edge, # fingers bending up flat against the right face. xl = LEFT_PALM_NORMAL.normalized() a = math.radians(LEFT_FINGER_ANGLE_DEG) yl = (math.cos(a) * Vector((1, 0, 0)) + math.sin(a) * Vector((0, -1, 0))) yl = (yl - xl * yl.dot(xl)).normalized() zl = xl.cross(yl) # index side points forward wrist_l = place_hand('L', LEFT_KNUCKLE, xl, yl, zl) mw = rig.matrix_world sr = mw @ rig.pose.bones['upperarm_L.R'].head sl = mw @ rig.pose.bones['upperarm_L.L'].head set_world('elbow_IK_L.R', (sr + wrist_r) / 2 + RIGHT_ELBOW_POLE) # right elbow out and down set_world('elbow_IK_L.L', (sl + wrist_l) / 2 + LEFT_ELBOW_POLE) # left elbow under the rifle curl('R', +1, RIGHT_CURL) curl('L', -1, LEFT_CURL) rig.pose.bones['index_L.R'].rotation_euler.x = math.radians(RIGHT_INDEX_LIFT_DEG) # +X bends toward the index side (up) update() return wrist_r, wrist_l def place_camera(): eye = EYE.copy() cam = scene.camera cam.location = eye cam.rotation_mode = 'XYZ' cam.rotation_euler = Euler((math.radians(90 + EYE_PITCH_DEG), 0, math.radians(-90 + EYE_YAW_DEG)), 'XYZ') update() return eye def report(wrist_r, wrist_l): mw = rig.matrix_world hand_r = mw @ rig.pose.bones['hand_L.R'].head hand_l = mw @ rig.pose.bones['hand_L.L'].head return { 'right_hand_error': round((hand_r - wrist_r).length, 3), # > 0 means the arm can't reach 'left_hand_error': round((hand_l - wrist_l).length, 3), } place_rig() _wr, _wl = pose_hands() _eye = place_camera() POSE_REPORT = report(_wr, _wl) POSE_REPORT['eye'] = [round(v, 3) for v in _eye] print(POSE_REPORT)