import bpy, sys, os, json
root, out = sys.argv[sys.argv.index('--') + 1:]
res = []
for d, _, fs in os.walk(root):
    for f in sorted(fs):
        if not f.lower().endswith('.fbx'): continue
        p = os.path.join(d, f)
        bpy.ops.wm.read_factory_settings(use_empty=True)
        try:
            bpy.ops.import_scene.fbx(filepath=p)
        except Exception as e:
            res.append({'f': os.path.relpath(p, root), 'err': str(e)[:120]}); continue
        objs = [o for o in bpy.data.objects if o.type == 'MESH']
        mats = sorted({s.material.name for o in objs for s in o.material_slots if s.material})
        arm = any(o.type == 'ARMATURE' for o in bpy.data.objects)
        # world-space size after the file's own scale
        import mathutils
        mn = [1e9] * 3; mx = [-1e9] * 3
        for o in objs:
            for c in o.bound_box:
                w = o.matrix_world @ mathutils.Vector(c)
                for i in range(3): mn[i] = min(mn[i], w[i]); mx[i] = max(mx[i], w[i])
        imgs = sorted({os.path.basename(i.filepath.replace(chr(92), '/')) for i in bpy.data.images})
        res.append({'f': os.path.relpath(p, root).replace(os.sep, '/'), 'imgs': imgs, 'mats': mats, 'verts': sum(len(o.data.vertices) for o in objs),
                    'meshes': len(objs), 'arm': arm, 'anim': len(bpy.data.actions), 'size': [round(mx[i] - mn[i], 4) for i in range(3)] if objs else None})
json.dump(res, open(out, 'w'), indent=0)
print('done', len(res))
