import bpy
import bmesh
def findMinMax(listlist):
minU=None
minV=None
maxU=None
maxV = None
for list in listlist:
for uv in list:
if minU is None or uv[0]>maxU:
maxU = uv[0]
if minU is None or uv[0]<minU:
minU = uv[0]
if minV is None or uv[1]>maxV:
maxV = uv[1]
if minV is None or uv[1]<minV:
minV = uv[1]
return [ minU, maxU, minV, maxV ]
class Normalizer:
def __init__(self, min, max):
self.b = -min;
self.a = 1/(max-min)
def transform(self, val):
return (val+self.b) *self.a;
def expandUVToFit(mesh):
bm = bmesh.from_edit_mesh(mesh)
uvl = bm.loops.layers.uv[mesh.uv_layers.active_index]
minMax = findMinMax( [ [ le[uvl].uv for le in face.loops if le[uvl].select] for face in bm.faces if face.select])
print(minMax)
xn = Normalizer(minMax[0], minMax[1])
yn = Normalizer(minMax[2], minMax[3])
for face in bm.faces:
if not face.select:
continue
for le in face.loops:
if not le[uvl].select:
continue
le[uvl].uv[0] = xn.transform(le[uvl].uv[0])
le[uvl].uv[1] = yn.transform(le[uvl].uv[1])
bmesh.update_edit_mesh(mesh)
obj = bpy.context.active_object
expandUVToFit(obj.data)
|
Blender python API quick-start
Syntax highlighting by Pygments.