0
$\begingroup$

I was looking at this uilist, but I understand very little about how to replace materials with objects:

class MATERIAL_UL_Example(bpy.types.UIList):

    def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
        ob = data
        slot = item
        ma = slot.material


        if self.layout_type in {'DEFAULT', 'COMPACT'}:
            if ma:               
                layout.prop(ma, "name", text= "", emboss=False, icon_value=icon)

and in the panel class:

row.template_list("MATERIAL_UL_Example", "", ob, "material_slots", ob, "active_material_index",rows = 3)

I was wondering how to compile a list for some objects in the scene, I would need to compile objects based on a string property or by name, something like:

for o in scene.object:
    if "My ob name" in o.name:
        #put this object into the list
$\endgroup$
2
$\begingroup$

Edit of previous.

enter image description here

From the related answer to one of your previous question, edited to display all objects in the scene collection. (Class names still reflect material based nature of original)

Compare to original, and you'll see it's minimal change to display one collection or another.

import bpy

from bpy.types import PropertyGroup

from bpy.props import (
    CollectionProperty,
    IntProperty,
    BoolProperty,
    StringProperty,
    PointerProperty,
)


class MATERIAL_UL_extreme_matslot(bpy.types.UIList):

    def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
        scene = data
        ob = item
        #print(data, item, active_data, active_propname)


        if self.layout_type in {'DEFAULT', 'COMPACT'}:

            layout.prop(ob, "name", text="", emboss=False, icon_value=layout.icon(ob))


class SCENE_PT_materials(bpy.types.Panel):

    bl_label = "My label"
    bl_idname = "SCENE_PT_materials"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "My Category"

    def draw(self, context):

        scn = context.scene
        layout = self.layout
        col = layout.column()
        col.template_list(
            "MATERIAL_UL_extreme_matslot",
            "",
            scn,
            "objects",
            scn,
            "active_object_index")


classes = (,
           MATERIAL_UL_extreme_matslot,
           SCENE_PT_materials)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.active_object_index = IntProperty()


def unregister():
    # fill this in.
    pass


if __name__ == "__main__":
    register()

To sum up the changes, The object that owns the collection is scene the collection "objects" -> scene.objects its item will be an object.

The active index is scene.active_object_index which I haven't set to anything. Something like

scene.objects[:].index(context.object)

will set it to the active object.

$\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.