1
$\begingroup$

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:

bpy.ops.object.hide_view_set(unselected=False)
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.ops.mesh.uv_texture_add()
        bpy.ops.object.editmode_toggle()
        bpy.context.object.data.uv_textures["UVMap.001"].name = "Lightmap"
        bpy.ops.mesh.select_all(action='SELECT')
        bpy.ops.uv.lightmap_pack()
        bpy.ops.mesh.select_all(action='DESELECT')
        bpy.context.object.data.active_index = 1
        bpy.ops.object.hide_view_set(unselected=False)


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?

$\endgroup$
4
$\begingroup$

Replace

bpy.context.object.data.active_index = 1

with

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).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.