Butchering a mesh with Python in Blender
It seems that I am on this trip with scripting. My last post also relates to scripting. (Aren’t scripting languages wonderful? They should be the future of the enterprise! – and by that I don’t mean the Starship Enterprise) One of my hobbies lately has been to play around with 3D modeling and animation in Blender. As a result of that playing around, I wrote a Python script that takes a given mesh and creates a new mesh from each individual face.
Now you may be wondering what use such a script has. In my case, I used it for a text that I converted to a mesh and then exploded into all its individual faces, scattering them randomly along a single axis. When seen from above or from the side, it looks like a mess (subdividing bigger faces before the time helps a lot). Then you look at it with an orthogonal camera, and when panning around to the front (or the top, or whichever way the text is facing), you see your text come together. It makes for a nice effect.
Now the script below does just that – it scatters the faces randomly along the Z axis. You could of course just remove that code and have the script create the individual pieces for you without changing their location.
Here’s the script:
# For a selected mesh, create a new mesh from each face # and set its origin to somewhere random along the Y axis # and subsequently clear the mesh from Blender import * mesh = Mesh.Get('myMesh') #<-- Your mesh's name here faces = mesh.faces counter = 0 # Traverse faces in mesh for face in faces: # New mesh and object names counter = counter + 1 meshname = 'mesh' + str(counter) newmesh = Mesh.New(meshname) objectname = 'obj' + str(counter) newface = [ face ] newverts =  newface =  vert_count = 0 rand_z = Mathutils.Rand(-7, 7) # Build list of vertices and a face for the new mesh from the old one for v in face.verts: # Create new vertices and faces newverts.append([v.co.x, v.co.y, ( v.co.z + rand_z) ]) newface.append(vert_count) vert_count += 1 newmesh.verts.extend(newverts) newmesh.faces.extend(newface) # Add the new mesh object to the scene scn = Scene.GetCurrent() ob = scn.objects.new(newmesh, objectname) Redraw()