0
$\begingroup$

I want make two template_icon_view previews in text editor > templates > python > ui_previews_dynamic_enum.py in blender 2.8 or 2.79, but I do not understand how make two different regions of previews.

The original template script is simple, I made my library, but I want two or more library previews inside 1 panel with 2 different directories.

For example:

directory = wm.my_previews_dir

I want another:

directory_two =.....
$\endgroup$
2
$\begingroup$

I just made everything double:

import os
import bpy


def enum_previews_from_directory_items(self, context):
    """EnumProperty callback"""
    enum_items = []

    if context is None:
        return enum_items

    wm = context.window_manager
    directory = wm.my_previews_dir

    # Get the preview collection (defined in register func).
    pcoll = preview_collections["main"]

    if directory == pcoll.my_previews_dir:
        return pcoll.my_previews

    print("Scanning directory: %s" % directory)
    def_dir = os.path.dirname(bpy.data.filepath) + directory[1:]
    if directory and os.path.exists(def_dir):
        # Scan the directory for png files
        image_paths = []
        for fn in os.listdir(def_dir):
            if fn.lower().endswith(".png"):
                image_paths.append(fn)
        for i, name in enumerate(image_paths):
            # generates a thumbnail preview for a file.
            filepath = os.path.join(def_dir, name)
            thumb = pcoll.load(filepath, filepath, 'IMAGE')
            enum_items.append((name, name, "", thumb.icon_id, i))

    pcoll.my_previews = enum_items
    pcoll.my_previews_dir = directory
    return pcoll.my_previews

def enum_previews_from_directory_items2(self, context):
    """EnumProperty callback"""
    enum_items = []

    if context is None:
        return enum_items

    wm = context.window_manager
    directory = wm.my_previews_dir2

    # Get the preview collection (defined in register func).
    pcoll = preview_collections["main2"]

    if directory == pcoll.my_previews_dir2:
        return pcoll.my_previews2

    print("Scanning directory: %s" % directory)
    def_dir = os.path.dirname(bpy.data.filepath) + directory[1:]
    if directory and os.path.exists(def_dir):
        # Scan the directory for png files
        image_paths = []
        for fn in os.listdir(def_dir):
            if fn.lower().endswith(".png"):
                image_paths.append(fn)
        for i, name in enumerate(image_paths):
            # generates a thumbnail preview for a file.
            filepath = os.path.join(def_dir, name)
            thumb = pcoll.load(filepath, filepath, 'IMAGE')
            enum_items.append((name, name, "", thumb.icon_id, i))

    pcoll.my_previews2 = enum_items
    pcoll.my_previews_dir2 = directory
    return pcoll.my_previews2

class PreviewsExamplePanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Previews Example Panel"
    bl_idname = "OBJECT_PT_previews"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

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

        row = layout.row()
        row.prop(wm, "my_previews_dir")

        row = layout.row()
        row.template_icon_view(wm, "my_previews")

        row = layout.row()
        row.prop(wm, "my_previews")

        row = layout.row()
        row.prop(wm, "my_previews_dir2")

        row = layout.row()
        row.template_icon_view(wm, "my_previews2")

        row = layout.row()
        row.prop(wm, "my_previews2")


# We can store multiple preview collections here,
# however in this example we only store "main"
preview_collections = {}


def register():
    from bpy.types import WindowManager
    from bpy.props import (
            StringProperty,
            EnumProperty,
            )

    WindowManager.my_previews_dir = StringProperty(
            name="Folder Path",
            subtype='DIR_PATH',
            default=""
            )

    WindowManager.my_previews = EnumProperty(
            items=enum_previews_from_directory_items,
            )
    WindowManager.my_previews_dir2 = StringProperty(
            name="Folder Path",
            subtype='DIR_PATH',
            default=""
            )

    WindowManager.my_previews2 = EnumProperty(
            items=enum_previews_from_directory_items2,
            )

    # Note that preview collections returned by bpy.utils.previews
    # are regular Python objects - you can use them to store custom data.
    #
    # This is especially useful here, since:
    # - It avoids us regenerating the whole enum over and over.
    # - It can store enum_items' strings
    #   (remember you have to keep those strings somewhere in py,
    #   else they get freed and Blender references invalid memory!).
    import bpy.utils.previews
    pcoll = bpy.utils.previews.new()
    pcoll.my_previews_dir = ""
    pcoll.my_previews = ()
    pcoll2 = bpy.utils.previews.new()
    pcoll2.my_previews_dir2 = ""
    pcoll2.my_previews2 = ()

    preview_collections["main"] = pcoll
    preview_collections["main2"] = pcoll2

    bpy.utils.register_class(PreviewsExamplePanel)


def unregister():
    from bpy.types import WindowManager

    del WindowManager.my_previews

    for pcoll in preview_collections.values():
        bpy.utils.previews.remove(pcoll)
    preview_collections.clear()

    bpy.utils.unregister_class(PreviewsExamplePanel)


if __name__ == "__main__":
    register()

I am sure It can be done more professionally, but it's good starting point. If You want more than two You should reed about collections

$\endgroup$
  • $\begingroup$ Nice i try it and work. Nice trick $\endgroup$ – Pastrokkio Feb 10 at 10:14
  • $\begingroup$ Seems to have problems because it does not load any image, not even if I put a specific directory in C: do you give this problem to you too? $\endgroup$ – Pastrokkio Feb 10 at 16:37
  • 1
    $\begingroup$ I am working on Linux and I had the same problem. I solved id by adding print(def_dir) almost everywhere, and print(os.path.exists(def_dir)) - has to be true. Just edit code until def_dir will be correct. And make sure Your images are .png. $\endgroup$ – Piotr Kowalczyk Feb 11 at 8:24

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.