Files
Ornament-Generator-RNG/blender/trim_island.py
T
2026-11-16 23:50:40 +03:00

20 lines
1.3 KiB
Python

import bpy,bmesh,os
from mathutils import Vector
sc=bpy.context.scene;o=sc.objects.get('Island');assert o and o.type=='MESH'
folder=os.path.dirname(bpy.data.filepath)
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(folder,'map_before_island_trim.blend'),copy=True)
o.data=o.data.copy()
world=[o.matrix_world@v.co for v in o.data.vertices]
lo=min(p.z for p in world);hi=max(p.z for p in world)
cut=hi-(hi-lo)*.16
inv=o.matrix_world.inverted();point=inv@Vector((0,0,cut));normal=o.matrix_world.to_3x3().transposed()@Vector((0,0,1));normal.normalize()
bm=bmesh.new();bm.from_mesh(o.data)
res=bmesh.ops.bisect_plane(bm,geom=list(bm.verts)+list(bm.edges)+list(bm.faces),dist=.0001,plane_co=point,plane_no=normal,clear_inner=True,clear_outer=False)
edges=[e for e in bm.edges if e.is_boundary and all(abs((o.matrix_world@v.co).z-cut)<.002 for v in e.verts)]
filled=bmesh.ops.holes_fill(bm,edges=edges,sides=0)
for f in filled['faces']:f.material_index=1 if len(o.data.materials)>1 else 0
bmesh.ops.recalc_face_normals(bm,faces=list(bm.faces));bm.to_mesh(o.data);bm.free();o.data.update()
assert min((o.matrix_world@v.co).z for v in o.data.vertices)>=cut-.01
path=os.path.join(folder,'map_trimmed_island.blend');bpy.ops.wm.save_as_mainfile(filepath=path)
print('Previous Z range',lo,hi,'new bottom',cut,'bottom faces',len(filled['faces']),'saved',path)