Files
Ornament-Generator-RNG/blender/export_roblox_glb.py
T
2026-11-17 20:19:55 +03:00

54 lines
3.3 KiB
Python

import bpy, os, json
BASE=os.path.dirname(os.path.abspath(__file__))
reports=[]
for name in ('christmas_train','gingerbread_house'):
folder=os.path.join(BASE,name)
bpy.ops.wm.open_mainfile(filepath=os.path.join(folder,name+'.blend'))
bpy.context.scene.frame_set(1)
def presentation(o):
while o:
if o.name.startswith('PRESENTATION'): return True
o=o.parent
return False
for o in list(bpy.data.objects):
if presentation(o) or o.type in {'CAMERA','LIGHT'}: bpy.data.objects.remove(o,do_unlink=True)
bpy.ops.object.select_all(action='DESELECT')
meshes=[o for o in bpy.context.scene.objects if o.type in {'MESH','CURVE'}]
for o in meshes:o.select_set(True)
bpy.context.view_layer.objects.active=meshes[0]
bpy.ops.object.convert(target='MESH')
meshes=[o for o in bpy.context.scene.objects if o.type=='MESH']
for o in meshes:
if not o.data.uv_layers:
bpy.ops.object.select_all(action='DESELECT');o.select_set(True);bpy.context.view_layer.objects.active=o
bpy.ops.object.mode_set(mode='EDIT');bpy.ops.mesh.select_all(action='SELECT');bpy.ops.uv.smart_project(island_margin=.02);bpy.ops.object.mode_set(mode='OBJECT')
# Original assets use solid-color PBR materials. Embed their colors as real
# PNG textures while keeping metallic, roughness and emission settings.
mats={m for o in meshes for m in o.data.materials if m}
texdir=os.path.join(folder,'textures');os.makedirs(texdir,exist_ok=True)
for i,m in enumerate(sorted(mats,key=lambda m:m.name)):
p=next((n for n in m.node_tree.nodes if n.type=='BSDF_PRINCIPLED'),None)
if not p:continue
color=tuple(p.inputs['Base Color'].default_value)
im=bpy.data.images.new(name+'_'+m.name+'_BaseColor',width=16,height=16,alpha=True)
im.generated_color=color
im.pixels[:]=list(color)*256
im.filepath_raw=os.path.join(texdir,'%02d_basecolor.png'%i);im.file_format='PNG';im.save();im.pack()
tex=m.node_tree.nodes.new('ShaderNodeTexImage');tex.image=im
m.node_tree.links.new(tex.outputs['Color'],p.inputs['Base Color'])
for o in bpy.context.scene.objects:
if o.animation_data:o.animation_data_clear()
bpy.ops.object.select_all(action='SELECT')
target=os.path.join(folder,name+'_roblox.glb')
bpy.ops.export_scene.gltf(filepath=target,export_format='GLB',use_selection=True,export_apply=True,export_animations=False,export_yup=True,export_texcoords=True,export_normals=True,export_materials='EXPORT')
expected=len(meshes)
# Round-trip check ensures the file actually loads with meshes and textures.
bpy.ops.wm.read_factory_settings(use_empty=True)
bpy.ops.import_scene.gltf(filepath=target)
imported=[o for o in bpy.context.scene.objects if o.type=='MESH']
images=[im for im in bpy.data.images if im.type=='IMAGE']
assert imported and images, 'Missing meshes or embedded textures'
reports.append({'file':target,'bytes':os.path.getsize(target),'source_mesh_objects':expected,'imported_mesh_objects':len(imported),'embedded_images':len(images),'triangles':sum(len(p.vertices)-2 for o in imported for p in o.data.polygons)})
with open(os.path.join(BASE,'glb_export_report.json'),'w',encoding='utf8') as f:json.dump(reports,f,indent=2)
print('GLB_EXPORT_VERIFIED',json.dumps(reports))