Quick question...adding uv layer

I’m trying to write a function to check a mesh for a uv layer called “UVName”, if it exists, set it to active, if not create it and set it to active.

I’m new to python, and haven’t quite got methods, attributes and instance variables worked out yet and seem to struggle everytime on simple things!

The line that gives me problems is the one that actually adds a new channel for UV coordinates

docs: addUVLayer(name)

I’m trying mesh.addUVlayer(UVName) and just get the error:

AttributeError: ‘Blender Mesh’ object has no attribute ‘addUVlayer’

what am I doing wrong?


def add_GetUV(UVName):             
        # Checks selected visible meshes for a set of UVs called UVName.
        # If it exists set it to active, if it doesn't exist create it and set to active.
        scn = data.scenes.active
        for ob in scn.objects.context:
                if not ob.lib and ob.type == 'Mesh':    # object isn't from a library and is a mesh
 
                        me = ob.getData(mesh=1)        
                        me.faceUV = True                
                        UVLayers = me.getUVLayerNames()

                        #Check to see if there's already a set of UVs called UVName
                        found = 0
                        for UVL in UVLayers:
                                if UVL == UVName:
                                        found = 1
                                        me.activeUVLayer = UVName
                                        print "found"
                        if found == 0:    
                                 mesh.addUVlayer(UVName)
                                me.activeUVLayer = UVName


just a very quick try:

Shouldn’t the penultimate line be :
me.addUVLayer(UVName)

instead of:
mesh.add…

why “mesh” ?

Oops, that’s a typo frpom something else I was trying…

me.addUVLayer(UVName) doesn’t work either…

confused!

Weird:confused:

Here, this works.
Run with object(s) selected

from Blender import Window, sys 
import bpy 


def add_GetUV(sce, UVName):
    for ob in sce.objects.context:
        if (ob.type == 'Mesh'):
            me = ob.getData(mesh=1)
            me.faceUV = True
            UVLayers = me.getUVLayerNames()
            
            found = 0
            # check to see if UVLayer name already exists
            for UVL in UVLayers:
                if UVL == UVName:
                    found = 1
                    me.activeUVLayer = UVName
                    print "Found layer pre-existing"
            if (found == 0):
                me.addUVLayer(UVName)
                me.activeUVLayer = UVName


def main(): 
     
    # Gets the current scene, there can be many scenes in 1 blend file. 
    sce = bpy.data.scenes.active 
     
    Window.WaitCursor(1) 
    t = sys.time() 
     
    # Run the object editing function 
#    my_object_util(sce) 
    add_GetUV(sce, 'myUVLayer')
    
     
    # Timing the script is a good way to be aware on any speed hits when scripting 

    print 'My Script finished in %.2f seconds' % (sys.time()-t) 
    Window.WaitCursor(0) 
     
     
# This lets you can import the script without running it 
if __name__ == '__main__': 
    main() 
 

I made some small changes and it works now…:

 import Blender
from Blender import *
    
def add_GetUV(UVName):             
        # Checks selected visible meshes for a set of UVs called UVName.
        # If it exists set it to active, if it doesn't exist create it and set to active.
        scn = Scene.GetCurrent()
        for ob in scn.objects.context:
                if not ob.lib and ob.type == 'Mesh':    # object isn't from a library and is a mesh
 
                        me = ob.getData(mesh=1)        
                        me.faceUV = True                
                        UVLayers = me.getUVLayerNames()

                        #Check to see if there's already a set of UVs called UVName
                        found = 0
                        for UVL in UVLayers:
                                if UVL == UVName:
                                        found = 1
                                        me.activeUVLayer = UVName
                                        print "found"
                        if found == 0:    
                                me.addUVLayer(UVName)
                                me.activeUVLayer = UVName

add_GetUV('newLayer')

EDIT: oops a bit late, i didn’t see that somebody answered it already

Ahhh so the big mistake was how I got the scene…

Thanks for that, i thought I was going mad!

Oooh and a missing capital on me.addUVLayer(UVName)

Ooops!

Now all that works, How do I set which set of UVs a texture uses in python? (the field in the map input tab of the materials buttons)
for multitexturing where different texture layers use different coordinates

I can only see how to set a texture to use “UV”

does the api not support this?

Yes you can set this. its called the mtex.uvlayer I think, look up the mtex docs for 245

hmm, can’t find it in the docs… will try anyway…

Your documentation sir: http://www.zoo-logique.org/3D.Blender/scripts_python/API/Texture.MTex-class.html

Just have a look at the source code for getIPO setIPO or clearIPO for the way to get access to the mtex class.

S.