1
$\begingroup$

I have a blend file with a mesh and its corresponding UV map. I'm trying to scale the UV map programmatically with python script, but when I use bpy.ops.transform.resize it scales the mesh, not the UV map. I wasn't able to find anything that scales UV map in bpy.ops.uv.

Is this even doable?

$\endgroup$

1 Answer 1

9
$\begingroup$

I have not found a built in function either.

Here is a commented code to do it if that can help:

import bpy
from mathutils import Vector

#Get object and UV map given their names
def GetObjectAndUVMap( objName, uvMapName ):
    try:
        obj = bpy.data.objects[objName]

        if obj.type == 'MESH':
            uvMap = obj.data.uv_layers[uvMapName]
            return obj, uvMap
    except:
        pass

    return None, None

#Scale a 2D vector v, considering a scale s and a pivot point p
def Scale2D( v, s, p ):
    return ( p[0] + s[0]*(v[0] - p[0]), p[1] + s[1]*(v[1] - p[1]) )     

#Scale a UV map iterating over its coordinates to a given scale and with a pivot point
def ScaleUV( uvMap, scale, pivot ):
    for uvIndex in range( len(uvMap.data) ):
        uvMap.data[uvIndex].uv = Scale2D( uvMap.data[uvIndex].uv, scale, pivot )

#UV data are not accessible in edit mode
bpy.ops.object.mode_set(mode='OBJECT')

#The names of the object and map
objName = 'Cube'
uvMapName = 'UVMap'

#Defines the pivot and scale
pivot = Vector( (0.5, 0.5) )
scale = Vector( (2, 2) )

#Get the object from names
obj, uvMap = GetObjectAndUVMap( objName, uvMapName )

#If the object is found, scale its UV map
if obj is not None:
    ScaleUV( uvMap, scale, pivot )
$\endgroup$
1
  • $\begingroup$ I'm surprised to be honest. So far during my endeavours with Blender, all the "pick" functions always had an equivalent for running them in a non interactive environment. In any case, your solution works perfectly, thanks! :) $\endgroup$ Commented Mar 7, 2017 at 17:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .