Blender (jusqu'à 2.49)
Python : 
Multiplier les cubes 2/2 
Création d'une classe basée sur le module Mesh
(voir aussi : formes élémentaires)
 
    Début   Index
précédent Python : modèle cube
Script Python : spheres en masse Suivant

Exemple d'utilisation du module Mesh
 

# ***** BEGIN GPL LICENSE BLOCK *****
#
# Script copyright (C) J-M Soler 0701//02008
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
 

from Blender import Material,Redraw
import bpy

#-------------------------
# Exemple de classe 
#------------------------- 
class CUBE:
 def __init__(self,taille=1.0,
                  deplacement=1.0,
                 max=1,
                centre=0,
               nom='unMesh'):
  self.Taille=taille 
  self.deplacement_lateral=deplacement
  self.nx=self.ny=self.nz=-self.deplacement_lateral
  self.maxx=self.maxy=self.maxz=max*self.deplacement_lateral
  self.nom=nom
  self.centre=float(centre)*max*self.deplacement_lateral/2.0

  self.vertices_list=[ 
      [-self.Taille/2,  -self.Taille/2, -self.Taille/2],
      [-self.Taille/2,   self.Taille/2,  -self.Taille/2],
      [self.Taille/2,    self.Taille/2,  -self.Taille/2],
      [self.Taille/2,   -self.Taille/2, -self.Taille/2],
      [-self.Taille/2,  -self.Taille/2,  self.Taille/2],
      [-self.Taille/2,   self.Taille/2,   self.Taille/2],
      [self.Taille/2,    self.Taille/2,   self.Taille/2],
      [self.Taille/2,   -self.Taille/2,  self.Taille/2]
  ]

  #-------------------------
  # La liste eest la m^ême pour toutes 
  # les instances 
  #-------------------------
  self.faces_list=[[0,1,2,3],
        [7,6,5,4],
        [3,7,4,0],
        [5,6,2,1],
        [4,5,1,0],
        [2,6,7,3]] 

  self.MESH=None
  #-------------------------
  # ouverture de l'objet mesh dans la mémoire ram
  #-------------------------
  if not self.MESH :
    self.MESH = bpy.data.meshes.new(self.nom) 

  #-------------------------
  # on active les vertex color dans ce mesh
  #------------------------- 
  if not self.MESH.vertexColors:
    self.MESH.vertexColors = 1 

  scn = bpy.data.scenes.active 
  self.OBJET = scn.objects.new(self.MESH)

 def Add_CUBE(self,i,j,k, norm=[0.0,0.0,1.0]): 
   #------------------------- 
   # fonction de creation des cubes
   #------------------------- 
   lastvertex=len(self.MESH.verts)
   VERTEX=[]
   for coordinate in self.vertices_list:
    VERTEX.append([coordinate[0]+i-self.centre, 
                       coordinate[1]+j-self.centre, 
                        coordinate[2]+k-self.centre])
   self.MESH.verts.extend(VERTEX) 

   FACES=[]
   for thisface in self.faces_list:
    A_FACE=[] 
    for vertexpos in thisface:
     A_FACE.append(self.MESH.verts[vertexpos+lastvertex])
    FACES.append(A_FACE)
   self.MESH.faces.extend(FACES) 

   #-------------------------
   # on calcule les valeurs rvb
   #------------------------- 
   r=int(255.0*(i/float(self.maxx)))
   v=int(255.0*(j/float(self.maxy)))
   b=int(255.0*(k/float(self.maxz)))

   #-------------------------
   # on ajoute la couleur a chaque sommet
   #------------------------- 
   for n in [-6,-5,-4,-3,-2,-1]:
    for t in [0,1,2,3]:
     self.MESH.faces[n].col[t].r = r 
     self.MESH.faces[n].col[t].g = v
     self.MESH.faces[n].col[t].b = b

 #-------------------------
 # fonction de multiplication
 #-------------------------
 def MULTIPLICATION(self): 
  #-------------------------
  # while pour éviter de gaspiller de la mémoire 
  # avec range qui cree la liste complete...
  #-------------------------
  while self.nx<self.maxx-1:
   self.nx+=self.deplacement_lateral
   self.ny=self.nz=-self.deplacement_lateral
   while self.ny<self.maxy-1:
     self.ny+=self.deplacement_lateral
     self.nz=-self.deplacement_lateral 
     while self.nz<self.maxz-1:
      self.nz+=self.deplacement_lateral
      self.Add_CUBE(self.nx,self.ny,self.nz)

 def Add_MATERIAU(self):
   #-------------------------
   # on cree un materiau
   #-------------------------
   if not self.MESH.materials:
      mat = Material.New()
      #-------------------------
      # on l'attache au mesh
      #-------------------------
      self.MESH.materials+=[mat]
      #-------------------------
      # on fait en sorte que les vertex 
      # color soient visibles
      #-------------------------
      mat.mode=54591623

#-------------------------
# Déclaration de l'objet cube
#-------------------------
cube=CUBE(1.0,2.0,5,0,'test')

#-------------------------
# appel des fonctions de l'objet
#-------------------------
cube.MULTIPLICATION()
cube.Add_MATERIAU()

Redraw() 


 
précédentpython : modèle cube
Script Python : spheres en masse 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