Fragment mesh script

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

:smiley: :smiley: :smiley:

Great could be what I have asking on shattering glass post.

Regards

I tested the script. Works on anything if you first create a mesh plane, and while in edit mode, delete all vertices and add other types, I added Suzanne. Then ran the script. Joined the fragmented pieces, extruded, esc. then Alt S the extruded faces to give them some volume. Subsurfed and superimposed it on the original mesh. :smiley: Quick render:
http://img4.imageshack.us/img4/8967/smartiemonkey4pa.th.jpg

… Have a need to fragment the faces in a mesh into individual objects, couldn’t find anything with search,…

This one, shorter, exists since 2004/08 :
http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_mesh2object.htm