I was trying to recreate batFINGER's answer to this question How to get data and edit it on UV map edges?

The code looks like this:

import bpy
import bmesh

from collections import defaultdict
context = bpy.context
ob = context.object
me = ob.data
bm = bmesh.new()
uvbm = bmesh.new()

uv_layer = bm.loops.layers.uv.verify()
vert_index = uvbm.verts.layers.int.new("index")
face_index = uvbm.faces.layers.int.new("index")
# adjust uv coordinates
for face in bm.faces:
    fverts = []
    for loop in face.loops:
        uv = loop[uv_layer].uv
        v = uvbm.verts.new((uv.x, uv.y, 0))
        v[vert_index] = loop.vert.index
    f = bmesh.ops.contextual_create(uvbm, geom=fverts)["faces"].pop()
    f[face_index] = face.index

# remove doubles

bmesh.ops.remove_doubles(uvbm, verts=uvbm.verts, dist=1e-7)    
me = bpy.data.meshes.new("UVEdgeMesh")
ob = bpy.data.objects.new("UVEdgeMesh", me)
ob.show_wire = True

edge_pairs = defaultdict(list)
boundary_edges = [e for e in uvbm.edges if e.is_boundary]

for e in boundary_edges:
    key = tuple(sorted(v[vert_index] for v in e.verts))

for key, edges in edge_pairs.items():

    print(key, [e.index for e in edges]) 

    for e in edges:
        if not e.is_boundary:
        f = e.link_faces[0]
        p = (e.verts[0].co + e.verts[1].co) / 2
        p += (f.calc_center_median() - p) / 4
        bpy.ops.object.text_add(radius=0.04, location=p)
        bpy.context.object.data.body = f"{key}"

When I run this on the Blender Cube primitive it works as intended, no problem. But when I try to use bpy.ops.import_mesh.stl(filepath=test_object) it doesn't create the uvbm mesh correctly, the object exists without there being a corresponding 2D mesh like I get from the Blender Cube.

I cannot see what is different about the two cubes (other than mine being a different size). What could be causing this and how can I fix it?

  • 1
    $\begingroup$ Perhaps the imported cube doesn't have a UVLayer, unlike the default cube $\endgroup$ Apr 17 at 9:50
  • 1
    $\begingroup$ Ah! You're correct! I thought that the line "uv_layer = bm.loops.layers.uv.verify()" would create a UVlayer if one wasn't present, but adding "bpy.ops.mesh.uv_texture_add()" first fixed the problem, thank-you! $\endgroup$
    – Ciara
    Apr 17 at 10:18


You must log in to answer this question.

Browse other questions tagged .