I'm trying to use python to make a very basic tool to add a new uv map to the selected object and uv unwrap it with a lightmap pack, then select the default uv map as the active one, change into object mode and hide the object. So far it gets up to un wrapping the object but then it stops and gives me the following error:

Traceback (most recent call last):
  File "/Applications/Unity 5/Projects/Cartoon Driving/BlenderAssets/Map 01/Map 01.blend/lightmap_creator.py", line 15, in execute
AttributeError: 'Mesh' object has no attribute 'active_index'

location: <unknown location>:-1

Here's my code:

import bpy
from bpy.types import Menu

class Fence_Operation(bpy.types.Operator):
    bl_label = "Lightmap"
    bl_idname = "lightmap.lightmap_panel"

    def execute(self, context):
        bpy.context.object.data.uv_textures["UVMap.001"].name = "Lightmap"
        bpy.context.object.data.active_index = 1

class IGLayoutDemoPanel(bpy.types.Panel):
    bl_label = "Lightmap"
    bl_idname = "lightmap.creator"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "data"

    def draw(self, context):
        layout = self.layout
        row = layout.row(align = True)
        scene = context.scene

        row.operator("lightmap.lightmap_panel", text = "Lightmap")

I don't quite understand how to fix this though, I just copied the code that came up in the Info panel when I actually did it, and dumped that into my python code. Why doesn't that work? Does anyone know how to fix it?


1 Answer 1



bpy.context.object.data.active_index = 1


context.object.data.uv_textures.active_index = 1

The mesh is not a collection and has no active_index, hence the error.

Also consider using the last in the collection rather than by name 'UVMap.001'

context.object.data.uv_textures[-1].name = "Lightmap"

To get on my soapbox replace bpy.context with context as context is passed via the parameters, eg def execute(self, context).


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .