Skip to content

Commit 49bd588

Browse files
committed
feat(transpile): add settings for hook (#27, #38)
1 parent ef60cf8 commit 49bd588

File tree

6 files changed

+75
-26
lines changed

6 files changed

+75
-26
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ private/
44

55
/examples
66

7+
# Ignore dynaconf files
8+
.secrets.*
9+
*.local.*
10+
711
# Hatch will only respect the first .gitignore file
812
/src/comfy_script/runtime/**/*.pyi
913

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ ComfyScript can also be used without installed ComfyUI. See [only ComfyScript pa
8686
## Transpiler
8787
The transpiler can translate ComfyUI's workflows to ComfyScript.
8888

89-
When ComfyScript is installed as custom nodes, `SaveImage` and similar nodes will be hooked to automatically save the script as images' metadata. And the script will also be output to the terminal.
89+
When ComfyScript is installed as custom nodes, `SaveImage` and similar nodes will be hooked to automatically save the script as the image's metadata. The script will also be printed to the terminal.
9090

9191
For example, here is a workflow in ComfyUI:
9292

@@ -133,6 +133,8 @@ Comparing scripts:
133133

134134
![](docs/images/README/diff.png)
135135

136+
To control these features, see [settings.example.toml](settings.example.toml).
137+
136138
You can also use the transpiler via the [CLI](docs/Transpiler.md#cli).
137139

138140
## Runtime

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ client = [
3737

3838
# Transpiler
3939
transpile = [
40+
# Already required by ComfyUI (torch)
4041
"networkx[default] ~= 3.0",
4142

4243
# Used to get nodes info
4344
"comfy-script[client]",
45+
46+
"dynaconf ~= 3.0",
4447
]
4548

4649
# Runtime

settings.example.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Settings example
2+
# Do not edit this file directly, instead, copy the following as `settings.toml` and edit that file.
3+
4+
# These settings can also be overriden by:
5+
# - Environment variables
6+
# e.g. `COMFY_SCRIPT_TRANSPILE_HOOK_ENABLED=false`
7+
# - Python code
8+
# e.g.
9+
# ```python
10+
# from comfy_script.config import settings
11+
# settings.transpile.hook.enabled=False
12+
# ```
13+
14+
[transpile.hook]
15+
# When ComfyScript is installed as custom nodes, `SaveImage` and similar nodes will be hooked to automatically save the script as the image's metadata. The script will also be printed to the terminal.
16+
# To disable a feature, change its value to `false`.
17+
save_script = true
18+
print_script = true

src/comfy_script/config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pathlib import Path
2+
from dynaconf import Dynaconf, Validator
3+
4+
settings = Dynaconf(
5+
envvar_prefix='COMFY_SCRIPT',
6+
root_path=Path(__file__).resolve().parent,
7+
settings_files=['settings.toml', '.secrets.toml'],
8+
validators=[
9+
Validator('transpile.hook.enabled', default=True),
10+
Validator('transpile.hook.save_script', default=True),
11+
Validator('transpile.hook.print_script', default=True),
12+
],
13+
)
14+
15+
# See `settings.example.toml` for details.

src/comfy_script/nodes/__init__.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,35 @@ def chunks(self):
7777
chunks = self._chunks
7878
self._chunks = []
7979

80-
if 'workflow' in self._texts or 'prompt' in self._texts:
81-
try:
82-
workflow, zip = self._texts['workflow' if 'workflow' in self._texts else 'prompt']
83-
84-
end_nodes = None
85-
# TODO: UNIQUE_ID
86-
frame = inspect.currentframe()
87-
while frame := frame.f_back:
88-
if 'unique_id' in frame.f_locals:
89-
end_nodes = [frame.f_locals['unique_id']]
90-
break
91-
else:
92-
print('ComfyScript: Failed to resolve the id of current node.')
93-
94-
comfy_script = transpile.WorkflowToScriptTranspiler(workflow).to_script(end_nodes)
95-
# TODO: Syntax highlight?
96-
print('ComfyScript:', comfy_script, sep='\n')
97-
98-
super().add_text('ComfyScript', comfy_script, zip)
99-
except Exception:
100-
# Print stack trace, but do not block the original saving
101-
traceback.print_exc()
102-
else:
103-
print("ComfyScript: Failed to save ComfyScript because neither of workflow and prompt is in extra_pnginfo")
80+
print_script = settings.transpile.hook.print_script
81+
save_script = settings.transpile.hook.save_script
82+
if print_script is True or save_script is True:
83+
if 'workflow' in self._texts or 'prompt' in self._texts:
84+
try:
85+
workflow, zip = self._texts['workflow' if 'workflow' in self._texts else 'prompt']
86+
87+
end_nodes = None
88+
# TODO: UNIQUE_ID
89+
frame = inspect.currentframe()
90+
while frame := frame.f_back:
91+
if 'unique_id' in frame.f_locals:
92+
end_nodes = [frame.f_locals['unique_id']]
93+
break
94+
else:
95+
print('ComfyScript: Failed to resolve the id of current node.')
96+
97+
comfy_script = transpile.WorkflowToScriptTranspiler(workflow).to_script(end_nodes)
98+
if print_script is True:
99+
# TODO: Syntax highlight?
100+
print('ComfyScript:', comfy_script, sep='\n')
101+
102+
if save_script is True:
103+
super().add_text('ComfyScript', comfy_script, zip)
104+
except Exception:
105+
# Print stack trace, but do not block the original saving
106+
traceback.print_exc()
107+
else:
108+
print("ComfyScript: Failed to save ComfyScript because neither of workflow and prompt is in extra_pnginfo")
104109

105110
if 'ComfyScriptSource' in self._texts:
106111
try:
@@ -165,7 +170,9 @@ def chunks(self):
165170
# setattr(SaveImage, SaveImage.FUNCTION, save_images_hook)
166171

167172
try:
168-
setup()
173+
from ..config import settings
174+
if settings.transpile.hook.enabled is True:
175+
setup()
169176
except ImportError as e:
170177
success = False
171178
print(

0 commit comments

Comments
 (0)