I have a dropdown that should show a list of materials available for the selected object. The current material should be the selected entry in the dropdown list. I've been looking through posts but can't find the answer to this simple question.

I can set the value through the console by doing bpy.context.scene.mydropdown = 'my selection'

but doing this anywhere inside of my script gives me an error: Writing to ID classes in this context is not allowed.

class SC_PT_Wall(SaunaCloudPanel, bpy.types.Panel):
    bl_idname = "SCPANEL_PT_Wall"
    bl_label = "Wall"

    def draw(self, context):
        layout = self.layout
        sc = context.scene.saunacloud
        obj = context.active_object
        layout.prop(obj, "sc_has_heater", text="Heaters")
        sc.matlist = obj.material_slots[0].material.name  #this causes an error
        layout.prop(sc,  'matlist')

class SC_Global_Props(PropertyGroup):
    def getMatList(self, context):
        obj = context.active_object
        type = obj['sc_type']
        lst = []
        for mat in bpy.data.materials:
            parts = mat.name.split("_")
            if parts[0]==type:
                lst.append((mat.name, mat.name[len(parts[0])+1:], "Tooltip baby!"))
        return lst
    def setMat(self, context):
        obj = context.active_object
        matName = context.scene.saunacloud.matlist
        mat = bpy.data.materials[matName]
        slots = obj.material_slots    
        if len(slots)!=1:
            print("Can't set material because '"+obj.name+"' has more than one material.")
            slots[0].material = mat
    matlist: EnumProperty(
        description="Tooltip for the Dropdownbox",

Ditch the Enum.

enter image description here

AFAIC Using an enum property to list the materials has been superseded by using a pointer property to the ID object type. In this case a material.

Custom search data to UILayout.prop_search

Search custom properties in objects on the scene (Python)

Have used this option here. Given the property group a material property, which is a pointer to blend material, or None if not set.

An update method on this property sets the objects active material to the sauna specific one chosen.

Added a boolean property to material type to flag (poll)) it as a "Sauna Cloud" material. The only materials that poll will have this set as true.

A select menu is drawn by the UI using a UILayout.prop(...) (commented out) or with the ID template. (Note a custom operator to add material can be set here, I took out the generic add material_)

Other alterations.

Drew the context objects material slots in the panel and an option to add a new one. Further to this would be to define your own UIList to add the "Is sauna material" checkbox in the list, add a filter etc.

Displayed the is sauna material checkbox, in panel and prepended it to Material Preview panel, by way of example.

Set the context members material (context.material only exists in the PROPERTIES area, otherwise needs to be set for material operators) which can used in your addons custom operators, with buttons in this layout, as

mat = context.material
sc_mat = context.sauna_cloud_material

or in hindsight sc = context.sauna_cloud.

Changed the namespace from SC to SAUNA and would go as far as to say even SAUNA_CLOUD The naming convention, _PT_ for panel type. is all about limiting name clashes.

import bpy
from bpy.props import PointerProperty, BoolProperty
from bpy.types import PropertyGroup

class SaunaCloudPanel:
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Sauna"
    def poll(cls, context):
        ao = context.active_object
        return ao and ao.type == 'MESH'

class SAUNA_PT_Wall(SaunaCloudPanel, bpy.types.Panel):
    bl_idname = "SAUNA_PT_Wall"
    bl_label = "Wall"

    def draw(self, context):
        layout = self.layout
        sc = context.scene.saunacloud
        ob = context.active_object
        mat = ob.active_material
        layout.context_pointer_set("material", mat)
        layout.context_pointer_set("sauna_cloud_material", sc.material)
        box = layout.box()
        box.prop(ob, "name")
        col = box.column()
        col.template_list("MATERIAL_UL_matslots", "", ob, "material_slots", ob, "active_material_index", rows=5)
        if mat:
            col.prop(mat, "is_sauna_cloud")
        #col.prop(sc,  'material', text=getattr(sc.material, "name", "None"))
        col.template_ID(sc, "material")

class SAUNA_Global_Props(PropertyGroup):
    def poll_material(self, material):
        return material.is_sauna_cloud

    def update_material(self, context):
        scene = self.id_data
        sc = self
        ob = context.object
        mat = self.material
        # debug print
        print(f"Update object: {ob.name} with material: {mat.name}")
        ob.active_material = mat

    material : PointerProperty(

classes = (

def draw_sc_material_checkbox(self, context):
    mat = getattr(context, "material", None)
    self.layout.label(text=mat.name if mat else "None")
    if mat:
        self.layout.prop(mat, "is_sauna_cloud")

def register():
    for cls in classes:
    bpy.types.Scene.saunacloud = bpy.props.PointerProperty(type=SAUNA_Global_Props)
    bpy.types.Material.is_sauna_cloud = BoolProperty()

def unregister():
    for cls in classes:

if __name__ == "__main__":

To answer the question, in an update or items method self is the propertygroup and self.id_data the ID object down the chain it belongs to. Scene in this case.

To get the enum value in either


Consider defining an enum and using same on material and object to poll instead of relying on naming convention.

enum = EnumProperty(... our types ...)

bpy.types.Material.sauna_type = enum
bpy.types.Object.sauna_type = enum

You can't set properties of ID objects in a draw method.

In the update method, to do same as above

ob.active_material = bpy.data.materials.get(sc.matname)

however IMO far better to poll a pointer. Import from bpy import context into the poll method to get context object. eg using matching enums,

def material_poll(self, material):
    from bpy import context
    ob = context.object
    return material.sauna_type == getattr(ob, "sauna_type", None) 

or via name slicing method used in question

  • $\begingroup$ Thank you for the effort you clearly put into this answer. I still don't know how to set the selected value of a dropdown though. I need this for more than just materials, that was just an example. I have a more stripped down example here: pasteall.org/u6UP All I want to know, is how do I set the selected value and have it change based on the selected object. $\endgroup$ Mar 8 at 0:20

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.