Python and UVLayer

Hello, fellow blenderers…
I’m here trying to find enlightenment for using uvlayers when creating a new object via python scripting.

I’m developing a import plugin (two, actually), that uses UV layers. I’ve got most of it figured out, but I can’t seem to set the uvlayer property on material.

According to http://www.blender.org/documentation/245PythonDoc/Material.Material-class.html there is a uvlayer property on material (I tought it should be in texture), but I always get this error: AttributeError: ‘Blender Material’ object has no attribute ‘uvlayer’

Trying to see if the documentation is somehow wrong, I tried to assign the parameter on the texture, instead of the material, but I got the following: AttributeError: ‘Blender Texture’ object has no attribute ‘uvlayer’

Here’s some relevants chucks of code:


def createMat(texname):
  filename = "data/" + texname
  
  # texture
  texture = Texture.New(texname)
  texture.setType('Image')
  try:
    img = Image.Load(filename);
    texture.image = img
  except:
    print ("Can't find texture %s. You'll have to assign it on your own" % filename)

  mat = Material.New(texname)
  mat.setTexture(3, texture, Blender.Texture.TexCo.UV)
  
  return(mat)

(...)
  # NOTE: This is inside another function, that's why it's indented. That's not an error.
  # ===== MATERIALS =====
  # Assign vertexes to group
  mat = []
  # this holds the colbits for the mesh
  val = 0
  for i in range(rsm.getMeshTexCount(Mesh_ID)):
    name = texnames_utf[i]
  
    nodup = removeDup(vertgroups[i])
    me.assignVertsToGroup(name, nodup, 1.0, Blender.Mesh.AssignModes.REPLACE)
    
    # Create materal for that group
    x = createMat(name)
    # x.uvlayer = name # <<- raises error
    mat.append(x)
    val += 1 << i
    
  # assign materials to the object
  me.materials = mat
  ob_act.setMaterials(mat)
  ob_act.colbits = val
  
  # set uvlayers
  try:
    for i in range(rsm.getMeshTexCount(Mesh_ID)):
      me.materials[i].uvlayer = texnames_utf[i]
  except:
    print("ERROR: Can't set uvlayer names...Why? WHY!?")
  
  # ===== /MATERIALS =====

Can anyone give me a hand? I’m running out of ideas here… If more code is needed, I’ll gladly provide.

Now, that brings up another question: What if I have two textures in the same material using different uv maps?

Ack, documentation is wrong :confused: - uvlayer is an MTex attribute. will have to update docs.

Thanks!!! It worked!!

Like this:

def createMat(texname, path = ""):
  filename = "texture/" + texname.replace('\\','/')
  
  ffn = "%s%s" % (path, filename)
  
  #dn = filename.decode('utf-8')
  
  print filename
  #print dn
  
  # texture
  texture = Texture.New(texname)
  texture.setType('Image')
  # texture.uvlayer = texname
  try:
    img = Image.Load(ffn);
    #img = Image.Load(dn);
  except:
    print ("Can't find texture %s. You'll have to assign it on your own" % ffn)
    img = Image.New(ffn, 10, 10, 1)

  texture.image = img

  mat = Material.New(texname)
  mat.setTexture(3, texture, Blender.Texture.TexCo.UV)
  
  # TODO: Optimize this
  for m in mat.getTextures():
    if (m != None):
      m.uvlayer = texname
  
  return(mat)

Another question:
I create some uv layers (3, to be exact) using

me = bpy.data.meshes.new(name)
me.addUVLayer(name_utf)

Now, How do I set a image to this uv layer? I already have the image loaded into a image texture. When my import is done, I have to assign the images to the right uv layers. How can I do it with python? I couldn’t find it anywhere…

Thanks!

It depends on what to you want to assign.
If you want to assign to faces, then set the layer as active, and then assign the texture to all the faces you want to.

If you want to assign to material( I guess thats what you want to do) you set it in the mapto somewhere methinks ;). It’s badly documented, I often use the dir(…) function to get what’s in there.