Rotate the uv's of a selected object instead of the object itself.

I wanted to write a quick script to go over a selection of objects, unwrap them (smartunwrap) and rotate those UV’s 45 degrees.
That’s all I want it to do. However I seem to have run into a pinch. Whenever I try to rotate the UV’s in my script I’m instead rotating the objects themselves.

This is bassicly what I do in my script ( within my for loop over my selection:


    bpy.ops.object.editmode_toggle()
    # select everything
    bpy.ops.mesh.select_all(action='SELECT')

    #unwrap
    bpy.ops.uv.smart_project(angle_limit=66, island_margin=0, user_area_weight=0)
    bpy.ops.uv.select_all(action='SELECT')

    # rotate
    bpy.ops.transform.rotate(value=(0.785398,), axis=(0,0,-1),constraint_axis=  (False,False,False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0,0,0), snap_align=False, snap_normal=(0,0,0), release_confirm=False)

    # exit edit mode
    bpy.ops.object.editmode_toggle()

How do I make sure blender rotates the uv’s?

you need to either overwrite context, or change area type to ‘IMAGE_EDITOR’:

import bpy
from math import radians

last_area = bpy.context.area.type
bpy.context.area.type = 'IMAGE_EDITOR'

# you need to make sure here, that object is in edit mode and UVs are selected or it will crash!

bpy.ops.transform.rotate(value=(radians(45),))

bpy.context.area.type = last_area

Nice!

:cool: