That's acutally pretty close. You just have to assign a MeshUVLoopLayer
reference to MeshUVLoopLayer.active
according to the API. Also I'd suggest exclude the active object from the loop and test whether there is an active layer within the poll method:
import bpy
class OBJECT_OT_set_active_uv(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.set_active_uv_selection"
bl_label = "Set UV Layer on all Objects in Selection"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.active_object
return obj is not None and obj.type == 'MESH' and obj.data.uv_layers.active
def execute(self, context):
target_uv = context.active_object.data.uv_layers.active
for obj in context.selected_objects:
if obj.type == 'MESH' and obj != context.active_object:
if target_uv.name in obj.data.uv_layers.keys():
# Active UV Layer = Target Layer
obj.data.uv_layers.active = obj.data.uv_layers[target_uv.name]
obj.data.uv_layers[target_uv.name].active_render = True
return {'FINISHED'}
def draw_set_active_uv(self, context):
layout = self.layout
layout.operator(OBJECT_OT_set_active_uv.bl_idname)
def register():
bpy.utils.register_class(OBJECT_OT_set_active_uv)
bpy.types.DATA_PT_uv_texture.append(draw_set_active_uv)
def unregister():
bpy.utils.unregister_class(OBJECT_OT_set_active_uv)
bpy.types.DATA_PT_uv_texture.remove(draw_set_active_uv)
if __name__ == "__main__":
register()
# Test call
#bpy.ops.object.set_active_uv_selection()