From e381a4b9ed0694fccae80a4840d42fa4837372c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20=20Sk=C3=B6ld?= Date: Wed, 19 Jun 2024 15:51:59 +0200 Subject: [PATCH] Changed random character select from long list to just favourites --- src/character_select.py | 14 +++++- web/js/excludeRandomCharacter.js | 76 ++++++-------------------------- 2 files changed, 26 insertions(+), 64 deletions(-) diff --git a/src/character_select.py b/src/character_select.py index 13203d4..97c9f75 100644 --- a/src/character_select.py +++ b/src/character_select.py @@ -117,10 +117,20 @@ class CharacterSelectNode: def select_random_character(self): # Fetching the exclusion settings - excluded_characters = ConfigReader.get_setting("sn0w.ExcludedRandomCharacters", []) + exclude_characters = ConfigReader.get_setting("sn0w.ExcludedRandomCharacters", False) + if (exclude_characters == False): + random_character_name = random.choice(list(filtered_characters.keys())) + char_item = filtered_characters[random_character_name] + self.logger.log("Random Character: " + str(char_item["name"]), "INFORMATIONAL") + return char_item + + favourite_characters = ConfigReader.get_setting("sn0w.FavouriteCharacters", []) + if (favourite_characters == []): + self.logger.log("No valid characters found based on the specified criteria.", "WARNING") + return "" # Filter the characters by excluding the ones in the exclusion list - filtered_characters = {name: char for name, char in self.final_character_dict.items() if self.sanitize_name(name) in excluded_characters} + filtered_characters = {name: char for name, char in self.final_character_dict.items() if name in favourite_characters} # Choosing a random character from the filtered list if filtered_characters: diff --git a/web/js/excludeRandomCharacter.js b/web/js/excludeRandomCharacter.js index 8f037d8..da635d2 100644 --- a/web/js/excludeRandomCharacter.js +++ b/web/js/excludeRandomCharacter.js @@ -1,68 +1,20 @@ -import { SettingUtils } from './sn0w.js'; import { app } from "../../../scripts/app.js"; -const Id = "sn0w.ExcludedRandomCharacters"; -const jsonUrls = ['/extensions/ComfyUI-Sn0w-Scripts/settings/characters.json', '/extensions/ComfyUI-Sn0w-Scripts/settings/custom_characters.json']; - -// Function to fetch, sort, and load options from multiple JSON files -const loadOptionsFromJson = async (urls) => { - const allData = []; - - // Fetch data from each URL and combine it into one array - for (const url of urls) { - try { - const response = await fetch(url); - if (!response.ok) { - console.warn('Network response was not ok for URL: ' + url); - continue; - } - const data = await response.json(); - allData.push(...data); - } catch (error) { - console.warn('Failed to fetch or parse JSON from URL: ' + url, error); - } - } - - if (allData.length === 0) { - console.error('No data fetched from any URLs'); - return []; - } - - // Sort the combined data alphabetically by the name property - const sortedData = allData.sort((a, b) => a.name.localeCompare(b.name)); - - // Map the sorted data to the required format - return sortedData.map(item => ({ - label: item.name, - value: item.name.replace(/[^a-z0-9]/gi, '_').toLowerCase() // Sanitize the value - })); +const id = "sn0w.ExcludedRandomCharacters"; +const settingDefinition = { + id, + name: "[Sn0w] Random Characters from Favourites Only", + defaultValue: false, + type: "boolean" }; -// Function to register the setting -const registerSetting = (settingDefinition) => { - const extension = { - name: settingDefinition.id, - init() { - app.ui.settings.addSetting(settingDefinition); - } - }; - app.registerExtension(extension); +let setting; + +const extension = { + name: id, + init() { + setting = app.ui.settings.addSetting(settingDefinition); + }, }; -// Load options and register the setting -loadOptionsFromJson(jsonUrls).then(options => { - const SettingDefinition = { - id: Id, - name: "[Sn0w] Exclude/Include Random Characters", - type: SettingUtils.createCheckboxSetting, - defaultValue: [], - attrs: { - options: options, - tooltip: "" - } - }; - - registerSetting(SettingDefinition); -}).catch(error => { - console.error('Error registering setting:', error); -}); +app.registerExtension(extension);