Created
October 24, 2023 09:52
-
-
Save tin2tin/ecb86ce5383c84c606918b94fc5e3089 to your computer and use it in GitHub Desktop.
Add files to uilist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bl_info = { | |
"name": "LoRA Browser", | |
"blender": (2, 80, 0), | |
"category": "Object", | |
} | |
import bpy | |
import os | |
# Custom property to store the list of files | |
class LORABrowserFileItem(bpy.types.PropertyGroup): | |
name: bpy.props.StringProperty() | |
enabled: bpy.props.BoolProperty(default=True) | |
weight_value: bpy.props.FloatProperty(default=1.0) | |
index: bpy.props.IntProperty(name="Index", default=0) | |
# Custom UIList | |
class LORABROWSER_UL_files(bpy.types.UIList): | |
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index): | |
row = layout.row(align=True) | |
row.prop(item, "enabled", text="") | |
split = row.split(factor=0.7) | |
split.label(text=item.name) | |
split.prop(item, "weight_value", text="", emboss=False) | |
# UI Panel | |
class LORABROWSER_PT_main_panel(bpy.types.Panel): | |
bl_label = "LoRA Browser" | |
bl_idname = "LORABROWSER_PT_main_panel" | |
bl_space_type = 'SEQUENCE_EDITOR' | |
bl_region_type = 'UI' | |
bl_category = 'LoRA Browser' | |
def draw(self, context): | |
layout = self.layout | |
scene = context.scene | |
# Folder selection and refresh button | |
row = layout.row(align=True) | |
row.prop(scene, "lora_folder", text="Folder") | |
row.operator("lora.refresh_files", text="", icon="FILE_REFRESH") | |
# Custom UIList | |
lora_files = scene.lora_files | |
list_len = len(lora_files) | |
if list_len > 0: | |
layout.template_list("LORABROWSER_UL_files", "The_List", scene, "lora_files", scene, "lora_files_index") | |
if list_len == 0: | |
layout.label(text="No .safetensor files found in the selected folder.") | |
# Button to print items | |
layout.operator("lora.print_items", text="Print Items") | |
class LORA_OT_RefreshFiles(bpy.types.Operator): | |
bl_idname = "lora.refresh_files" | |
bl_label = "Refresh Files" | |
def execute(self, context): | |
scene = context.scene | |
directory = scene.lora_folder | |
if not directory: | |
self.report({'ERROR'}, "No folder selected") | |
return {'CANCELLED'} | |
lora_files = scene.lora_files | |
lora_files.clear() | |
for filename in os.listdir(directory): | |
if filename.endswith(".safetensors"): | |
file_item = lora_files.add() | |
file_item.name = filename.replace(".safetensors", "") | |
file_item.enabled = True | |
file_item.weight_value = 1.0 | |
else: | |
print(filename) | |
return {'FINISHED'} | |
class LORA_OT_PrintItems(bpy.types.Operator): | |
bl_idname = "lora.print_items" | |
bl_label = "Print Items" | |
def execute(self, context): | |
scene = context.scene | |
lora_files = scene.lora_files | |
for item in lora_files: | |
print(f"Name: {item.name}, Enabled: {item.enabled}, Weight: {item.weight_value}") | |
return {'FINISHED'} | |
# Register the classes | |
def register(): | |
bpy.utils.register_class(LORABrowserFileItem) | |
bpy.utils.register_class(LORABROWSER_PT_main_panel) | |
bpy.utils.register_class(LORA_OT_RefreshFiles) | |
bpy.utils.register_class(LORABROWSER_UL_files) | |
bpy.utils.register_class(LORA_OT_PrintItems) | |
bpy.types.Scene.lora_files = bpy.props.CollectionProperty(type=LORABrowserFileItem) | |
bpy.types.Scene.lora_files_index = bpy.props.IntProperty(name="Index", default=0) | |
# Create a default folder value | |
bpy.types.Scene.lora_folder = bpy.props.StringProperty( | |
name="Folder", | |
description="Select a folder", | |
subtype='DIR_PATH', | |
default="", # Set a default folder value | |
update=update_folder_callback, | |
) | |
# Unregister the classes | |
def unregister(): | |
bpy.utils.unregister_class(LORABROWSER_PT_main_panel) | |
bpy.utils.unregister_class(LORA_OT_RefreshFiles) | |
bpy.utils.unregister_class(LORABrowserFileItem) | |
bpy.utils.unregister_class(LORABROWSER_UL_files) | |
bpy.utils.unregister_class(LORA_OT_PrintItems) | |
# Function to update the file list when the folder is changed | |
def update_folder_callback(self, context): | |
if context.scene.lora_folder: | |
bpy.ops.lora.refresh_files() | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment