Setting active uv_texture in Python API

Hi guys,

I’m struggling with a little problem. I am trying to use the python api to change the active texture.
Being naive, I just say

obj.data.uv_textures.active=uvmap_target

where uvmap_target is a MeshTexturePolyLayer

However, this doesn’t actually change the active uv_texture, so apparently, uv_textures.active is readonly. I have tried changing it both while being in object mode and being in edit mode, but neither works.

So, how am I supposed to do this?

Thanks!

Seems to work for me?!

t = bpy.context.object.data.uv_textures
>>> t.active
bpy.data.meshes[‘Cube’].uv_textures[“UVMap”]

>>> t.active = t[‘foo’]

>>> t.active
bpy.data.meshes[‘Cube’].uv_textures[“foo”]

Object, as well as edit mode.

I see that you are doing this in the console. I am doing this in an operator and I am using the context that comes as an argument to the operator as opposed to the global bpy.context. Could this be an issue?

works fine, and it shouldn’t matter whether bpy.context or context as passed to the operator

import bpy



class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.object is not None

    def execute(self, context):
        ob = context.object
        uv_tex = ob.data.uv_textures
        uv_tex.active = uv_tex.new("foo")
        self.report({'INFO'}, uv_tex.active.name)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()


Hmm. Perhaps I’m overlooking something. I will try and see what goes wrong tonight and let you know how I screwed up. Thanks once again. You will get a big acknowledgement when I get this addon working the way I want to…

Hi CoDEmanX,

I got it working. For some reason the way I did it (obj.data.uv_textures.active=uvmap_target), does not work, but if I follow your way directly, it does work. So changing the active texture of a certain object should be done, but rather, the bpy.context.object.data.uv_textures should be used. So thanks again! My script now works!

It shouldn’t matter whether you use bpy.context.object or an object reference to retrieved earlier. It must use a uv texture of that object however, maybe you tried to assign a uv texture reference from another object?

I have only one object in the test scene, so that is unlikely. If you want I can try to isolate the problem further. Right now I’m just happy that it works in my code, but I can easily produce a version that does not work. As said, it seems to only occur when doing this in an operator. If you say it shouldn’t matter, I’m either doing something wrong somewhere else, or I have clumsely stumbled upon a bug.