-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy path__init__.py
More file actions
230 lines (205 loc) · 9.76 KB
/
__init__.py
File metadata and controls
230 lines (205 loc) · 9.76 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""VNCCS - Visual Novel Character Creator Suite for ComfyUI."""
from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
WEB_DIRECTORY = "web"
import os, json, inspect
import traceback
def _vnccs_register_endpoint(): # lazy registration to avoid import errors in analysis tools
try:
from server import PromptServer
from aiohttp import web
except Exception:
return
@PromptServer.instance.routes.get("/vnccs/config")
async def vnccs_get_config(request):
name = request.rel_url.query.get("name")
if not name:
return web.json_response({"error": "name required"}, status=400)
try:
from .nodes.character_creator import CharacterCreator
base = CharacterCreator().base_path
except Exception:
base = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "output", "VN_CharacterCreatorSuit"))
cfg_path = os.path.join(base, name, f"{name}_config.json")
if not os.path.exists(cfg_path):
return web.json_response({"error": "not found", "path": cfg_path}, status=404)
try:
with open(cfg_path, 'r', encoding='utf-8') as f:
data = json.load(f)
return web.json_response(data)
except Exception as e:
return web.json_response({"error": "read failed", "detail": str(e)}, status=500)
@PromptServer.instance.routes.get("/vnccs/create")
async def vnccs_create_character(request):
name = request.rel_url.query.get("name", "").strip()
if not name:
return web.json_response({"error": "name required"}, status=400)
forbidden = set('/\\:')
if any(c in forbidden for c in name):
return web.json_response({"error": "invalid characters"}, status=400)
defaults = dict(
existing_character=name,
background_color="green",
aesthetics="masterpiece",
nsfw=False,
sex="female",
age=18,
race="human",
eyes="blue eyes",
hair="black long",
face="freckles",
body="medium breasts",
skin_color="",
additional_details="",
seed=0,
negative_prompt="bad quality,worst quality,worst detail,sketch,censor, missing arm, missing leg, distorted body",
lora_prompt="",
new_character_name=name,
)
try:
from .nodes.character_creator import CharacterCreator
cc = CharacterCreator()
os.makedirs(cc.base_path, exist_ok=True)
base_char_dir = os.path.join(cc.base_path, name)
config_path = os.path.join(base_char_dir, f"{name}_config.json")
if os.path.exists(config_path):
try:
with open(config_path, 'r', encoding='utf-8') as f:
existing_data = json.load(f)
except Exception:
existing_data = None
return web.json_response({
"ok": True,
"name": name,
"existing": True,
"config_path": config_path,
"data": existing_data,
})
# Backward compatibility: drop force_new if method doesn't accept it
try:
sig = inspect.signature(cc.create_character)
if 'force_new' not in sig.parameters and 'force_new' in defaults:
defaults.pop('force_new')
except Exception:
defaults.pop('force_new', None)
positive_prompt, seed, negative_prompt, age_lora_strength, sheets_path, faces_path, face_details = cc.create_character(**defaults)
return web.json_response({
"ok": True,
"name": name,
"seed": seed,
"sheets_path": sheets_path,
"faces_path": faces_path,
"positive_prompt": positive_prompt,
"negative_prompt": negative_prompt,
"age_lora_strength": age_lora_strength,
"face_details": face_details,
"config_path": os.path.join(base_char_dir, f"{name}_config.json"),
})
except Exception as e:
return web.json_response({
"error": "create failed",
"detail": str(e),
"type": type(e).__name__,
"trace": traceback.format_exc(),
}, status=500)
@PromptServer.instance.routes.get("/vnccs/create_costume")
async def vnccs_create_costume(request):
character_name = request.rel_url.query.get("character", "").strip()
costume_name = request.rel_url.query.get("costume", "").strip()
if not character_name or not costume_name:
return web.json_response({"error": "character and costume required"}, status=400)
forbidden = set('/\\:')
if any(c in forbidden for c in character_name) or any(c in forbidden for c in costume_name):
return web.json_response({"error": "invalid characters"}, status=400)
try:
from .utils import load_config, save_config, ensure_costume_structure
config = load_config(character_name)
if not config:
config = {"character_info": {}, "costumes": {}}
if "costumes" not in config:
config["costumes"] = {}
if costume_name in config["costumes"]:
return web.json_response({"error": "Costume already exists"})
config["costumes"][costume_name] = {
"face": "",
"head": "",
"top": "",
"bottom": "",
"shoes": "",
"negative_prompt": ""
}
if save_config(character_name, config):
ensure_costume_structure(character_name, costume_name)
return web.json_response({"ok": True, "costume": costume_name})
else:
return web.json_response({"error": "Failed to save"}, status=500)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
@PromptServer.instance.routes.get("/vnccs/models/{filename}")
async def vnccs_get_model(request):
"""Serve FBX model files for 3D pose editor"""
filename = request.match_info.get("filename", "")
if not filename.endswith(".fbx"):
return web.Response(text="Only FBX files allowed", status=400)
# Get the models directory
models_dir = os.path.join(os.path.dirname(__file__), "models")
file_path = os.path.join(models_dir, filename)
# Security check - ensure file is within models directory
if not os.path.abspath(file_path).startswith(os.path.abspath(models_dir)):
return web.Response(text="Invalid path", status=400)
if not os.path.exists(file_path):
return web.Response(text=f"Model not found: {filename}", status=404)
try:
with open(file_path, 'rb') as f:
return web.Response(
body=f.read(),
content_type='application/octet-stream',
headers={
'Content-Disposition': f'inline; filename="{filename}"',
'Access-Control-Allow-Origin': '*'
}
)
except Exception as e:
return web.Response(text=f"Error reading file: {str(e)}", status=500)
@PromptServer.instance.routes.get("/vnccs/pose_presets")
async def vnccs_get_pose_presets(request):
"""Get list of available pose presets"""
try:
presets_dir = os.path.join(os.path.dirname(__file__), "presets", "poses")
presets = []
if os.path.exists(presets_dir):
for filename in sorted(os.listdir(presets_dir)):
if filename.endswith('.json'):
# Create preset entry
preset_id = filename[:-5] # Remove .json
label = preset_id.replace('_', ' ').title()
presets.append({
"id": preset_id,
"label": label,
"file": filename
})
return web.json_response(presets)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
@PromptServer.instance.routes.get("/vnccs/pose_preset/{filename}")
async def vnccs_get_pose_preset(request):
"""Get specific pose preset file"""
try:
filename = request.match_info.get("filename", "")
if not filename.endswith('.json'):
return web.Response(text="Only JSON files allowed", status=400)
presets_dir = os.path.join(os.path.dirname(__file__), "presets", "poses")
file_path = os.path.join(presets_dir, filename)
# Security check
if not os.path.abspath(file_path).startswith(os.path.abspath(presets_dir)):
return web.Response(text="Invalid path", status=400)
if not os.path.exists(file_path):
return web.Response(text=f"Preset not found: {filename}", status=404)
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
return web.json_response(data)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
_vnccs_register_endpoint()
_vnccs_register_endpoint()