Skip to content

Commit

Permalink
feat: ⚡️ add an "actions" endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
melMass committed Jul 24, 2023
1 parent 3801a44 commit 3de160a
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
24 changes: 24 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,30 @@ async def get_debug(request):
return web.json_response({"enabled": enabled})


@PromptServer.instance.routes.get("/mtb/actions")
async def no_route(request):
from . import endpoint

if "text/html" in request.headers.get("Accept", ""):
html_response = f"""
<h1>Actions has no get for now...</h1>
"""
return web.Response(
text=endpoint.render_base_template("Actions", html_response),
content_type="text/html",
)
return web.json_response({"message": "actions has no get for now"})


@PromptServer.instance.routes.post("/mtb/actions")
async def do_action(request):
from . import endpoint

reload(endpoint)

return await endpoint.do_action(request)


# - WAS Dictionary
MANIFEST = {
"name": "MTB Nodes", # The title that will be displayed on Node Class menu,. and Node Class view
Expand Down
48 changes: 48 additions & 0 deletions endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,54 @@

endlog = mklog("mtb endpoint")

#- ACTIONS

def ACTIONS_getStyles(style_name=None):
from .nodes.conditions import StylesLoader

styles = StylesLoader.options
match_list = ["name"]
if styles:
filtered_styles = {
key: value
for key, value in styles.items()
if not key.startswith("__") and key not in match_list
}
if style_name:
if style_name in filtered_styles:
return filtered_styles[style_name]
else:
return {"error": "Style not found"}
return filtered_styles
return {"error": "No styles found"}


async def do_action(request) -> web.Response:
endlog.debug("Init action request")
request_data = await request.json()
name = request_data.get("name")
args = request_data.get("args")

endlog.debug(f"Received action request: {name} {args}")

method_name = "ACTIONS_" + name
method = globals().get(method_name)

if callable(method):
result = method(args) if args else method()
endlog.debug(f"Action result: {result}")
return web.json_response({"result": result})

available_methods = [
attr[len("ACTIONS_") :] for attr in globals() if attr.startswith("ACTIONS_")
]

return web.json_response(
{"error": "Invalid method name.", "available_methods": available_methods}
)


# - HTML UTILS
def render_table(table_dict, sort=True, title=None):
table_rows = ""
table_dict = sorted(
Expand Down
9 changes: 9 additions & 0 deletions web/comfy_shared.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* File: comfy_shared.js
* Project: comfy_mtb
* Author: Mel Massadian
*
* Copyright (c) 2023 Mel Massadian
*
*/

import { app } from '/scripts/app.js'

export const log = (...args) => {
Expand Down
63 changes: 63 additions & 0 deletions web/mtb_widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,69 @@ const mtb_widgets = {
}
break
}
case 'Styles Loader (mtb)': {
const origGetExtraMenuOptions = nodeType.prototype.getExtraMenuOptions
nodeType.prototype.getExtraMenuOptions = function (_, options) {
const r = origGetExtraMenuOptions
? origGetExtraMenuOptions.apply(this, arguments)
: undefined

const getStyle = async (node) => {
try {
const getStyles = await api.fetchApi('/mtb/actions', {
method: 'POST',
body: JSON.stringify({
name: 'getStyles',
args:
node.widgets && node.widgets[0].value
? node.widgets[0].value
: '',
}),
})

const output = await getStyles.json()
return output?.result
} catch (e) {
console.error(e)
}
}
const extracters = [
{
content: 'Extract Positive to Text node',
callback: async () => {
const style = await getStyle(this)
if (style && style.length >= 1) {
if (style[0]) {
const tn = LiteGraph.createNode('Text box')
app.graph.add(tn)
tn.title = `${this.widgets[0].value} (Positive)`
tn.widgets[0].value = style[0]
} else {
}
}
},
},
{
content: 'Extract Negative to Text node',
callback: async () => {
const style = await getStyle(this)
if (style && style.length >= 2) {
if (style[1]) {
const tn = LiteGraph.createNode('Text box')
app.graph.add(tn)
tn.title = `${this.widgets[0].value} (Negative)`
tn.widgets[0].value = style[1]
} else {
}
}
},
},
]
options.push(...extracters)
}

break
}
case 'Save Tensors (mtb)': {
const onDrawBackground = nodeType.prototype.onDrawBackground
nodeType.prototype.onDrawBackground = function (ctx, canvas) {
Expand Down

0 comments on commit 3de160a

Please sign in to comment.