Blender (jusqu'à 2.49)
Python 
Tips and  Tricks (3)
Description: split a mesh in its different  pieces v0.1.
(Updated : 2005/07/12)
(version française)

 
 
Beginning Index
previous  Script : envmap
Script spread by material : Next

Description  : this script spreads the various pieces contained in the same mesh in several distinct objects. 

In the old versions of blender the boolean operations worked in edit mode not in object mode . As it is really easier to select objects than fragments in a  mesh with the Lkey, the first interest is to separate the pieces issued from an operation of intersection. 

Another possibility would be related to the dispersion of the pieces of the mesh resulting from a calculation of radiosity.  However the current algorithm does not make it possible to preserve the names of materials (perhaps because of a bug in the NMesh module).
 
 

#-------------------------------------------- 
#  split  different parts of a mesh  in separated objects
#   Jm Soler  2002 --> july 2005
#  upadated for r blender2.37a
#  This script is under  GPL licence.
#-------------------------------------------- 
import Blender
from Blender import *
import sys
def split_fragmented_mesh(selected):
    me=Blender.Object.Get(selected)
    mesh=me.getData()
    LF=mesh.faces[:]
    fragment=[]
    List_fragment=[]
    LFAT=[] #List_Face_A_Traiter
    LFDT=[] #List_Face_Deja_Traitees
    LFAT.append(LF[0])
    while len(LF)!=0:
     for f1 in LFAT:
        for v in f1.v:
            for f2 in LF:
               if (v in f2.v)  and (f2 not in LFDT) and  (f2 not in LFAT):
                  LFAT.append(f2)
        if f1 not in LFDT:
           LFDT.append(f1)
     List_fragment.append(LFAT[:])
     for f1 in LFAT:
         del LF[LF.index(f1)]
     LFAT=[]
     if len(LF)!=0:
        LFAT.append(LF[0])
    if len(List_fragment)>1:
      for nouveau in List_fragment:
           fragment=Blender.NMesh.GetRaw()
           for v1 in nouveau:
              for v2 in v1.v:
                 if v2 not in fragment.verts:
                     fragment.verts.append(v2)
           fragment.faces=nouveau
           fragment.materials=mesh.materials[:]
           LFI=List_fragment.index(nouveau)
           if LFI>0:
              names = NMesh.GetNames()
              n=0
              nom_prov=mesh.name+'%s' %LFI
              while nom_prov in names:
                 nom_prov=nom_prov+'%s' % (LFI+n)
                 n+=1 
              ob=NMesh.PutRaw(fragment,nom_prov,1)
              ob.setMatrix(me.getMatrix()) 
           else:
              NMesh.PutRaw(fragment,mesh.name)
    else: print "nothing to do" 
try:
   EDITMODE=Blender.Window.EditMode()
   Blender.Window.EditMode(0)
   list=Blender.Object.GetSelected()[0] 
   if list.getType()=='Mesh':
      split_fragmented_mesh(list.name ) 
   else:
      pass
   Blender.Window.EditMode(EDITMODE) 
except:
   print 'perhaps not a mesh or no object selected' 
Blender.Redraw()

Old version :
 

#-------------------------------------------- 
#  fractionnement d'un mesh en differents objets
#   Jm Soler  2002 (blender 1.80-->2.23, except 2.10/2.12)
#  ce script est proposé sous licence GPL.
#-------------------------------------------- 
import Blender
from Blender import *
import sys

def split_fragmented_mesh(selected):
    List=Blender.Object.Get()

    Ln2=[]
    for n in List:
        Ln2.append(n.name)

    me=Blender.Object.Get(selected)
    nom=me.data.name
    mesh=NMesh.GetRaw(nom)
    List_Faces=mesh.faces[:]
    LF=List_Faces
    fragment=[]
    List_fragment=[]
    List_Face_A_Traiter=[]
    List_Face_Deja_Traitees=[]

    LFAT=List_Face_A_Traiter
    LFDT=List_Face_Deja_Traitees

    LFAT.append(LF[0])
    n=1

    while len(LF)!=0:
     print len(LFAT)
     for v1 in LFAT:
        for v in v1.v:
            for v2 in LF:
               if (v in v2.v) and (v2 not in LFDT) and (v2 not in LFAT):
                  LFAT.append(v2)
                  n= n+1; print n
        if v1 not in LFDT:
           LFDT.append(v1) 
     List_fragment.append(LFAT[:])
     for v1 in LFAT:
          del LF[LF.index(v1)]
     LFAT=[]
     if len(LF)!=0:
        LFAT.append(LF[0])

    for nouveau in List_fragment:
        fragment=Blender.NMesh.GetRaw()
        for v1 in nouveau:
           for v2 in v1.v:
              print v1.mat, len(fragment.verts),len(v1.v), v1.v.index(v2), 
              if v2 not in fragment.verts:
                   fragment.verts.append(v2)
        fragment.faces=nouveau
        fragment.mats=mesh.mats[:]
        nom_prov=mesh.name+'%s' % List_fragment.index(nouveau)
        mprov=NMesh.GetRaw(nom_prov)
        while mprov!=None:
              nom_prov=nom_prov+'%s' % List_fragment.index(nouveau)
              mprov=NMesh.GetRaw(nom_prov)

        NMesh.PutRaw(fragment,nom_prov,1)
        List=Blender.Object.Get()
        Ln=[]
        N_obname=''
        for n in List:
           Ln.append(n.name)
        for L in Ln:
            if L not in Ln2:
                N_obname=L;
                break 
        Ln2=Ln

        ob=Blender.Object.Get(N_obname)
        ob.loc[0],ob.loc[1],ob.loc[2]=me.loc[0],me.loc[1],me.loc[2]
        ob.rot[0],ob.rot[1],ob.rot[2]=me.rot[0],me.rot[1],me.rot[2]
        ob.size[0],ob.size[1],ob.size[2]=me.size[0],me.size[1],me.size[2]
 

list=Blender.Object.GetSelected()
if len(list)>0:
   selected=list[0].name 
   split_fragmented_mesh(selected)
   Blender.Redraw()

sys.stdout.flush()


 
previous  Script : envmap
 Script spread by material : Nextt
Vers le  Haut de page

Les questions concernant cette page  peuvent être posées sur  :
news://news.zoo-logique.org/3D.Blender


 

 

Livre en français
Blender : apprenez, pratiquez, Créez, livre, Ed. Campus Press, coll. Starter Kit
Blender Starter Kit

Forum
FAQ
Lexique
Didacticiels
Compilations
Blender2KT
Débuter
Modelage
Blender python
Materiaux
Lumière
Animation
API python (eng)
Archives nzn
Statistiques
Doc flash Sculptris
Galerie Sculptris

mon site de démos sur youtube