1
$\begingroup$

I have created an enum property that contains all UV maps of an object

uvs_enum: bpy.props.EnumProperty(
    name='UV Map',
    update=update_enum,
    items=get_uv_layouts,
    default=0
)

def get_uv_layouts(scene, context):
    mesh = context.object.data
    if mesh.__class__.__name__ == 'Mesh':
        list = []
        for i, layer in enumerate(mesh.uv_layers):
            list.append((layer.name, layer.name, ''))
        return list
    return []

using the update function I can save the selection to the object

def update_enum(self, context):
    cb_props = bpy.context.scene.cb_props
    bpy.context.active_object[UV_SCALE_MAP_NAME] = cb_props.uvs_enum

enter image description here My problem is that I can't find a way to set the enum to the selection I have saved in the object. Is it possible to save the enum to the object itself or to set the enum selection when the get_uv_layouts function is called? If there is a better way to select a UV map for every object that would also be perfect.

$\endgroup$

1 Answer 1

1
$\begingroup$

I think this is what your question means.

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Test"

    def draw(self, context):
        layout = self.layout

        layout.prop(context.scene, 'uv_map')


def register():
    bpy.utils.register_class(HelloWorldPanel)
    
    def uv_maps(self, context):
        if context.object and context.object.type == 'MESH':
            return [(uv.name, uv.name, '') for uv in context.object.data.uv_layers]
        else:
            return [('NONE', 'None', ''),]

    def update_uvmap(self, context):
        if context.object is None or context.object.data is None or not hasattr(context.object.data, "uv_layers"):
            return
        context.object.data.uv_layers.active = context.object.data.uv_layers[self.uv_map]

    bpy.types.Scene.uv_map = bpy.props.EnumProperty(
        name = 'UV Map',
        items = uv_maps,
        update = update_uvmap,
    )


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)
    
    del bpy.types.Scene.uv_map


if __name__ == "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ I'm not trying to change the active UV-map. This is for my addon where the user has to select a UV-map that is used for later processing. This has to be done on multiple objects, so when I select one of them I need the property to reflect the selection that was made before. $\endgroup$ Commented Nov 20, 2023 at 15:17

You must log in to answer this question.

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