Blender (jusqu'à 2.49)
Modules
Mod_elementary shapes
Mod_formeselementaires
    Début   Index
précédent Fonction helpdoc
Sauver un Theme Suivant


#!BPY

""" Registration info for Blender menus: <- these words are ignored
Name: 'module : elementary shapes'
Blender: 233
Group: 'Mesh'
Tip: 'return a few class of mesh class. '
"""

#----------------------------------------------
# Enregistrez ce script dans le répertoire /.blender/scripts
# Save this script in the  /.blender/scripts
#
#  Son nom : mod_form_elem.py
#  its name :
#
#  On peut l'utiliser tel quel mais il est surtout prévu pour etre appeler 
#  a partir d'un autre script, voir exemple d'utilisation ci-dessous .
#
# We can use it as it is  but it was planed to be called from an other script,
# just see the amble behind this cell.

#  Briques elementaires 
# (c) jm soler juillet 2004, released under Blender Artistic Licence 
#    for the Blender 2.34 Python Scripts Bundle.
#----------------------------------------------
# Page officielle :
#   http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_modelmshape.htm
# Communiquer les problemes et erreurs sur:
#   http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender
#---------------------------------------------
# ce script est proposé sous Blender Artistic Licence pour etre associe
# a la distribution de Blender 2.33 et 2.34
#----------------------------------------------

import Blender
from Blender import Object,NMesh,Scene 
from math import pi,cos,sin

def sphere(nom='sphereModele',type='ico',n=8,smooth=0):
            """ nam='sphereModele' = default name 
                type='ico' = type au choix 'ico' ou 'UV', 
                Seul le type 'UV' permet une modification du nombre de face.
                n=8        = number  of face for the  type UV
                smooth=0   = lissage automatique
            """ 
            ME=Object.New('Mesh',nom)
            Scene.getCurrent().link(ME)
            me=ME.getData() 
            if type=='ico':
               icovert = [    [0.0,0.0,-2.0], 
                [1.4472, -1.05144,-0.89443],
                [-0.55277, -1.70128,-0.89443], 
                [-1.78885,0.0,-0.89443],
                [-0.55277,1.70128,-0.89443], 
                [1.4472,1.05144,-0.89443],
                [0.55277,-1.70128,0.89443],
                [-1.4472,-1.05144,0.89443],
                [-1.4472,1.05144,0.89443],
                [0.55277,1.70128,0.89443],
                [1.78885,0.0,0.89443],
                [0.0,0.0,2.0]]

               icoface = [
                [1,0,2],
                [1,0,5],
                [2,0,3],
                [3,0,4],
                [4,0,5],
                [1,5,10],
                [2,1,6],
                [3,2,7],
                [4,3,8],
                [5,4,9],
                [10,1,6],
                [6,2,7],
                [7,3,8],
                [8,4,9],
                [9,5,10],
                [6,10,11],
                [7,6,11],
                [8,7,11],
                [9,8,11],
                [10,9,11]] 
               for v in icovert:
                    vn=NMesh.Vert(v[0],v[1],v[2])
                    me.verts.append(vn)

               for f in icoface:
                    f_=NMesh.Face()
                    for v in f: 
                       f_.append(me.verts[v])
                    f_.smooth=smooth
                    me.faces.append(f_)
            elif type=='UV':
                for i in range(0,n):
                    for j in range(0,n):
                        x=sin(j*pi*2.0/(n-1))*cos(-pi/2.0+i*pi/(n-1))*2.0
                        y=cos(j*pi*2.0/(n-1))*(cos(-pi/2.0+i*pi/(n-1)))*2.0
                        z=sin(-pi/2.0+i*pi/(n-1))*2.0
                        v=NMesh.Vert(x,y,z)
                        me.verts.append(v)
                n0=len(range(0,n))
                for i in range(0,n-1): 
                    for j in range(0,n-1): 
                        f=NMesh.Face() 
                        f.v.append(me.verts[i*n0+j]) 
                        f.v.append(me.verts[i*n0+j+1]) 
                        f.v.append(me.verts[(i+1)*n0+j+1]) 
                        f.v.append(me.verts[(i+1)*n0+j]) 
                        me.faces.append(f)
                        f.smooth=smooth 
                    #me.faces.append(f)
                    #f.smooth=smooth

            me.update()
            return ME

def cylindre(nom='cylindreModele',type='cyl',r1=1.0, r2=1.0,h=1.0, n=8,smooth=1):
          ME=Object.New('Mesh',nom)
          Scene.getCurrent().link(ME)
          me=ME.getData() 
          r=[r1,r2]
          for i in range(0,2):
              for j in range(0,n):
                  x=sin(j*pi*2/(n))*r[i]
                  y=cos(j*pi*2/(n))*r[i]
                  z=float(i)*h
                  v=NMesh.Vert(x,y,z)
                  me.verts.append(v)

          vlist=[v for v in range(n)]
          vlist.append(0)
          #print vlist
          for i in range(n):
              f=NMesh.Face() 
              f.v.append(me.verts[vlist[i]]) 
              f.v.append(me.verts[vlist[i+1]])
              f.v.append(me.verts[vlist[i+1]+n]) 
              f.v.append(me.verts[vlist[i]+n]) 
              me.faces.append(f) 
              f.smooth=smooth

          if type=='cyl':
              pos=[[0.0,0.0,0.0],[0.0,0.0,h]]
              V0=NMesh.Vert(pos[0][0],pos[0][1],pos[0][2])
              V1=NMesh.Vert(pos[1][0],pos[1][1],pos[1][2])
              me.verts.append(V0)
              me.verts.append(V1)

              for i in range(n):
                  f=NMesh.Face()
                  f.v.append(me.verts[vlist[i]]) 
                  f.v.append(me.verts[vlist[i+1]])
                  f.v.append(me.verts[-2])
                  me.faces.append(f) 
                  f.smooth=smooth

                  f=NMesh.Face()
                  f.v.append(me.verts[vlist[i+1]+n]) 
                  f.v.append(me.verts[vlist[i]+n]) 
                  f.v.append(me.verts[-1])
                  me.faces.append(f) 
                  f.smooth=smooth
          
          me.update()
          return ME

def box(nom='boxModele',low=[-1.0,-1.0,-1.0],high=[1.0,1.0,1.0]):
                vertices_list=[ 
                    [low[0],low[1],low[2]],
                    [low[0],high[1],low[2]],
                    [high[0],high[1],low[2]],
                    [high[0],low[1],low[2]],
                    [low[0],low[1],high[2]],
                    [low[0],high[1],high[2]],
                    [high[0],high[1],high[2]],
                    [high[0],low[1],high[2]]
                ]

                faces_list=[[0,1,2,3],
                    [4,5,6,7],
                    [0,4,7,3],
                    [1,2,6,5],
                    [0,1,5,4],
                    [3,7,6,2]]
                ME=Object.New('Mesh',nom)
                Scene.getCurrent().link(ME)
                me=ME.getData() 

                for coordinate in vertices_list:
                    v=NMesh.Vert(coordinate[0], coordinate[1], coordinate[2])
                    me.verts.append(v)

                for thisface in faces_list:
                    f=NMesh.Face()
                    for vertexpos in thisface:
                        f.append(me.verts[vertexpos])
                    me.faces.append(f)
                me.update()
                return ME
 
 

#
# on suppose que les points sont déjà organisés
#
def polygon( nom='polygoneModele',
                      pointlist=[[0.0,0.0,0.0],
                                     [0.0,1.0,0.0],
                                     [1.0,1.0,0.0],
                                     [1.0,0.0,0.0]]):
        ME=Object.New('Mesh',nom)
        Scene.getCurrent().link(ME)
        me=ME.getData()  
        for p in pointlist:
            v=NMesh.Vert(p[0], p[1], p[2])
            me.verts.append(v)
        pf=[]
        for v in range(len(me.verts)-1):
               pf.append([v,v+1])
        pf.append([len(me.verts)-1,0])
        print 'len(pf)       ',len(pf) 

        if  len(pf)%2==0 and len(pf)>2: 
            P0=pf[:(len(pf))/2] ; P1=pf[len(pf)/2:]; P1.reverse()
            for s in range(len(P0)-1):
                f=NMesh.Face()
                table=[P0[s][0],P0[s][1],P1[s+1][0],P1[s+1][1]]
                for t in table:
                    f.v.append(me.verts[t])
                me.faces.append(f)

        elif len(pf)%2==1 and len(pf) >3 :

            P0=pf[:(len(pf)-1)/2];
            P1=pf[(len(pf)-1)/2:-1];
            P1.reverse()

            print P0,P1
            for s in range(len(P0)-1):
                f=NMesh.Face()
                table=[P0[s][0],P0[s][1],P1[s+1][0],P1[s+1][1]]
                print table
                for t in table:
                    f.v.append(me.verts[t])
                me.faces.append(f)

                f=NMesh.Face()
                table=[pf[-1][0],P0[0][0],P1[1][1]]
                print table
                for t in table:
                    f.v.append(me.verts[t])

                me.faces.append(f)

        elif len(pf) ==3 : 
           f=Blender.NMesh.Face()
           table=[pf[0][0],pf[0][1],pf[1][1]]
           for t in table:f.v.append(me.verts[t])
           me.faces.append(f)
        me.update()
        return ME 

def plane(nom='planeModele',X=1.0,Y=1.0):
        poly=polygon(nom,[[-X,-Y,0.0],[-X,Y,0.0],[X,Y,0.0],[X,-Y,0.0]])

def disque( nom='disqueModele',type='cyl',r=1.0,n=8):
        pointlist=[]
        for j in range(n):
            x=sin(j*pi*2.0/(n))*r
            y=cos(j*pi*2.0/(n))*r
            pointlist.append([x,y,0.0])
        if type=='plane':
           poly=polygon(nom,pointlist)
        else:
          ME=Object.New('Mesh',nom)
          Scene.getCurrent().link(ME)
          me=ME.getData()  

          for p in pointlist:
            v=NMesh.Vert(p[0], p[1], p[2])
            me.verts.append(v)
          pos=[0.0,0.0,0.0]
          V0=NMesh.Vert(pos[0],pos[1],pos[2])
          me.verts.append(V0)
          vlist=[v for v in range(n)]
          vlist.append(0)
          for i in range(n):
                  f=NMesh.Face()
                  f.v.append(me.verts[vlist[i]]) 
                  f.v.append(me.verts[vlist[i+1]])
                  f.v.append(me.verts[-1])
                  me.faces.append(f) 
          me.update()
          return ME 

if __name__=="__main__" :
 sph=sphere('test2','ico',1)
 sph=sphere('test','UV',16)
 cyl=cylindre('test4','tube',1.0,1.0,2.0,10)
 cyl=cylindre('test3','cyl',1.0,0.5,2.0,10)
 cube=box('test5')
 cube=box('test6',[-4.0,-1.0,0.0],[1.0,2.0,3.0])
 poly=polygon('test7')
 d=disque('dd','cyl',2.5, 5)
 d=disque('dd0','plane',2.5, 5)

Exemple d'utilisation:
 
 

import Blender
from Blender import *

import mod_form_elem
# ===================================
# la ligne suivante n'est necessaire que si 
# le fichier forme elementaire ne se trouve pas 
# la pile des textes du fichier blender actuel
# ===================================
reload(mod_form_elem)
# ===================================
from mod_form_elem import *

sph=sphere('test2','ico',1)
sph=sphere('test','UV',16)

cyl=cylindre('test4','tube',1.0,1.0,2.0,10)
cyl=cylindre('test3','cyl',1.0,0.5,2.0,10)
cube=box('test5')
cube=box('test6',[-4.0,-1.0,0.0],[1.0,2.0,3.0])
poly=polygon('test7')

d=disque('dd',2.5, 5) 


 
précédent Fonction helpdoc
 Sauver un Theme Suivant
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