Comfy Hooks
Extension hooks
At various points during Comfy execution, the application calls
#invokeExtensionsAsync
or #invokeExtensions
with the name of a hook.
These invoke, on all registered extensions, the appropriately named method (if present), such as setup
in the example above.
Comfy provides a variety of hooks for custom extension code to use to modify client behavior.
Events during workflow execution are handled by the
apiUpdateHandlers
A few of the most significant hooks are described below.
As Comfy is being actively developed, from time to time additional hooks are added, so
search for #invokeExtensions
in app.js
to find all available hooks.
See also the sequence in which hooks are invoked.
Commonly used hooks
Start with beforeRegisterNodeDef
, which is used by the majority of extensions, and is often the only one needed.
beforeRegisterNodeDef()
Called once for each node type (the list of nodes available in the AddNode
menu), and is used to
modify the bahaviour of the node.
The object passed in the nodeType
parameter essentially serves as a template
for all nodes that will be created of this type, so modifications made to nodeType.prototype
will apply
to all nodes of this type. nodeData
is an encapsulation of aspects of the node defined in the Python code,
such as its category, inputs, and outputs. app
is a reference to the main Comfy app object (which you
have already imported anyway!)
The usual idiom is to check nodeType.ComfyClass
, which holds the Python class name corresponding to this node,
to see if yuo need to modify the node. Often this means modifying the custom nodes that you have added,
although you may sometimes need to modify the behavior of other nodes (or other custom nodes
might modify yours!), in which case care should be taken to ensure interoperability.
A very common idiom in beforeRegisterNodeDef
is to ‘hijack’ an existing method:
In this idiom the existing prototype method is stored, and then replaced. The replacement calls the
original method (the ?.apply
ensures that if there wasn’t one this is still safe) and then
performs additional operations. Depending on your code logic, you may need to place the apply
elsewhere in your replacement code,
or even make calling it conditional.
When hijacking a method in this way, you will want to look at the core comfy code (breakpoints are your friend) to check and conform with the method signature.
nodeCreated()
Called when a specific instance of a node gets created
(right at the end of the ComfyNode()
function on nodeType
which serves as a constructor).
In this hook you can make modifications to individual instances of your node.
beforeRegisterNodeDef
as described above.init()
Called when the Comfy webpage is loaded (or reloaded). The call is made after the graph object has been created, but before any
nodes are registered or created. It can be used to modify core Comfy behavior by hijacking methods of the app, or of the
graph (a LiteGraph
object). This is discussed further in Comfy Objects.
setup()
Called at the end of the startup process. A good place to add event listeners (either for Comfy events, or DOM events), or adding to the global menus, both of which are discussed elsewhere.
afterConfigureGraph
, not setup
Call sequences
These sequences were obtained by insert logging code into the Comfy app.js
file. You may find similar code helpful
in understanding the execution flow.
Web page load
Loading workflow
Adding new node
Was this page helpful?