1
$\begingroup$

I need to create a searchable material dropdown that excludes grease pencil materials. However if I do it like this

row.prop_search(item, "material_selector", bpy.data, "materials")

then all the materials in scene get included in material dropdown. How can I give custom search elements to prop_search method instead of generic bpy.data?

$\endgroup$
0

1 Answer 1

2
$\begingroup$

Use a pointer property with a poll.

To distinguish a regular material from one for a gp object there is the handy attribute Material.is_grease_pencil

Similarly to https://blender.stackexchange.com/a/159155/15543 and using a poll method https://blender.stackexchange.com/a/101303/15543

Will narrow down the selection based and offer the same choices as the regular material drop down.

import bpy
from bpy.props import PointerProperty

def poll_material(self, material):
    return not material.is_grease_pencil

class TEST_PT_layout_panel(bpy.types.Panel):
    bl_label = "Prop Panels"
    bl_category = "Test Panel"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        scene = context.scene
        layout = self.layout
        
        col = layout.column()
        col.prop_search(scene, "prop", bpy.data, "materials")
        #or 
        col.prop(scene, "prop")


def register():
    bpy.types.Scene.prop = PointerProperty(
        type=bpy.types.Material,
        poll=poll_material,
        )
    bpy.utils.register_class(TEST_PT_layout_panel)


def unregister():
    bpy.utils.unregister_class(TEST_PT_layout_panel)
    del bpy.types.Scene.prop
    
if __name__ == "__main__":
    register()

Kinda related:

How to populate UIList with all material slot in scene? 2.8

$\endgroup$
1
  • $\begingroup$ Never got notified about your comment here (coming from blender.stackexchange.com/a/203312/31447) after reading your answer. I feel honored, thanks! However, I guess I'm too grumpy for that and don't like the lazy peps just asking something... I honestly was in hope you would participate. Regarding python questions as well as flagging dupes the reality is that nothing really changed after the election -> your vote, duartes vote, my vote, which is a bit disappointing IMO. We will see, hopefully that will change. Cheers $\endgroup$
    – brockmann
    Commented Nov 27, 2020 at 14:00

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