1
$\begingroup$

Let's say I have a simple plane in which I have created a UV with the "Smart UV Project":

enter image description here

I've then created this simple script to put the UV coordinates (x and y position) on each vertex:

import bpy
import bmesh
import json

selected_object = bpy.context.active_object

if selected_object is not None:
    uv_info_dict = {}

    if selected_object.type == 'MESH':
        mesh = selected_object.data

        bm = bmesh.new()
        bm.from_mesh(mesh)

        bm.verts.ensure_lookup_table()
        
        for face in bm.faces:
            for vertex in face.verts:
                
                uv_coords = []
                for layer_name, _ in mesh.uv_layers.items():
                    uv = mesh.uv_layers[layer_name].data[vertex.index].uv                    
                    uv_coords.append("{:.4f}_{:.4f}".format(uv.x, uv.y))

                uv_key = uv_coords[0]

                bpy.ops.object.text_add()
                text_object = bpy.context.active_object
                text_object.location = (vertex.co.x, vertex.co.y, vertex.co.z) 
                text_object.data.body = uv_key 
                text_object.scale = (0.1, 0.1, 0.1)       
            
    else:
        print("Selected object is not a mesh.")
else:
     print("No object selected.")

When I ran the script it showed something like this:

enter image description here

Where I was expecting to show something like this:

enter image description here

I've been stuck on this issue for a few days and I have no idea why is this having this behaviour. Maybe I'm not seeing things correctly...

Do you have any idea why the top right corner is not showing (1,1) ?

$\endgroup$
1
  • $\begingroup$ I recreated your setup and it gives me the same results. Scratching my head. I'm confused too. $\endgroup$
    – maddes8cht
    Sep 20, 2023 at 0:31

1 Answer 1

2
$\begingroup$

I have made the following experiment of changing the vertex indices of the plane, and you will notice that there is no obvious pattern to determine which vertex is used to map to which UV coordinate. However, with the code line text_object.location = (vertex.co.x, vertex.co.y, vertex.co.z), you are assuming that the 3D vertex positions directly correspond to the same corners as the 2D UV map, which is not always the case. I think this assumption overlooks the fact that UV unwrapping (You probably will have to ask the developers how they do the mapping), involves a more complex process of projecting 3D vertices onto the 2D UV space, which can result in varying mappings between vertices and UV coordinates across the mesh.

enter image description here

You could try using this script instead:

import bpy
import bmesh

selected_object = bpy.context.active_object

if selected_object is not None:

    if selected_object.type == 'MESH':
        mesh = selected_object.data

        bm = bmesh.new()
        bm.from_mesh(mesh)
        bm.verts.ensure_lookup_table()

        for face in bm.faces:
            for vertex in face.verts:
                uv_coords = []
                for layer_name, _ in mesh.uv_layers.items():
                    uv = mesh.uv_layers[layer_name].data[vertex.index].uv
                    uv_coords.append((uv.x, uv.y))

                uv = uv_coords[0]

                x = (uv[0] - 0.5) * selected_object.dimensions.x
                y = (uv[1] - 0.5) * selected_object.dimensions.y
                z = selected_object.location[1]

                bpy.ops.object.text_add()
                text_object = bpy.context.active_object
                text_object.location = (x, y, z)
                text_object.data.body = "{:.4f}, {:.4f}".format(uv[0], uv[1])
                text_object.scale = (0.1, 0.1, 0.1)

        bm.free()
    else:
        print("Selected object is not a mesh.")
else:
    print("No object selected.")
$\endgroup$
1
  • 1
    $\begingroup$ Thank you for your help, Harry, . I'll check this out. $\endgroup$
    – Goncalo
    Sep 20, 2023 at 18:37

You must log in to answer this question.

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