1
$\begingroup$

I'm looking for a help/solution to my question "How to stack UV island using Python in Blender.

I haven't got far from beginning but this is all I got this far:

import bpy

me = bpy.context.object.data
uv_layer = me.uv_layers.active.data

for poly in me.polygons:
    # Here I want to move the selected poly in the location of my 2D cursor
    # So eventually all my polygons are stacked into the same pile 

This is a very beginner question and all the help and hints are much appreciated.

------------------EDIT---------------------

Here's illustration what I want to achieve with the script:

  1. Select UV-islandenter image description here

  2. Snap UV-island to the 2D cursor enter image description here

  3. Repeat phases 1 and 2 for every face in the mesh, so eventually all the faces are stacked on top of each other enter image description here

$\endgroup$
6
  • $\begingroup$ Hi and welcome. So the question is about finding UVs? UVs islands? Selected UVs? And stack means here setting all islands in top of each other? If yes, have these islands the same shape? $\endgroup$ Commented Sep 17, 2020 at 17:27
  • $\begingroup$ Hi, @lemon so my problem more specifically is that first I want to separate every face of my mesh to separate UV island. Secondly I want to iterate through every UV island of my mesh and transform all the islands individually to the location of my 2D cursor. So, yes stack means all UV islands of my mesh on top of each other. And islands doesn't need to share same shape, it's not important here. I just want them to one place so afterwards it's is easier to drag islands from one stack to on top of the image. $\endgroup$ Commented Sep 17, 2020 at 17:46
  • $\begingroup$ If I understand well, the UV layer is already created, yes? or should the script do it? $\endgroup$ Commented Sep 17, 2020 at 17:52
  • $\begingroup$ I'm not sure if the UV layer is created correctly in my script but that was my aim. $\endgroup$ Commented Sep 17, 2020 at 17:58
  • 1
    $\begingroup$ mmm... still not sure to understand (my bad)... so you want all UV to overlap being stretched to bounds? If that should reset the UVs. bpy.ops.uv.reset()... ? If not, could you illustrate with some images (editing your question)? $\endgroup$ Commented Sep 17, 2020 at 18:07

1 Answer 1

3
$\begingroup$

Here we need three things:

  • Find the 2d cursor position
  • Check the mode we are in before processing the UV coordinates
  • Use the UV layer to find UV coordinates

For each face, once UV coordinates are found, calculate their center and offset the coordinates by the difference from the cursor.

Note that if no UV editor opened, won't find the cursor and does nothing.

Commented code:

import bpy
from mathutils import Vector


def find_cursor_location():
    # Look through area and find the first image editor
    for area in bpy.context.screen.areas:
        if area.type == 'IMAGE_EDITOR':
            return area.spaces.active.cursor_location
    return None

obj = bpy.context.object
cursor = find_cursor_location()

if cursor:
    #Check the mode as we cant do it in edit mode
    mode = obj.mode
    if mode != 'OBJECT':
        bpy.ops.object.mode_set(mode='OBJECT')
        
    me = obj.data #Need to get it here in case mode is changed
    uv_layer = me.uv_layers.active

    if uv_layer:
        for poly in me.polygons:
            # Get all Uv coordinates of the face
            uvs = [uv_layer.data[loop_index].uv for loop_index in poly.loop_indices]
            # Its center
            center = sum(uvs, Vector((0,0))) / len(uvs)
            # The needed offset
            delta = center - cursor
            # Shift UV coords
            for uv_data in [uv_layer.data[loop_index] for loop_index in poly.loop_indices]:
                uv_data.uv -= delta

    #Back to the mode we were in
    if mode != 'OBJECT':
        bpy.ops.object.mode_set(mode=mode)
$\endgroup$
1
  • $\begingroup$ This is awesome! Thanks for really beginner friendly commented script. This taught me so much. $\endgroup$ Commented Sep 18, 2020 at 18:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.