__author__ = 'thoth'
import bpy
import bmesh
from math import *
from mathutils import *
def set_uvs_for_face(bm, fi, uv_layer):
face = bm.faces[fi]
normal = face.normal
dx=abs(normal[0])
dy=abs(normal[1])
dz=abs(normal[2])
if (dz > dx):
u = Vector([1,0,0])
if (dz>dy):
v = Vector([0,1,0])
else:
v = Vector([0,0,1])
else:
v = Vector([0,0,1])
if dx>dy:
u = Vector([0,1,0])
else:
u = Vector([1,0,0])
for i in range(len(face.loops)):
l = face.loops[i]
l[uv_layer].uv = [ u.dot(l.vert.co),
v.dot(l.vert.co)]
def set_uvs(mesh, name=None):
if name is None:
if 0<len(mesh.uv_textures):
uv = mesh.uv_textures[0]
else:
uv = mesh.uv_textures.new("cubic")
else:
uv = mesh.uv_textures.get(name)
if uv is None:
uv = mesh.uv_textures.new(name)
bm = bmesh.new()
bm.from_mesh(mesh)
bm.faces.ensure_lookup_table()
uv_layer = bm.loops.layers.uv[uv.name]
for fi in range(len(bm.faces)):
set_uvs_for_face(bm, fi, uv_layer)
bm.to_mesh(mesh)
#
#
#
mode = bpy.context.mode
if 'OBJECT' != mode:
bpy.ops.object.mode_set(mode='OBJECT')
obj = bpy.context.active_object
set_uvs(obj.data)
if 'OBJECT' != mode:
bpy.ops.object.mode_set(mode = mode)
#
|
Blender python API quick-start
Syntax highlighting by Pygments.