76 lines
3.3 KiB
Python
76 lines
3.3 KiB
Python
"""Export the posed gloved arms (no rifle) as a Roblox-ready GLB that lines up with sniper_rifle.glb.
|
|
|
|
Run in background Blender on sniper_reference_v12_hands_natural.blend (the .blend is not saved):
|
|
blender -b <v12.blend> --python export_hands_roblox.py
|
|
|
|
The pose comes from pose_hands_natural.py. Every mesh is split per material, moved so the
|
|
pistol grip (GRIP) is the origin, like the rifle export, and each part's bounding-box centre is
|
|
written to hands_layout.json: Roblox recentres each MeshPart on import, so the viewmodel
|
|
script places the parts from these centres.
|
|
"""
|
|
import json
|
|
import os
|
|
|
|
import bmesh
|
|
import bpy
|
|
from mathutils import Matrix, Vector
|
|
|
|
OUT_DIR = r'C:\roblox\hunter\3D models\sniper\export'
|
|
GLB = os.path.join(OUT_DIR, 'sniper_hands.glb')
|
|
GRIP = Vector((-2.75, 0.0, -0.85))
|
|
MAX_TEX = 1024
|
|
|
|
g = {'__name__': 'pose'}
|
|
exec(open(r'C:\roblox\hunter\blender\pose_hands_natural.py', encoding='utf-8').read(), g)
|
|
scene = bpy.data.scenes['Reference Sculpt V4']
|
|
dg = bpy.context.evaluated_depsgraph_get()
|
|
dg.update()
|
|
|
|
created, layout = [], {}
|
|
for src_name, label in (('hardknuckle', 'Glove'), ('sleeve', 'Sleeve')):
|
|
o = bpy.data.objects[src_name]
|
|
me = bpy.data.meshes.new_from_object(o.evaluated_get(dg), preserve_all_data_layers=True, depsgraph=dg)
|
|
me.transform(Matrix.Translation(-GRIP) @ o.matrix_world)
|
|
for i, mat in enumerate(me.materials):
|
|
bm = bmesh.new()
|
|
bm.from_mesh(me)
|
|
bmesh.ops.delete(bm, geom=[f for f in bm.faces if f.material_index != i], context='FACES')
|
|
if not bm.faces:
|
|
bm.free()
|
|
continue
|
|
bmesh.ops.triangulate(bm, faces=bm.faces)
|
|
name = f'Arms_{label}' + ('' if i == 0 else f'_{mat.name}')
|
|
part = bpy.data.meshes.new(name)
|
|
bm.to_mesh(part)
|
|
bm.free()
|
|
part.materials.clear()
|
|
part.materials.append(mat)
|
|
for p in part.polygons:
|
|
p.material_index = 0
|
|
ob = bpy.data.objects.new(name, part)
|
|
scene.collection.objects.link(ob)
|
|
created.append(ob)
|
|
xs = [v.co for v in part.vertices]
|
|
lo = Vector([min(v[k] for v in xs) for k in range(3)])
|
|
hi = Vector([max(v[k] for v in xs) for k in range(3)])
|
|
layout[name] = {'center': [round(c, 4) for c in (lo + hi) / 2], 'size': [round(c, 4) for c in hi - lo],
|
|
'triangles': len(part.polygons), 'material': mat.name if mat else None}
|
|
bpy.data.meshes.remove(me)
|
|
|
|
# Roblox textures top out at 1024 px; shrink in memory only (the .blend is never saved).
|
|
for im in bpy.data.images:
|
|
if im.size[0] > MAX_TEX:
|
|
im.scale(MAX_TEX, MAX_TEX)
|
|
|
|
for o in scene.objects:
|
|
o.select_set(o in created)
|
|
bpy.context.window_scene if hasattr(bpy.context, 'window_scene') else None
|
|
bpy.ops.export_scene.gltf(filepath=GLB, export_format='GLB', use_selection=True, use_active_scene=True,
|
|
export_apply=True, export_animations=False, export_skins=False, export_yup=True,
|
|
export_texcoords=True, export_normals=True, export_materials='EXPORT',
|
|
export_image_format='JPEG', export_jpeg_quality=90)
|
|
|
|
with open(os.path.join(OUT_DIR, 'hands_layout.json'), 'w', encoding='utf-8') as f:
|
|
json.dump(layout, f, indent=2)
|
|
print('HANDS_EXPORT', json.dumps({'file': GLB, 'bytes': os.path.getsize(GLB), 'layout': layout}))
|