Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 217 additions & 7 deletions neo/modelsrc/weapons/README.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions neo/modelsrc/weapons/compile_viewmodels.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Compile exported viewmodel SMDs using Valve's Source SDK 2013 studiomdl.

Run with Python 3: compile_viewmodels.py SOURCE [SOURCE ...] --compiler PATH --game PATH
Run with Python 3: compile_viewmodels.py PROJECT [PROJECT ...] --compiler PATH --game PATH

Each SOURCE is a folder beside this script that contains one .qc file.
Each PROJECT is a folder beside this script that contains one .qc file (it may $include a shared GUN/viewmodel.qci).
The game path must contain gameinfo.txt.
Compilation uses a temporary game configuration because SDK tools cannot resolve the client's appid search path.
The resulting model files are copied to the chosen destination, while gameinfo.txt is not changed
Expand All @@ -17,8 +17,8 @@
def main():
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('sources', nargs='+', metavar='SOURCE',
help='Source folder beside this script, such as jinrai_tachi')
parser.add_argument('sources', nargs='+', metavar='PROJECT',
help='Project folder beside this script, such as jinrai_tachi')
parser.add_argument('--compiler', type=Path, required=True,
help='Path to studiomdl.exe in the Source SDK Base 2013 Multiplayer bin folder')
parser.add_argument('--game', type=Path, required=True,
Expand All @@ -35,7 +35,7 @@ def main():
for source in args.sources:
found = sorted((root / source).glob('*.qc'))
if len(found) != 1:
parser.error('Source folder must contain one .qc file: ' + source)
parser.error('Project folder must contain one .qc file: ' + source)
qcs.append(found[0])
with tempfile.TemporaryDirectory(prefix='neo-viewmodel-') as temporary:
staging = Path(temporary)
Expand Down
358 changes: 337 additions & 21 deletions neo/modelsrc/weapons/export_viewmodels.py

Large diffs are not rendered by default.

Binary file not shown.
75 changes: 1 addition & 74 deletions neo/modelsrc/weapons/jinrai_tachi/v_jinrai_tachi.qc
Original file line number Diff line number Diff line change
@@ -1,75 +1,2 @@
$modelname "weapons/v_jinrai_tachi.mdl"
$cdmaterials "models/Weapons/"
$body "body" "reference.smd"
$definebone "Dummy01" "" -2.277793 -6.712466 -19.960323 -0.000000 89.999978 0.000000 0 0 0 0 0 0
$definebone "bone forearm01" "Dummy01" -6.973125 11.498295 -10.988396 -31.114590 90.939514 89.978900 0 0 0 0 0 0
$definebone "bone hand01" "bone forearm01" -0.035541 -23.756721 -0.084862 -0.042628 -1.058999 -0.805063 0 0 0 0 0 0
$definebone "bone middle 05" "bone hand01" 0.273360 -3.765809 -0.387136 -0.000000 -0.000001 -0.000000 0 0 0 0 0 0
$definebone "bone middle 06" "bone middle 05" 0.000002 -3.784243 -0.191475 -10.216125 -0.000002 -0.116826 0 0 0 0 0 0
$definebone "bone middle 07" "bone middle 06" 0.024372 -3.235442 0.175100 -0.154929 0.133554 -12.925470 0 0 0 0 0 0
$definebone "bone middle 08" "bone middle 07" -0.111963 -3.553718 0.074803 4.989030 0.902865 -18.339691 0 0 0 0 0 0
$definebone "bone index 05" "bone hand01" 1.858448 -3.773904 0.198660 -14.044973 4.338951 -0.000000 0 0 0 0 0 0
$definebone "bone index 06" "bone index 05" 0.000004 -3.784240 -0.191475 5.207097 -1.406557 -6.343559 0 0 0 0 0 0
$definebone "bone index 07" "bone index 06" 0.074586 -3.334557 0.036040 -0.154928 0.133556 -12.948845 0 0 0 0 0 0
$definebone "bone index 08" "bone index 07" -0.033768 -2.966134 0.050352 1.496623 2.758618 -18.165053 0 0 0 0 0 0
$definebone "bone ring 05" "bone hand01" -1.407701 -3.765813 -0.387136 0.000000 -5.445049 0.000000 0 0 0 0 0 0
$definebone "bone ring 06" "bone ring 05" 0.000002 -3.784244 -0.191475 -7.169937 1.442592 -0.480196 0 0 0 0 0 0
$definebone "bone ring 07" "bone ring 06" 0.038987 -2.749642 0.107386 -1.660432 -2.201248 -12.888514 0 0 0 0 0 0
$definebone "bone ring 08" "bone ring 07" 0.013190 -3.474698 0.024520 1.138697 2.167728 -31.676656 0 0 0 0 0 0
$definebone "bone pinky 05" "bone hand01" -2.992724 -3.619925 0.091621 -0.000000 -9.635662 -0.000000 0 0 0 0 0 0
$definebone "bone pinky 06" "bone pinky 05" -0.231696 -2.899982 -0.226467 -7.169938 1.442592 -2.020135 0 0 0 0 0 0
$definebone "bone pinky 07" "bone pinky 06" 0.047093 -3.464708 0.060474 0.392362 0.981704 -12.970447 0 0 0 0 0 0
$definebone "bone pinky 08" "bone pinky 07" -0.012931 -2.746934 0.096229 -4.151539 3.541852 -14.719691 0 0 0 0 0 0
$definebone "bone thumb 04" "bone hand01" 2.231814 -1.416993 1.590842 -38.208904 65.612956 -60.895681 0 0 0 0 0 0
$definebone "bone thumb 05" "bone thumb 04" 0.316679 -3.892339 -0.185575 -33.168527 -11.613910 -10.910492 0 0 0 0 0 0
$definebone "bone thumb 06" "bone thumb 05" 0.269053 -3.933444 0.328680 5.133243 -12.418461 -13.006143 0 0 0 0 0 0
$definebone "bone forearm" "Dummy01" 12.577013 11.498295 1.716358 31.114527 -89.060440 -89.978894 0 0 0 0 0 0
$definebone "bone hand" "bone forearm" 0.035544 23.756721 -0.084859 -14.678660 2.606786 -11.549912 0 0 0 0 0 0
$definebone "bone middle 1" "bone hand" -0.273364 3.765811 0.387137 0.000003 0.000001 0.000002 0 0 0 0 0 0
$definebone "bone middle 2" "bone middle 1" -0.000001 3.784237 0.191473 -8.198563 3.499229 -25.560163 0 0 0 0 0 0
$definebone "bone middle 3" "bone middle 2" -0.024372 3.235442 -0.175100 -0.154925 0.133562 -71.831997 0 0 0 0 0 0
$definebone "bone middle 4" "bone middle 3" 0.111963 3.553717 -0.074805 4.989024 0.902872 -78.199574 0 0 0 0 0 0
$definebone "bone index 1" "bone hand" -1.858449 3.773898 -0.198660 -14.044967 4.338953 0.000002 0 0 0 0 0 0
$definebone "bone index 2" "bone index 1" -0.000001 3.784236 0.191477 5.346332 -0.276393 -7.004521 0 0 0 0 0 0
$definebone "bone index 3" "bone index 2" -0.074585 3.334558 -0.036042 -0.154921 0.133558 -12.832018 0 0 0 0 0 0
$definebone "bone index 4" "bone index 3" 0.033765 2.966134 -0.050352 1.496628 2.758624 -49.071600 0 0 0 0 0 0
$definebone "bone ring 1" "bone hand" 1.407699 3.765804 0.387136 0.000003 -5.445046 0.000002 0 0 0 0 0 0
$definebone "bone ring 2" "bone ring 1" -0.000003 3.784241 0.191476 -6.664011 2.311486 -40.444693 0 0 0 0 0 0
$definebone "bone ring 3" "bone ring 2" -0.038986 2.749646 -0.107382 -1.660430 -2.201242 -72.795031 0 0 0 0 0 0
$definebone "bone ring 4" "bone ring 3" -0.013189 3.474696 -0.024517 1.138692 2.167730 -81.536477 0 0 0 0 0 0
$definebone "bone pinky 1" "bone hand" 2.992723 3.619924 -0.091623 0.000004 -9.635659 0.000002 0 0 0 0 0 0
$definebone "bone pinky 2" "bone pinky 1" 0.231693 2.899976 0.226466 -7.169932 1.442595 -41.833261 0 0 0 0 0 0
$definebone "bone pinky 3" "bone pinky 2" -0.047095 3.464705 -0.060474 0.392365 0.981709 -77.830297 0 0 0 0 0 0
$definebone "bone pinky 4" "bone pinky 3" 0.012933 2.746925 -0.096229 -4.151539 3.541855 -65.579491 0 0 0 0 0 0
$definebone "bone thumb 1" "bone hand" -2.231816 1.416988 -1.590842 -46.604322 61.384001 -55.069086 0 0 0 0 0 0
$definebone "bone thumb 2" "bone thumb 1" -0.316676 3.892338 0.185578 -33.168533 -11.613905 -33.910502 0 0 0 0 0 0
$definebone "bone thumb 3" "bone thumb 2" -0.269053 3.933444 -0.328678 5.133236 -12.418460 -66.142942 0 0 0 0 0 0
$definebone "magrel" "bone hand" -1.389966 15.611851 -2.785499 87.000140 -92.999972 90.000075 0 0 0 0 0 0
$definebone "slide" "magrel" 0.000001 1.764277 6.216676 0.000004 0.000004 0.000001 0 0 0 0 0 0
$definebone "Eject" "slide" -0.120959 -0.439801 -0.840956 -10.016456 178.846850 0.201737 0 0 0 0 0 0
$definebone "body" "magrel" 0.000000 -0.000001 -0.000000 0.000004 0.000004 0.000001 0 0 0 0 0 0
$definebone "catch2" "body" 0.000001 0.367147 3.416009 0.000004 0.000004 0.000001 0 0 0 0 0 0
$definebone "bullet_eject01" "body" -0.000000 4.833648 3.431723 0.000004 0.000004 0.000001 0 0 0 0 0 0
$definebone "trigger" "magrel" 0.000000 1.612491 2.633620 0.000004 0.000004 0.000001 0 0 0 0 0 0
$definebone "mag" "magrel" -0.059484 7.270134 1.720140 0.000004 0.000004 7.999981 0 0 0 0 0 0
$definebone "bullet2" "mag" 0.059484 -7.270135 -1.720142 0.000004 0.000003 0.000002 0 0 0 0 0 0
$definebone "Main" "magrel" -0.000622 3.390026 5.182981 -1.337800 -89.989460 89.981534 0 0 0 0 0 0
$definebone "Muzzle" "Main" 16.557522 -0.000008 -0.000003 -0.000004 0.041881 0.000005 0 0 0 0 0 0
$attachment "muzzle" "Muzzle" 0.0000 -0.0000 0.0000 rotate 0.000 -0.000 -0.000
$attachment "eject" "Eject" 0.0000 -0.0000 0.0000 rotate -0.000 -0.000 0.000
$sequence "idle01" "anims/idle01.smd" activity ACT_VM_IDLE 1 fps 30 fadein 0.20 fadeout 0.20 loop node "0"
$sequence "idle01empty" "anims/idle01empty.smd" activity ACT_VM_IDLE_EMPTY 1 fps 30 fadein 0.20 fadeout 0.20 loop node "0"
$sequence "fire" "anims/fire.smd" activity ACT_VM_PRIMARYATTACK 1 fps 30 fadein 0.20 fadeout 0.20 snap node "2" { event AE_MUZZLEFLASH 0 "PISTOL MUZZLE" }
$sequence "fire1" "anims/fire1.smd" activity ACT_VM_RECOIL1 1 fps 30 fadein 0.20 fadeout 0.20 snap node "2" { event AE_MUZZLEFLASH 0 "PISTOL MUZZLE" }
$sequence "fire2" "anims/fire2.smd" activity ACT_VM_RECOIL2 1 fps 30 fadein 0.20 fadeout 0.20 snap node "2" { event AE_MUZZLEFLASH 0 "PISTOL MUZZLE" }
$sequence "fire3" "anims/fire3.smd" activity ACT_VM_RECOIL3 1 fps 30 fadein 0.20 fadeout 0.20 snap node "2" { event AE_MUZZLEFLASH 0 "PISTOL MUZZLE" }
$sequence "fireempty" "anims/fireempty.smd" activity ACT_VM_DRYFIRE 1 fps 30 fadein 0.20 fadeout 0.20 node "2"
$sequence "draw" "anims/draw.smd" activity ACT_VM_DRAW 1 fps 30 fadein 0.20 fadeout 0.20 snap node "0"
$sequence "drawempty" "anims/drawempty.smd" fps 30 fadein 0.20 fadeout 0.20 node "0"
$sequence "reload" "anims/reload.smd" activity ACT_VM_RELOAD 1 fps 30 fadein 0.20 fadeout 0.20 node "0"
$sequence "holster" "anims/holster.smd" activity ACT_VM_HOLSTER 1 fps 30 fadein 0.20 fadeout 0.20 node "0"
$sequence "holsterempty" "anims/holsterempty.smd" activity ACT_VM_HOLSTER 1 fps 30 fadein 0.20 fadeout 0.20 node "0"
$sequence "idletolow" "anims/idletolow.smd" activity ACT_VM_LOWERED_TO_IDLE 1 fps 30 fadein 0.20 fadeout 0.20 transition "0" "1"
$sequence "lowtoidle" "anims/lowtoidle.smd" activity ACT_VM_IDLE_TO_LOWERED 1 fps 30 fadein 0.20 fadeout 0.20 transition "1" "0"
$sequence "lowidle" "anims/lowidle.smd" activity ACT_VM_IDLE_LOWERED 1 fps 30 fadein 0.20 fadeout 0.20 loop node "1"
$sequence "reloadempty" "anims/reloadempty.smd" activity ACT_VM_RELOAD_EMPTY 1 fps 30 fadein 0.20 fadeout 0.20 node "0"
$include "../tachi/viewmodel.qci"
187 changes: 187 additions & 0 deletions neo/modelsrc/weapons/merge_teams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
"""Merge the two team scenes of each gun into one: one rig and one action, both teams' arms.

Run from this folder (Blender 5.2.3 or newer, Blender Source Tools 3.4.3 or newer):
blender -b viewmodels.blend --python-exit-code 1 --python merge_teams.py -- [GUN ...] [--dry-run]
Without GUN, every gun that still has an nsf_GUN scene. --dry-run checks and reports but does not save.

Per gun, the jinrai_GUN scene keeps its rig and action. The NSF preview mesh (arms and gun) moves into that scene,
parented and skinned to the kept rig (its Armature modifier points at the kept rig; the vertex groups keep their bone
names). Each team's preview mesh stays in its own collection, jinrai_GUN_preview and nsf_GUN_preview: the team
toggle is that collection's visibility (Outliner eye, or Team in the Viewmodel panel). The NSF collection starts
hidden. Then the nsf_GUN scene, rig, armature, action and camera are deleted. The NSF project folder is not touched:
it compiles from its shipped SMDs on disk.

Preview-only re-bind: where a bone's rest pose differs between the two rigs (the SRS Release bone, 22 deg; the MilSO
arm and slide bones, 0.006 u), the NSF vertices weighted to it are moved by the rest difference, so the kept rig poses
them as the NSF rig did.
reference.smd is never written; the compiled NSF model keeps its own rest pose.

Gate: for every shipped slot of the NSF scene, at 4 frames, the NSF arms posed by the kept rig must match the NSF arms
posed by the NSF rig within ARMS_TOLERANCE. Any gun over it fails the run before anything is saved.
Runtime: ~1 min for all 19 guns.
"""
from pathlib import Path
import sys

import bpy
import numpy as np

sys.dont_write_bytecode = True
sys.path.insert(0, str(Path(__file__).resolve().parent))
import rom_limits

ARMS_TOLERANCE = 0.03 # units: well above the team clip differences (M41 0.026 u at the muzzle), far below a visible pose
REST_TOLERANCE = 1e-3 # units or matrix entries: a smaller rest difference needs no re-bind
ARMS_MATERIAL = 'armsheet' # the arm materials (jinrai_armsheet1, nsf_armsheet2.004, ...)
TEAM_KEY = 'team' # custom property on each team collection and preview mesh


def slot_frames(rig):
"""{slot name: 4 frames spread over the slot's keys}."""
frames = {}
for slot, _, first, last in rom_limits.slot_bags(rig):
frames[slot.name_display] = sorted({first, first + (last - first) // 3, first + 2 * (last - first) // 3, last})
return frames


def posed(scene, mesh, rig, slot, frame):
"""World positions [n, 3] of the evaluated mesh with the rig on slot at frame."""
rig.animation_data.action_slot = next(s for s in rig.animation_data.action.slots if s.name_display == slot)
scene.frame_set(frame)
depsgraph = scene.view_layers[0].depsgraph
depsgraph.update()
evaluated = mesh.evaluated_get(depsgraph)
data = evaluated.data
co = np.empty(len(data.vertices) * 3)
data.vertices.foreach_get('co', co)
co = co.reshape(-1, 3)
matrix = np.array(evaluated.matrix_world)
return co @ matrix[:3, :3].T + matrix[:3, 3]


def arm_vertices(mesh):
"""Indices of the vertices on arm-material faces."""
arms = {index for index, slot in enumerate(mesh.material_slots)
if slot.material and ARMS_MATERIAL in slot.material.name}
return sorted({v for polygon in mesh.data.polygons if polygon.material_index in arms for v in polygon.vertices})


def rebind(mesh, kept, old):
"""Move the vertices weighted to bones whose rest differs between the rigs by that difference (weighted blend);
return the bones whose vertices moved."""
to_rig = old.matrix_world.inverted() @ mesh.matrix_world
moved = {}
for group in mesh.vertex_groups:
bone_old, bone_kept = old.data.bones.get(group.name), kept.data.bones.get(group.name)
if bone_old is None or bone_kept is None:
continue
if np.abs(np.array(bone_old.matrix_local) - np.array(bone_kept.matrix_local)).max() < REST_TOLERANCE:
continue
moved[group.index] = (group.name, bone_kept.matrix_local @ bone_old.matrix_local.inverted())
if not moved:
return []
from_rig, used = to_rig.inverted(), set()
for vertex in mesh.data.vertices:
links = [(g.group, g.weight) for g in vertex.groups if g.weight > 0]
total = sum(weight for _, weight in links)
hits = [(moved[group][1], weight / total) for group, weight in links if group in moved]
if not hits:
continue
used.update(moved[group][0] for group, _ in links if group in moved)
at = to_rig @ vertex.co
shift = sum(((change @ at - at) * share for change, share in hits), at * 0)
vertex.co = from_rig @ (at + shift)
return sorted(used)


def merge_gun(gun):
"""Merge nsf_GUN into jinrai_GUN; return (arms max delta, whole-mesh max delta, worst slot/frame, re-bound bones)."""
kept_scene, old_scene = bpy.data.scenes['jinrai_' + gun], bpy.data.scenes['nsf_' + gun]
kept, old = kept_scene.objects['jinrai_%s_rig' % gun], old_scene.objects['nsf_%s_rig' % gun]
mesh = old_scene.objects['nsf_%s_preview' % gun]
team_collection = bpy.data.collections['nsf_%s_preview' % gun]
old_camera = old_scene.camera
arms = arm_vertices(mesh)
window = bpy.context.window
window.scene = old_scene
frames = slot_frames(old)
reference = {(slot, frame): posed(old_scene, mesh, old, slot, frame) for slot in frames for frame in frames[slot]}
kept_slots = {slot.name_display for slot in kept.animation_data.action.slots}
missing = sorted(set(frames) - kept_slots)
if missing:
raise RuntimeError('%s: NSF slots %s are missing from the Jinrai action' % (gun, missing))

old_scene.collection.children.unlink(team_collection)
kept_scene.collection.children.link(team_collection)
relative = old.matrix_world.inverted() @ mesh.matrix_world
bound = rebind(mesh, kept, old)
mesh.parent = kept
mesh.matrix_world = kept.matrix_world @ relative
for modifier in mesh.modifiers:
if modifier.type == 'ARMATURE':
modifier.object = kept
for collection, team in ((team_collection, 'nsf'), (bpy.data.collections['jinrai_%s_preview' % gun], 'jinrai')):
collection[TEAM_KEY] = team
for obj in collection.objects:
obj[TEAM_KEY] = team

window.scene = kept_scene
restore = kept.animation_data.action_slot, kept_scene.frame_current
worst_arms = worst_all = 0.0
worst_at = None
for (slot, frame), before in reference.items():
delta = np.linalg.norm(posed(kept_scene, mesh, kept, slot, frame) - before, axis=1)
if delta[arms].max() > worst_arms:
worst_arms, worst_at = float(delta[arms].max()), (slot, frame)
worst_all = max(worst_all, float(delta.max()))
kept.animation_data.action_slot = restore[0]
kept_scene.frame_set(restore[1])
layer = kept_scene.view_layers[0].layer_collection.children[team_collection.name]
layer.hide_viewport = True
team_collection.hide_render = True

action, armature = old.animation_data.action, old.data
bpy.data.scenes.remove(old_scene)
bpy.data.objects.remove(old)
bpy.data.armatures.remove(armature)
bpy.data.actions.remove(action)
if old_camera is not None:
camera = old_camera.data
bpy.data.objects.remove(old_camera)
if camera.users == 0:
bpy.data.cameras.remove(camera)
return worst_arms, worst_all, worst_at, bound, len(reference), len(arms)


def main():
arguments = sys.argv[sys.argv.index('--') + 1:] if '--' in sys.argv else []
dry = '--dry-run' in arguments
guns = [a for a in arguments if a != '--dry-run'] or sorted(
s.name[4:] for s in bpy.data.scenes if s.name.startswith('nsf_'))
bpy.ops.preferences.addon_enable(module='io_scene_valvesource')
failed = []
for gun in guns:
arms, whole, at, bound, samples, count = merge_gun(gun)
verdict = 'PASS' if arms <= ARMS_TOLERANCE else 'FAIL'
failed += [gun] if verdict == 'FAIL' else []
print('MERGED %-9s %s arms max %.4f u at %s (%d samples, %d arm vertices); whole mesh max %.4f u; re-bound %s' % (
gun, verdict, arms, at, samples, count, whole, bound or 'none'))
if failed:
raise RuntimeError('NSF arms moved over %.2f u on %s: nothing saved' % (ARMS_TOLERANCE, ', '.join(failed)))
if dry:
print('DRY RUN: not saved')
return
bpy.data.orphans_purge(do_recursive=True) # the removed rigs' bone shapes; a save drops one level only
bpy.ops.wm.save_mainfile()
backup = Path(bpy.data.filepath + '1')
if backup.exists():
backup.unlink()
print('SAVED', bpy.data.filepath)


if __name__ == '__main__':
try:
main()
except RuntimeError as problem:
print('MERGE FAILED:', problem)
sys.exit(1)
Binary file removed neo/modelsrc/weapons/nsf_tachi/v_nsf_tachi.blend
Binary file not shown.
Loading