Adding an empty UV map to a mesh

add-ons
texturing

(John Nagle) #1

I’m writing an automatic impostor generator. You fit a box, or other mesh object, around the objects to be impostored, run my “Impostor Maker” add on, and you get an impostor. The source objects are rendered in ortho mode looking straight at each face. Those textures are composited into a big image. UVs are constructed to map the texture onto the target object. Intended use is low-LOD models for Second Life, which are usually terrible.

All that works. See screenshot below. On the left, a 3D model with 3163 triangles. On the right, an impostor with 12 triangles. The one thing I can’t do yet is create UVs where none existed. I have to unwrap from the UI first to create the UV arrays so I can overwrite them. Does anyone know how to do that?

Previous discussion in 2016. Result of discussion - nobody found the answer, although many things were tried.


(kkar) #2

You might be able to use Camera uv projection modifier and then apply it, in the code if you need to do it in python use “bpy.ops.mesh.uv_texture_add()” , unless I misunderstood it :slight_smile:


(John Nagle) #3

It turns out to be really easy, but not documented anywhere I can find it.

Add an empty UV layer:

    me = obj.data     # mesh info
    if not me.uv_layers.active : # if no UV layer
        me.uv_texture_add()     # add UV layer

The data layer functions don’t seem to be well documented, but usually correspond to the user level “ops” operators. A few others I’ve been able to make work:

Create an image object:

    image = bpy.data.images.new(name=name, 
        width=width, height=height, alpha=True) 

Unlike the operator level version, there’s no “color” parameter, so if you need to clear the image to something, that has to be done separately.

Is the data layer documented somewhere? Mostly I’m finding snippets here and on Stack Overflow that hint at how to use it. Yet it’s clearly the way to do things when you need to link up textures, materials, images, nodes, etc.


(John Nagle) #4

Oops, it turns out

me.uv_texture_add()     # add UV layer

won’t work. Back to dumb old

    oldactive = bpy.context.scene.objects.active
    bpy.context.scene.objects.active = target
    bpy.ops.mesh.uv_texture_add()
    bpy.context.scene.objects.active = oldactive

(iceythe) #5

You almost had it, but you should use object.data.uv_textures.new() to add UV layers instead.

import bpy
C = bpy.context

for o in C.selected_objects:
    if o.type == 'MESH':
        if len(o.data.uv_layers) < 1:
            o.data.uv_textures.new()

The python console in Blender has auto-complete if you need to find your way around bpy.data