Clean KAMAZ source rig pipeline

This commit is contained in:
krustophski 2026-06-18 06:16:36 +03:00
parent 6f02325597
commit 3857424cfa
14 changed files with 519 additions and 38 deletions

View file

@ -70,6 +70,19 @@ Add these sockets on `SK_Kamaz_Chassis` after import:
## Run Export Prep
First create/update the clean source file:
```powershell
& "C:/Program Files/Blender Foundation/Blender 5.1/blender.exe" --background --python "C:/Users/post/Documents/mrPlow/mrPlow58/Tools/VehiclePipeline/clean_kamaz_source.py" -- --input-root "C:/Users/post/Documents/mrPlow/in"
```
This writes:
- `C:/Users/post/Documents/mrPlow/in/kamaz_65115_clean.blend`
- `C:/Users/post/Documents/mrPlow/in/kamaz_65115_clean_report.json`
Then export FBX files:
Example:
```powershell

View file

@ -12,7 +12,7 @@
"displayName": "KAMAZ KUZOV",
"attachmentType": "dumpBody",
"mountPoint": "dumpBody",
"sourceModelPath": "C:/Users/post/Documents/mrPlow/in/kamaz_65115.blend",
"sourceModelPath": "C:/Users/post/Documents/mrPlow/in/kamaz_65115_clean.blend",
"sourceObjectName": "KUZOV"
},
{
@ -20,9 +20,17 @@
"displayName": "KAMAZ GRUZ",
"attachmentType": "cargo",
"mountPoint": "dumpBody",
"sourceModelPath": "C:/Users/post/Documents/mrPlow/in/kamaz_65115.blend",
"sourceModelPath": "C:/Users/post/Documents/mrPlow/in/kamaz_65115_clean.blend",
"sourceObjectName": "GRUZ"
},
{
"attachmentId": "kamaz_broom",
"displayName": "KAMAZ rear broom",
"attachmentType": "rearBrush",
"mountPoint": "rear",
"sourceModelPath": "C:/Users/post/Documents/mrPlow/in/kamaz_65115_clean.blend",
"sourceObjectName": "broom"
},
{
"attachmentId": "kraz_body_for_bulk",
"displayName": "KrAZ body for bulk",

View file

@ -11,7 +11,7 @@
"vehicleId": "kamaz_65115",
"displayName": "KAMAZ 65115",
"manufacturer": "KAMAZ",
"sourceModelPath": "C:/Users/post/Documents/mrPlow/in/kamaz_65115.blend",
"sourceModelPath": "C:/Users/post/Documents/mrPlow/in/kamaz_65115_clean.blend",
"wheelFormula": "6x4",
"fuelType": "diesel",
"emptyMassKg": 10125,
@ -62,6 +62,7 @@
"name": "rear_1",
"leftWheelBone": "Phys_Wheel_ML1",
"rightWheelBone": "Phys_Wheel_MR1",
"wheelBones": ["Phys_Wheel_ML1", "Phys_Wheel_ML2", "Phys_Wheel_MR1", "Phys_Wheel_MR2"],
"driven": true,
"steerable": false,
"dualTires": true
@ -70,6 +71,7 @@
"name": "rear_2",
"leftWheelBone": "Phys_Wheel_RL1",
"rightWheelBone": "Phys_Wheel_RR1",
"wheelBones": ["Phys_Wheel_RL1", "Phys_Wheel_RL2", "Phys_Wheel_RR1", "Phys_Wheel_RR2"],
"driven": true,
"steerable": false,
"dualTires": true
@ -88,7 +90,8 @@
],
"notes": [
"KAMAZ 65115 has many modifications. Exact gearbox ratios must be confirmed for the selected chassis.",
"Source model may include KDM equipment, so Blender bounds can be longer than bare dump truck passport length."
"Source model uses C:/Users/post/Documents/mrPlow/in/kamaz_65115_clean.blend as the import source.",
"Source model includes KDM equipment, so Blender bounds can be longer than bare dump truck passport length."
]
},
{

View file

@ -0,0 +1,319 @@
import argparse
import json
import sys
from pathlib import Path
import bpy
from mathutils import Matrix, Vector
SOURCE_FILE = "kamaz_65115.blend"
CLEAN_FILE = "kamaz_65115_clean.blend"
MODULE_OBJECTS = {"KUZOV", "GRUZ", "broom"}
WHEEL_BONE_NAMES = [
"Phys_Wheel_FL",
"Phys_Wheel_FR",
"Phys_Wheel_ML1",
"Phys_Wheel_ML2",
"Phys_Wheel_MR1",
"Phys_Wheel_MR2",
"Phys_Wheel_RL1",
"Phys_Wheel_RL2",
"Phys_Wheel_RR1",
"Phys_Wheel_RR2",
]
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--input-root", required=True)
argv = sys.argv
if "--" in argv:
argv = argv[argv.index("--") + 1:]
else:
argv = []
return parser.parse_args(argv)
def mesh_objects():
return [obj for obj in bpy.context.scene.objects if obj.type == "MESH"]
def select_only(objects):
bpy.ops.object.select_all(action="DESELECT")
for obj in objects:
obj.select_set(True)
if objects:
bpy.context.view_layer.objects.active = objects[0]
def world_bounds(objects):
points = []
for obj in objects:
for corner in obj.bound_box:
points.append(obj.matrix_world @ Vector(corner))
if not points:
return Vector((0, 0, 0)), Vector((0, 0, 0))
return (
Vector((min(p.x for p in points), min(p.y for p in points), min(p.z for p in points))),
Vector((max(p.x for p in points), max(p.y for p in points), max(p.z for p in points))),
)
def center_of(objects):
min_v, max_v = world_bounds(objects)
return (min_v + max_v) * 0.5
def ensure_collection(name):
collection = bpy.data.collections.get(name)
if collection is None:
collection = bpy.data.collections.new(name)
bpy.context.scene.collection.children.link(collection)
return collection
def move_to_collection(obj, collection):
for source_collection in list(obj.users_collection):
source_collection.objects.unlink(obj)
collection.objects.link(obj)
def apply_mesh_transforms(objects):
select_only(objects)
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
def orient_vehicle_x_forward(objects):
cabin = bpy.data.objects.get("CABIN")
rear_body = bpy.data.objects.get("KUZOV")
if not cabin or not rear_body:
return False
cabin_center = center_of([cabin])
rear_center = center_of([rear_body])
if cabin_center.x > rear_center.x:
return False
rotation = Matrix.Rotation(3.141592653589793, 4, "Z")
for obj in objects:
obj.matrix_world = rotation @ obj.matrix_world
return True
def find_wheel_candidates():
candidates = []
for obj in mesh_objects():
if obj.name in MODULE_OBJECTS:
continue
name = obj.name.lower()
if not any(token in name for token in ("tyre", "tire", "wheel")):
continue
c = center_of([obj])
max_dim = max(obj.dimensions.x, obj.dimensions.y, obj.dimensions.z)
# Real side wheels sit near the vehicle sides. This excludes the spare tire.
if max_dim > 0.35 and abs(c.y) > 0.70:
candidates.append({"object": obj, "center": c, "max_dim": max_dim})
if not candidates:
return []
min_z = min(item["center"].z for item in candidates)
ground = [item for item in candidates if item["center"].z <= min_z + 0.75]
# Unreal convention after orientation: X forward, Y right, Z up.
# For a +X-facing vehicle, front axles have larger X, left wheels have negative Y.
ground.sort(key=lambda item: (-item["center"].x, item["center"].y))
return ground[: len(WHEEL_BONE_NAMES)]
def wheel_bone_name_for_index(index):
return WHEEL_BONE_NAMES[index]
def name_wheel_candidates(wheel_candidates):
axles = []
for item in wheel_candidates:
x = item["center"].x
for axle in axles:
if abs(axle["x"] - x) < 0.15:
axle["items"].append(item)
axle["x"] = sum(i["center"].x for i in axle["items"]) / len(axle["items"])
break
else:
axles.append({"x": x, "items": [item]})
axles.sort(key=lambda axle: -axle["x"])
named = []
name_index = 0
for axle in axles:
axle["items"].sort(key=lambda item: item["center"].y)
for item in axle["items"]:
if name_index >= len(WHEEL_BONE_NAMES):
break
named.append({"name": wheel_bone_name_for_index(name_index), **item})
name_index += 1
return named
def create_armature(chassis_objects, wheel_candidates):
bpy.ops.object.armature_add(enter_editmode=True, location=(0, 0, 0), rotation=(0, 0, 0))
armature = bpy.context.object
armature.name = "SK_Kamaz_Chassis_Armature"
armature.data.name = "SKEL_Kamaz"
bones = armature.data.edit_bones
root = bones[0]
root.name = "Root"
root.head = (0, 0, 0)
root.tail = (0, 0, 1)
body = bones.new("Body")
body.parent = root
body.head = center_of(chassis_objects)
body.tail = body.head + Vector((0, 0, 1))
wheel_bones = []
for item in name_wheel_candidates(wheel_candidates):
c = item["center"]
bone_name = item["name"]
bone = bones.new(bone_name)
bone.parent = root
bone.head = c
bone.tail = c + Vector((0, 0, 0.5))
wheel_bones.append({"name": bone_name, "center": c})
bpy.ops.object.mode_set(mode="OBJECT")
return armature, wheel_bones
def add_full_weight_group(obj, group_name):
obj.vertex_groups.clear()
group = obj.vertex_groups.new(name=group_name)
if obj.data.vertices:
group.add([vertex.index for vertex in obj.data.vertices], 1.0, "REPLACE")
def nearest_wheel_bone(obj, wheel_bones):
if not wheel_bones:
return None
c = center_of([obj])
nearest = min(wheel_bones, key=lambda bone: (bone["center"] - c).length)
if (nearest["center"] - c).length <= 0.85:
return nearest["name"]
return None
def rig_chassis_objects(chassis_objects, armature, wheel_bones):
for obj in chassis_objects:
wheel_bone = nearest_wheel_bone(obj, wheel_bones)
target_bone = wheel_bone if wheel_bone else "Body"
add_full_weight_group(obj, target_bone)
modifier = obj.modifiers.new("SK_Kamaz_Chassis_Armature", "ARMATURE")
modifier.object = armature
obj.parent = armature
def add_socket_empty(name, location, collection):
existing = bpy.data.objects.get(name)
if existing:
bpy.data.objects.remove(existing, do_unlink=True)
obj = bpy.data.objects.new(name, None)
obj.empty_display_type = "PLAIN_AXES"
obj.empty_display_size = 0.35
obj.location = location
collection.objects.link(obj)
return obj
def add_socket_markers(chassis_objects, modules, collection):
min_v, max_v = world_bounds(chassis_objects + modules)
chassis_center = center_of(chassis_objects)
kuzov = bpy.data.objects.get("KUZOV")
gruz = bpy.data.objects.get("GRUZ")
broom = bpy.data.objects.get("broom")
rear_center = center_of([kuzov]) if kuzov else chassis_center
cargo_center = center_of([gruz]) if gruz else rear_center
broom_center = center_of([broom]) if broom else Vector((chassis_center.x, chassis_center.y, min_v.z + 0.35))
add_socket_empty("Socket_FrontBlade", Vector((min_v.x - 0.25, 0, min_v.z + 0.65)), collection)
add_socket_empty("Socket_RearModule", rear_center, collection)
add_socket_empty("Socket_Cargo", cargo_center, collection)
add_socket_empty("Socket_Broom", broom_center, collection)
add_socket_empty("Socket_Hitch", Vector((max_v.x + 0.2, 0, min_v.z + 0.75)), collection)
def summarize(objects, wheel_bones):
min_v, max_v = world_bounds(objects)
negative_scale = [
obj.name for obj in objects
if obj.scale.x < 0 or obj.scale.y < 0 or obj.scale.z < 0
]
non_unit_scale = [
obj.name for obj in objects
if any(abs(value - 1.0) > 0.001 for value in (obj.scale.x, obj.scale.y, obj.scale.z))
]
return {
"boundsCm": {
"lengthX": round((max_v.x - min_v.x) * 100.0, 2),
"widthY": round((max_v.y - min_v.y) * 100.0, 2),
"heightZ": round((max_v.z - min_v.z) * 100.0, 2),
},
"negativeScaleObjects": negative_scale,
"nonUnitScaleObjects": non_unit_scale,
"wheelBones": [
{
"name": bone["name"],
"locationM": [round(bone["center"].x, 4), round(bone["center"].y, 4), round(bone["center"].z, 4)],
}
for bone in wheel_bones
],
}
def main():
args = parse_args()
input_root = Path(args.input_root)
source_path = input_root / SOURCE_FILE
clean_path = input_root / CLEAN_FILE
report_path = input_root / "kamaz_65115_clean_report.json"
bpy.ops.wm.open_mainfile(filepath=str(source_path))
for obj in list(bpy.context.scene.objects):
if obj.type == "ARMATURE":
bpy.data.objects.remove(obj, do_unlink=True)
meshes = mesh_objects()
reoriented_x_forward = orient_vehicle_x_forward(meshes)
apply_mesh_transforms(meshes)
chassis_collection = ensure_collection("MrPlow_Chassis")
modules_collection = ensure_collection("MrPlow_Modules")
rig_collection = ensure_collection("MrPlow_Rig")
modules = [obj for obj in mesh_objects() if obj.name in MODULE_OBJECTS]
chassis = [obj for obj in mesh_objects() if obj.name not in MODULE_OBJECTS]
for obj in chassis:
move_to_collection(obj, chassis_collection)
for obj in modules:
move_to_collection(obj, modules_collection)
wheel_candidates = find_wheel_candidates()
armature, wheel_bones = create_armature(chassis, wheel_candidates)
move_to_collection(armature, rig_collection)
rig_chassis_objects(chassis, armature, wheel_bones)
add_socket_markers(chassis, modules, rig_collection)
report = summarize(mesh_objects(), wheel_bones)
report["source"] = str(source_path)
report["clean"] = str(clean_path)
report["reorientedXForward"] = reoriented_x_forward
report["modules"] = sorted([obj.name for obj in modules])
report_path.write_text(json.dumps(report, indent=2), encoding="utf-8")
bpy.ops.wm.save_as_mainfile(filepath=str(clean_path))
if __name__ == "__main__":
main()

View file

@ -7,11 +7,13 @@ from mathutils import Vector
KAMAZ_FILE = "kamaz_65115.blend"
KAMAZ_CLEAN_FILE = "kamaz_65115_clean.blend"
KRAZ_FILE = "kraz_6322 .blend"
KAMAZ_MODULES = {
"KUZOV": "SM_Kamaz_Kuzov.fbx",
"GRUZ": "SM_Kamaz_Gruz.fbx",
"broom": "SM_Kamaz_Broom.fbx",
}
KRAZ_MODULES = {
@ -155,10 +157,51 @@ def parent_chassis_to_armature(chassis_objects, armature):
bpy.ops.object.parent_set(type="ARMATURE_NAME")
def collection_objects(name):
collection = bpy.data.collections.get(name)
if not collection:
return []
return list(collection.objects)
def export_clean_kamaz(output_root: Path):
for obj_name, file_name in KAMAZ_MODULES.items():
obj = bpy.data.objects.get(obj_name)
if obj:
select_only([obj])
export_selected_fbx(output_root / file_name)
armature = bpy.data.objects.get("SK_Kamaz_Chassis_Armature")
chassis = [obj for obj in collection_objects("MrPlow_Chassis") if obj.type == "MESH"]
if not armature or not chassis:
return False
select_only(chassis + [armature])
export_selected_fbx(output_root / "SK_Kamaz_Chassis.fbx", armature=True)
wheel_bones = [
bone.name for bone in armature.data.bones
if bone.name.startswith("Phys_Wheel_")
]
report = output_root / "SK_Kamaz_Chassis_report.txt"
report.write_text(
"Exported SK_Kamaz_Chassis.fbx from kamaz_65115_clean.blend\n"
f"Wheel bones: {', '.join(wheel_bones)}\n"
"Source orientation: X forward, Y right, Z up. Source units: meters. Unreal units: centimeters.\n",
encoding="utf-8",
)
return True
def export_kamaz(input_root: Path, output_root: Path):
bpy.ops.wm.open_mainfile(filepath=str(input_root / KAMAZ_FILE))
clean_path = input_root / KAMAZ_CLEAN_FILE
source_path = clean_path if clean_path.exists() else input_root / KAMAZ_FILE
bpy.ops.wm.open_mainfile(filepath=str(source_path))
ensure_dir(output_root)
if source_path.name == KAMAZ_CLEAN_FILE and export_clean_kamaz(output_root):
return
meshes = all_mesh_objects()
apply_transforms(meshes)

BIN
VehicleExports/SK_Kamaz_Chassis.fbx (Stored with Git LFS)

Binary file not shown.

View file

@ -1,3 +1,3 @@
Exported SK_Kamaz_Chassis.fbx
Wheel bones: Phys_Wheel_FL, Phys_Wheel_FR, Phys_Wheel_ML1, Phys_Wheel_MR1, Phys_Wheel_ML2, Phys_Wheel_MR2, Phys_Wheel_RL1, Phys_Wheel_RR1, Phys_Wheel_RL2, Phys_Wheel_RR2
Verify wheel bone centers in Blender before final Unreal import.
Exported SK_Kamaz_Chassis.fbx from kamaz_65115_clean.blend
Wheel bones: Phys_Wheel_FL, Phys_Wheel_FR, Phys_Wheel_ML1, Phys_Wheel_ML2, Phys_Wheel_MR1, Phys_Wheel_MR2, Phys_Wheel_RL1, Phys_Wheel_RL2, Phys_Wheel_RR1, Phys_Wheel_RR2
Source orientation: X forward, Y right, Z up. Source units: meters. Unreal units: centimeters.

BIN
VehicleExports/SM_Kamaz_Broom.fbx (Stored with Git LFS) Normal file

Binary file not shown.

BIN
VehicleExports/SM_Kamaz_Gruz.fbx (Stored with Git LFS)

Binary file not shown.

BIN
VehicleExports/SM_Kamaz_Kuzov.fbx (Stored with Git LFS)

Binary file not shown.

BIN
VehicleExports/SM_Kraz_BladeBucket.fbx (Stored with Git LFS)

Binary file not shown.

BIN
VehicleExports/SM_Kraz_BodyForBulk.fbx (Stored with Git LFS)

Binary file not shown.

View file

@ -0,0 +1,99 @@
{
"boundsCm": {
"lengthX": 822.36,
"widthY": 292.71,
"heightZ": 295.29
},
"negativeScaleObjects": [],
"nonUnitScaleObjects": [],
"wheelBones": [
{
"name": "Phys_Wheel_FL",
"locationM": [
2.0457,
-1.0791,
0.5372
]
},
{
"name": "Phys_Wheel_FR",
"locationM": [
2.0457,
1.0763,
0.5372
]
},
{
"name": "Phys_Wheel_ML1",
"locationM": [
-1.7023,
-1.0846,
0.5372
]
},
{
"name": "Phys_Wheel_ML2",
"locationM": [
-1.7023,
-0.76,
0.5372
]
},
{
"name": "Phys_Wheel_MR1",
"locationM": [
-1.7023,
0.7598,
0.5372
]
},
{
"name": "Phys_Wheel_MR2",
"locationM": [
-1.7023,
1.0844,
0.5372
]
},
{
"name": "Phys_Wheel_RL1",
"locationM": [
-3.1001,
-1.0859,
0.5372
]
},
{
"name": "Phys_Wheel_RL2",
"locationM": [
-3.1002,
-0.7614,
0.5372
]
},
{
"name": "Phys_Wheel_RR1",
"locationM": [
-3.1002,
0.7598,
0.5372
]
},
{
"name": "Phys_Wheel_RR2",
"locationM": [
-3.1002,
1.0844,
0.5372
]
}
],
"source": "C:\\Users\\post\\Documents\\mrPlow\\in\\kamaz_65115.blend",
"clean": "C:\\Users\\post\\Documents\\mrPlow\\in\\kamaz_65115_clean.blend",
"reorientedXForward": true,
"modules": [
"GRUZ",
"KUZOV",
"broom"
]
}

View file

@ -9,26 +9,23 @@
"vehicles": [
{
"vehicleId": "kamaz_65115",
"sourceModelPath": "C:\\Users\\post\\Documents\\mrPlow\\in\\kamaz_65115.blend",
"sourceModelPath": "C:\\Users\\post\\Documents\\mrPlow\\in\\kamaz_65115_clean.blend",
"exists": true,
"issues": [
"Negative scale objects must be applied before final export",
"Non-unit scale objects must be applied before final export"
],
"issues": [],
"meshObjectCount": 63,
"boundsCm": {
"lengthX": 848.2,
"widthY": 313.41,
"heightZ": 329.19
"lengthX": 822.36,
"widthY": 292.71,
"heightZ": 295.29
},
"boundsMinM": [
-3.7202,
-1.6072,
-0.3397
-4.7618,
-1.4636,
-0.0006
],
"boundsMaxM": [
4.7618,
1.5268,
3.4618,
1.4636,
2.9522
],
"sceneUnitSystem": "METRIC",
@ -39,16 +36,12 @@
"height": 286.5
},
"dimensionErrorPct": {
"lengthX": 21.52,
"widthY": 22.91,
"heightZ": 14.9
"lengthX": 17.82,
"widthY": 14.79,
"heightZ": 3.07
},
"negativeScaleObjects": [
"fuel_tank_other",
"bolt013",
"bolt012"
],
"nonUnitScaleObjectCount": 63
"negativeScaleObjects": [],
"nonUnitScaleObjectCount": 0
},
{
"vehicleId": "kraz_6322",