1

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?

griips21
  • 13
  • 2

1 Answers1

2

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

batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • Never got notified about your comment here (coming from https://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 – brockmann Nov 27 '20 at 14:00