Script: Split Object Faces

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.

These two scripts split mesh too :
http://jmsoler.free.fr/didacticiel/blender/tutor/python_wireshadows.htm
http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_mesh3d2uv2d.htm
but the method is simpler.

Hey thats fun and even usefull as it is, no probs running it by the way.

Windows XP home, geforcefx 5200, Blender 2.34.

Thanks, can’t wait for the updates.

Ken

nice. thanks.

will you make this onFrameChanged linked script?

Yes.

I am right now testing with “On Frame Changed” script link. Works ok.
Any ideas to make this script more useful?

Thanks for your interest in the script.

  • Satish.

nice. thanks.[/quote]

note: the unweld script in blender do the same thing and split all the faces if you select all the vertices.

nice. thanks.[/quote]

note: the unweld script in blender do the same thing and split all the faces if you select all the vertices.[/quote]

It does split the faces, but they are joined at one vertex. I want my faces to be totally separate. I tested it. Maybe i am wrong.

It works if you select all the vertices.

Hi jms.

I did that. Here is the output with your method and the output with my script.

http://heineken.cs.stevens-tech.edu/~sgoda/blender/wip.html

  • Satish.

you are right, there is a problem .
in fact, its work is just to disconnect one
or several vertices not complete faces.
to split faces, i use thetesselate script

It can work with minor improvements i.
If I change these lines:


...
         if result<4:
              for v in me.verts:
                  if v.sel==1:
                    lebon=v    
...

for :


       ...if result<4:
              vSelection=[]
              for v in me.verts:
                if v.sel==1:
                    vSelection.append(v)
              for v in  vSelection:
                    lebon=v    
...

Thanks jms.

Heres an idea, can one of you two create a script that seperates the faces from a mesh and then makes them their own objects with the object centers in the face center?

thinking about something to work with my BlODEd module.

MacBlender

hum… seems to be a good idea but
an object mesh like suzane has 1970 faces,
do you really think that blender can manage
as much object at the same time ?

Heres an idea, can one of you two create a script that seperates the faces from a mesh and then makes them their own objects with the object centers in the face center?

some time ago I was thinking about doing something along this lines, but as matter of fact I didn’t really got into it.

my method was tinkered to be:
a) split chunks of mesh with random selection of faces(for f in m.faces: [f.flag=1 if random()>0.5]) , then copy selectedfacesdata to new meshobj, then remove these faces from initial object and iterate this until done. this would to some extent solve huge mesh problems, because there would be less of new objects generated.
b) build a list of new expl_mesh.00x objects, and move them in f.normal of initial object and add random plus rotation plus some sort of gravity
c) do something about lifespan
d) I had some rig to do kabooms with ipos back then and it included a negative lamp in it to cater for craters and such so I wanted to implement this and particles, too

but I didn’t even got close to really writing it down

Something like that:
https://blenderartists.org/forum/viewtopic.php?t=28767
?

nice idea MacBlender. Will incorporate in in my script. How is your Blode thingy coming along? Waiting eagerly for its release. :smiley:

perfect, might take a bit to render but with that script i’ll have a version of an explode script that has object collision.

thanks jms

MacBlender

Hi all,

Hey this is a great thread, :smiley: I was wondering, with this explode script, would it be possible to have explosion patterns, maybe a vortex explode, Like when a small building is hit by a tornado and all the debris is taken up in a vortex, also is it posible to add random directions to the exploded parts.

Thanks great work :smiley: :o

Ken

Yes, if you ask this on the explosion script thread.
https://blenderartists.org/forum/viewtopic.php?t=28767