Blender (jusqu'à 2.49)
Python
Astuces pour utiliser des listes 
comme éléments références de dictionnaires
    Début   Index
précédentScript Python
Script boundbox Suivant

Astuces pour utiliser des listes comme éléments références de dictionnaires 

Ce script a pour objectif de comparer les couleurs d'une liste de  materials récupérés sur un objet mesh  avec celles d'un dictionnaire et de remplacer le material portant la couleurs par celui qui lui est associé . 
 

import Blender
# ---------------------------
# Dictionnary Object table 
# --------------------------- 
#  2 entries  : 
#  -->  first the list of rvb colors, 3 floats rounded to 0.001
#       as lists are not "hashable", and can not be used  in a dictionnary,
#       they are translated into strings .
#       CAUTION : always 3 digits at the right of the point, 
#       not "1.0" but "1.000".
#
#  -->  in second the material names 

#         'rvb color'       : 'metal name'
# ---------------------------
TABLE={ 
         '0.637,1.000,1.000': 'metal_', 
         '0.001,0.005,1.000': 'metal_02',
         '0.001,0.005,1.000': 'metal_03',
         '0.001,0.005,1.000': 'metal_04', 
         '0.001,0.005,1.000': 'metal_05',
         '0.001,0.005,1.000': 'metal_06',
         '0.001,0.005,1.000': 'metal_07',
         '0.001,0.005,1.000': 'metal_08',
         '0.001,0.005,1.000': 'metal_09',
         '0.001,0.005,1.000': 'metal_10',
         '0.001,0.005,1.000': 'metal_11',
         '0.001,0.005,1.000': 'metal_12',
         '0.001,0.005,1.000': 'metal_13',
         '0.001,0.005,1.000': 'metal_14' 
 }

# ----------------------------
# Get all the meshes : 
# ---------------------------- 
ME=[m for m in Blender.Object.Get() if m.getType()=='Mesh']

# ----------------------------
# in this list find ...
# ---------------------------- 
for m in ME:
   # ----------------------------
   # ... if, in the data ,...
   # ---------------------------- 
   me=m.getData() 
   # ----------------------------
   # ...the list of material contents ...
   # ---------------------------- 
   for c in me.materials :
    # ----------------------------
       # ... a color then translate this color in a usable string .
       # ---------------------------- 
       color ="%.3f,%.3f,%.3f"%(round(c.rgbCol[0],3),round(c.rgbCol[1],3),round(c.rgbCol[2],3)) 
       print color,TABLE.keys() 
    # ----------------------------
       # Compare this string with the dictionnary keys
       # ---------------------------- 
       if color in TABLE.keys():
            # ----------------------------
            #  if an identical color exists, replace the material by the other 
            # ---------------------------- 
            me.materials[me.materials.index(c)]=Blender.Material.Get(TABLE[color])
   #------------
   # replace the original mesh bu the modified one .
   #------------
   me.update()
Blender.Window.RedrawAll()
 


 
précédentScript Python
Script boundbox 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