File size: 2,684 Bytes
4450790 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#---------------------------------------------------------------------------------------------------------------------#
# CR Animation Nodes by RockOfFire and Akatsuzi https://github.com/Suzie1/CR-Animation-Nodes
# for ComfyUI https://github.com/comfyanonymous/ComfyUI
#---------------------------------------------------------------------------------------------------------------------#
import json
import os
#---------------------------------------------------------------------------------------------------------------------#
def read_json_file(file_path):
"""
Reads a JSON file's content and returns it.
Ensures content matches the expected format.
"""
if not os.access(file_path, os.R_OK):
print(f"Warning: No read permissions for file {file_path}")
return None
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = json.load(file)
# Check if the content matches the expected format.
if not all(['name' in item and 'prompt' in item and 'negative_prompt' in item for item in content]):
print(f"Warning: Invalid content in file {file_path}")
return None
return content
except Exception as e:
print(f"An error occurred while reading {file_path}: {str(e)}")
return None
#---------------------------------------------------------------------------------------------------------------------#
def load_styles_from_directory(directory):
"""
Loads styles from all JSON files in the directory.
Renames duplicate style names by appending a suffix.
"""
#json_files = get_all_json_files(directory)
json_files = [os.path.join(directory, file) for file in os.listdir(directory) if file.endswith('.json') and os.path.isfile(os.path.join(directory, file))]
combined_data = []
seen = set()
for json_file in json_files:
json_data = read_json_file(json_file)
if json_data:
for item in json_data:
original_style = item['name']
style = original_style
suffix = 1
while style in seen:
style = f"{original_style}_{suffix}"
suffix += 1
item['name'] = style
seen.add(style)
combined_data.append(item)
unique_style_names = [item['name'] for item in combined_data if isinstance(item, dict) and 'name' in item]
return combined_data, unique_style_names
#---------------------------------------------------------------------------------------------------------------------#
|