File size: 1,826 Bytes
3e8a75e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Imports each GLB listed in a text file into an empty Blender scene and prints one JSON line per file:
# {"file", "ok", "meshes", "triangles", "armatures", "actions", "frames", "error"}.
# A file passes with a mesh, or (a skeleton-only clip) with an armature and an action that has keys.
# Usage: blender -b --factory-startup --python blender_check.py -- <file_list.txt>
import json
import sys

import bpy

sys.stdout.reconfigure(encoding="utf-8", errors="replace")
list_path = sys.argv[sys.argv.index("--") + 1]
with open(list_path, encoding="utf-8") as handle:
    file_paths = [line.strip() for line in handle if line.strip()]

for file_path in file_paths:
    bpy.ops.wm.read_factory_settings(use_empty=True)
    record = {"file": file_path}
    try:
        bpy.ops.import_scene.gltf(filepath=file_path)
        meshes = [item for item in bpy.context.scene.objects if item.type == "MESH"]
        record["meshes"] = len(meshes)
        record["triangles"] = sum(sum(len(polygon.vertices) - 2 for polygon in item.data.polygons) for item in meshes)
        record["armatures"] = sum(1 for item in bpy.context.scene.objects if item.type == "ARMATURE")
        record["actions"] = len(bpy.data.actions)
        if bpy.data.actions:
            start, end = bpy.data.actions[0].frame_range
            record["frames"] = int(end - start) + 1
        skeleton_clip = record["armatures"] > 0 and record.get("frames", 0) > 1
        record["ok"] = (record["meshes"] > 0 and record["triangles"] > 0) or skeleton_clip
        if not record["ok"]:
            record["error"] = "imported without any mesh or animated armature"
    except Exception as error:  # Blender raises RuntimeError with the importer's message
        record["ok"] = False
        record["error"] = str(error)[:300]
    print(json.dumps(record), flush=True)