Hi Folks.
I was working on an explosion effect for which i needed the object’s faces to be separate so that they will fall apart. I wrote this small script.
import Blender
from Blender.NMesh import *
import math
#*******************************************************
# Global Variables
# 'g_dirOfDisp' specifies the direction the split face will be displaced
g_dirOfDisp = 0.1
#*******************************************************
so = Blender.Object.GetSelected()
if len(so) and so[0].getType() == 'Mesh':
vtxFaceHash = { }
me = so[0].getData()
#Create the vertex-face hash table
for face in me.faces:
for vtx in face.v:
if me.verts.index(vtx) not in vtxFaceHash.keys():
vtxFaceHash[me.verts.index(vtx)] = [me.faces.index(face)]
else:
vtxFaceHash[me.verts.index(vtx)].append(me.faces.index(face))
# Start splitting the faces
currVtx = newVtx = 0
changeWhere = -1
for pair in vtxFaceHash.items():
if len(pair[1]) > 1:
currVtx = me.verts[pair[0]]
for faceIndex in pair[1][1:]:
newVtx = Vert(currVtx.co[0], currVtx.co[1], currVtx.co[2])
me.verts.append(newVtx)
for vtx in me.faces[faceIndex].v:
changeWhere += 1
if vtx == currVtx:
me.faces[faceIndex].v[changeWhere] = newVtx
changeWhere = -1
del vtxFaceHash
me.update()
# Explode the object
for face in me.faces:
nml = face.normal
for vtx in face.v:
vtx.co[0] += nml[0]*g_dirOfDisp
vtx.co[1] += nml[1]*g_dirOfDisp
vtx.co[2] += nml[2]*g_dirOfDisp
me.update()
else:
print "No MESH Object Selected"
I hope this script is useful. If you find any bugs, please post here. I will add a GUI and some more control parameters asap.
- Satish.