Blender (jusqu'à 2.49)
Python :
to Create and UVmap a mesh
and
to blend the available UV textures 
in the current file .
(french version)
    Beginning   Index
previous  Script load image
script python : Next

 
1/ Create and UVmap a mesh
2/ blend the available UV textures
3/ How can we know if a vertex is selected in the UVeditor window ?

Creating and UVmapping a mesh
In this script version, it would be possible to assign one and single material to the complete mesh.  In fact, this material is created in the script itself .
 

#this seems to work

iimport Blender
from Blender import *

me=NMesh.GetRaw()
v=NMesh.Vert(1.0,0.0,0.0)
me.verts.append(v)
v=NMesh.Vert(1.0,1.0,0.0)
me.verts.append(v)
v=NMesh.Vert(0.0,1.0,0.0)
me.verts.append(v)
v=NMesh.Vert(0.0,0.0,0.0)
me.verts.append(v)

f=NMesh.Face()
f.v.append(me.verts[0])
f.v.append(me.verts[1])
f.v.append(me.verts[2])
f.v.append(me.verts[3])

uv=[]

uv.append( (0.0,0.0))
uv.append( (1.0,0.0))
uv.append( (1.0,1.0))
uv.append( (0.0,1.0))

f.uv=uv

im=Image.Load('f:/maincrystalpalace3.jpg')
f.image = im

if Blender.Get('version')>=228:
     C = NMesh.FaceModes
else:
     C = NMesh.Const

f.mode=C['LIGHT']
f.mode|=C['TEX']
f.mode|=C['TWOSIDE']

me.faces.append(f)
 

newmat = Material.New('newmat')
newmat.mode = newmat.mode  | 2048
me.materials.append(newmat) 

#
#f.mat=1

f.transp=NMesh.FaceTranspModes['SOLID']
f.flag=NMesh.FaceFlags['ACTIVE']

tex = Texture.New() 
tex.setType('Image') 
tex.image = im 
newmat.setTexture(0, tex) 
mtex = newmat.getTextures()
mtex[0].texco=Texture.TexCo.UV

NMesh.PutRaw(me,"Plane.002")
Blender.Redraw()

Blending available UV textures in the current blender file .
Download the script 
 

#!BPY

"""
Name: 'UV Texture Randomizer'
Blender: 232
Group: 'UV'
Tooltip: 'a script to blend the uv textures with random'
""" 
__author__ = "Jean-Michel Soler (jms)"
__url__ = ("""Script's homepage, http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_python_uvmap.htm""",
"""Communicate problems and errors, http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender""")
__version__ = "11/2004"
__bpydoc__ = """
This script blends all the uvtextures of the current file face by face.

Usage:

   run the script from the UV Scripts Menu. All UV textures will be collected and 
   connected by face.
""" 

#-------------------------------------------------
# script pour melanger les texture uv sur l'ensemble des
# mesh .
#-------------------------------------------------

import Blender
IMAGES=Blender.Image.Get()

#-------------------------------------------------
# BY_OBJECT=0 
#   textures blended by faces
#   textures melangees par faces
# BY_OBJECT=1 
#  textures blended by object
#   textures melangees par object
#---------------------------------------------------
BY_OBJECT=1

#---------------------------------------------------
# Fonction :
# Tester les objets constitues de segment sans aucune 
# face exportable. Retourne faux si au moins une face
# est un triangle ou un quad
#---------------------------------------------------
def onlysegment(o): 
    for f in o.getData().faces:
        if len(f.v)>2:
           return 0
    return 1

#---------------------------------------------------
# Fonction :
# detecte la presence de coordonnee uv 
#---------------------------------------------------
def hasUV(m):
    for f in m.faces:
        #print f.uv
        if f.uv:
              return 1 
    return 0
#---------------------------------------------------
# Operation :
# Creer la liste des objets meshes 
#---------------------------------------------------
OBJ=[o for o  in Blender.Object.Get() 
           if o.getType()== 'Mesh' and 
              (o.getData().faces!=[] or 
                    not onlysegment(o))]

print OBJ

if Blender.Get('version')>=228:
     C = Blender.NMesh.FaceModes
else:
     C = Blender.NMesh.Const

for o in OBJ:
  m=o.getData()
  if hasUV(m) ==1 :
    if BY_OBJECT==1:
       val=int(Blender.Noise.random()*len(IMAGES))
       img=IMAGES[val]
       print img, val
    for f in m.faces:
        if BY_OBJECT==0:
           img=IMAGES[int(Blender.Noise.random()*len(IMAGES))]
           print img
        f.image=img
        f.mode=C['LIGHT']+C['TEX']+C['TWOSIDE']
    m.update()
Blender.Redraw() 

3/ How can we know if a vertex is selected in the UVeditor window ?

At this date,   october, 1er,  2005, there is nothing in the API documentation that shows clearly how to proceed .. However we can imagine that the information about this problem must be easily reachable.  What we know is : UV co-ordinates are linked to the faces and the faces are equipped with a flag  which allows to know if  they are selected or hidden or displayed.

The Blender.NMesh.FaceFlags dictionary gives only 3 values, one for each possibility.  We can easily display them with the following script :
 

import Blender
for k in dir(Blender.NMesh.FaceFlags):
  a=Blender.NMesh.FaceFlags[k]
  print k,' : '
  for b in  [(a & 1<<n)>>n for n in range(15,-1,-1)]:print b,
  print 

 
Caution:  observe how the keys of this dictionary are obtained:
dir(Blender.NMesh.FaceFlags)
normally it should be Blender.NMesh.FaceFlags.keys() but this fonction returns an error.
Results
ACTIVE  :  2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
HIDE  :  64
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
SELECT  :  1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

As these data only take 3 bits out of the 16 available. We can wonder what blender does of the remainder.  It only remains to test each case and observe what the following script will display
 

import Blender
from Blender import NMesh

ME=Blender.Object.Get('Sphere').getData()
print dir(ME.faces[0].uv) 
SEL = NMesh.FaceFlags['SELECT']
FA=[f for f in ME.faces if (f.flag & SEL)]
a=FA[0].flag 
for b in  [(a & 1<<n)>>n for n in range(15,-1,-1)]:print b,

Case number 1 : none of the vertex selected
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
Case number 2 : only one vertex selected 
0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1
Case number 3 : two vertex selected 
0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1
Case number 3 : 3  three vertex selected
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1

Obviously the bits 2 to 5 are used to mark the selection of a vertex in the UVeditor window.  It only remains to determine which bit corresponds to which vertex in the continuation of UV coordinates. We slightly modify the scripts to display UV coordinates in the order:
 

import Blender
from Blender import NMesh

ME=Blender.Object.Get('Sphere').getData()
print dir(ME.faces[0].uv) 
SEL = NMesh.FaceFlags['SELECT']
FA=[f for f in ME.faces if (f.flag & SEL)]
print FA[0].flag 
a=FA[0].flag 
for b in  [(a & 1<<n)>>n for n in range(15,-1,-1)]:print b,
print
n=0
for a,b in FA[0].uv:
 print n,'uv : ', a*256,' X'
 print n,'uv : ', b*256,' Y' 
 n+=1
 print
 

and we compare with the values displayed in Properties when we press on key N :

previous  Script load image
 script python : 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