File size: 1,089 Bytes
870b85a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import bpy,sys,json,os,numpy as np,mathutils,hashlib
argv=sys.argv[sys.argv.index("--")+1:]; spec=json.load(open(argv[0])); out=argv[1]; os.makedirs(out,exist_ok=True)
for a in spec:
    f=out+"/"+hashlib.md5(a["key"].encode()).hexdigest()+".npz"
    if os.path.exists(f): continue
    bpy.ops.wm.read_factory_settings(use_empty=True); err=None
    for p in a["paths"]:
        try: bpy.ops.import_scene.gltf(filepath=p)
        except Exception as e: err=str(e)[:80]
    pts=[]
    dg=bpy.context.evaluated_depsgraph_get()
    for o in bpy.context.scene.objects:
        if o.type!="MESH": continue
        me=o.data; n=len(me.vertices)
        if not n: continue
        co=np.empty(n*3,dtype=np.float32); me.vertices.foreach_get("co",co); co=co.reshape(-1,3)
        M=np.array(o.matrix_world,dtype=np.float32); pts.append(co@M[:3,:3].T+M[:3,3])
    if not pts: np.savez(f,key=a["key"],err=err or "no_mesh"); continue
    P=np.concatenate(pts); rng=np.random.default_rng(0)
    if len(P)>6000: P=P[rng.choice(len(P),6000,replace=False)]
    np.savez(f,key=a["key"],pts=P,err=err or "")