chrisgoringe / Comfy-Custom-Node-How-To

An unofficial practical guide to getting started developing custom nodes for ComfyUI
GNU General Public License v3.0
143 stars 5 forks source link

Can I define my own data types? #3

Closed chrisgoringe closed 9 months ago

Lerc commented 9 months ago

I don't have a complete understanding of the data type system but it seems to me that data types are represented by widgets in the node editor. This is predominantly on the JavaScript side of the system.

A node plugin can provide a method getCustomWidgets to declare the type CHEESE

  getCustomWidgets(app) {
    return {
      CHEESE(node,inputName,inputData,app) {
         const widget = /* Make A Custom Widget To Edit Cheese */
          node.addCustomWidget(widget);
         return  widget;
      } 
    }
  },

This code requires a bit of attention to follow. getCustomWidgets returns an object. That object contains the field CHEESE which has a function for a value. That function creates a node widget for handling the data type and is called when a node has an INPUT_TYPE declared of type CHEESE

    "required": {
                "slice": ("CHEESE",),     

A custom widget is of the form

const widget = { type: inputData[0], name, size: [128,128], _value : "", get value() { return this._value }, set value(newValue) { this._value=newValue }, draw(ctx, node, width, y) {
}, computeSize(...args) {
return [128,128]; }, async serializeValue(nodeId,widgetIndex) { return "Data That Goes to the Python Side"; } }

There are probably more fields and methods available, but these are what I have used for making my own widgets so far. That canvas context that you are given seems to be for the entire node so you can draw wherever you want, not just in the widget area.

Looking at https://github.com/jagenjo/litegraph.js will give you more info on node widgets.

chrisgoringe commented 9 months ago

Thanks!

On the python side, I think you don't need to do anything.... you can set the output type to CHEESE, and set another node to have an input of type CHEESE, and then you can connect them together in the UI, and the backend will just pass whatever you give it!

So your description is only needed if you want have a custom widget - is that right?

Lerc commented 9 months ago

I think so, but I haven't actually made a node type that is purely Python side, so I can't absolutely confirm that.

chrisgoringe commented 9 months ago

Thanks. Added to the wiki