Files
Hunter/blender/material_bounds.py
2026-11-29 15:49:17 +03:00

43 lines
1.9 KiB
Python

"""Per-material bounding boxes of the exported rifle, in the same space export_sniper_roblox.py
writes (rifle space with the pistol grip at the origin, bevels applied).
Each exported MeshPart is one material, and Roblox centres a MeshPart on its mesh's bounding box,
so comparing these before and after a model edit says how far each MeshPart's centre moves.
exec(open(r'C:\\roblox\\hunter\\blender\\material_bounds.py', encoding='utf-8').read())
MATERIAL_BOUNDS -> {'Rifle_Rubber': {'center': [...], 'size': [...]}, ...}
"""
import bpy
from mathutils import Matrix, Vector
GRIP = Vector((-2.75, 0.0, -0.85))
def material_bounds(collection_name='Rifle V4'):
dg = bpy.context.evaluated_depsgraph_get()
lo, hi = {}, {}
for o in list(bpy.data.collections[collection_name].all_objects):
if o.type != 'MESH' or o.hide_render:
continue
oe = o.evaluated_get(dg)
me = bpy.data.meshes.new_from_object(oe, preserve_all_data_layers=False, depsgraph=dg)
me.transform(Matrix.Translation(-GRIP) @ o.matrix_world)
for poly in me.polygons:
slot = o.material_slots[poly.material_index] if poly.material_index < len(o.material_slots) else None
if not slot or not slot.material:
continue
name = 'Rifle_' + slot.material.name.title().replace(' ', '')
for vi in poly.vertices:
v = me.vertices[vi].co
a = lo.setdefault(name, [1e9] * 3)
b = hi.setdefault(name, [-1e9] * 3)
for i in range(3):
a[i] = min(a[i], v[i])
b[i] = max(b[i], v[i])
bpy.data.meshes.remove(me)
return {n: {'center': [round((lo[n][i] + hi[n][i]) / 2, 5) for i in range(3)],
'size': [round(hi[n][i] - lo[n][i], 5) for i in range(3)]} for n in lo}
MATERIAL_BOUNDS = material_bounds()