Hey everyone,
Have a need to fragment the faces in a mesh into individual objects, couldn’t find anything with search, but noticed a number of people asking about it. I wrote something rough this morning, so I thought I’d chuck it here for people to play with.
P.S. at the moment, it assumes your mesh is called ‘Plane’ and all its faces are three sided. If you want to use it on a mesh with four sided faces, just remove the triple hashes (###)
########################
# #
# Terrain fragmenter #
# #
########################
import Blender
from Blender import NMesh, Material, Window
# turn off editmode (if on)
editmode = Window.EditMode()
if editmode: Window.EditMode(0)
# get unfragmented terrain mesh
me = NMesh.GetRaw("Plane")
faces = me.faces
# loop through all of the faces in this mesh
for f in faces:
# Get vertices of this face (assuming 3)
v1x = f.v[0].co[0]
v1y = f.v[0].co[1]
v1z = f.v[0].co[2]
v2x = f.v[1].co[0]
v2y = f.v[1].co[1]
v2z = f.v[1].co[2]
v3x = f.v[2].co[0]
v3y = f.v[2].co[1]
v3z = f.v[2].co[2]
### v4x = f.v[3].co[0]
### v4y = f.v[3].co[1]
### v4z = f.v[3].co[2]
# create a new mesh object with one face at these vertices
# (until script stable, offset above host mesh for testing)
mesh = Blender.NMesh.New("Grass")
v1 = Blender.NMesh.Vert(v1x, v1y, v1z+5)
v2 = Blender.NMesh.Vert(v2x, v2y, v2z+5)
v3 = Blender.NMesh.Vert(v3x, v3y, v3z+5)
### v4 = Blender.NMesh.Vert(v4x, v4y, v4z+5)
mesh.verts.append(v1)
mesh.verts.append(v2)
mesh.verts.append(v3)
### mesh.verts.append(v4)
# for three sided faces
face = Blender.NMesh.Face([v1, v2, v3])
# for four sided faces
### face = Blender.NMesh.Face([v1, v2, v3, v4])
mesh.addFace(face)
obj = Blender.Object.New('Mesh', 'Grass')
obj.link(mesh)
scene = Blender.Scene.getCurrent()
scene.link(obj)
Blender.Redraw()
if editmode: Window.EditMode(1)
Cheers,
Simon O