# avatar-graph-comfyui

A custom_nodes module for creating real-time interactive avatars powered by blender bpy mesh api + Avatech Shape Flow runtime.
# Demo
https://github.com/avatechai/avatar-graph-comfyui/assets/18395202/47fd5a98-ab0f-4c5c-b132-5d4b0aa02763
[➡️Open in Avatech Labs]()
https://github.com/avatechai/avatar-graph-comfyui/assets/18395202/b229a4b3-88ba-4755-a8e0-9149260eef12
[➡️Open in Avatech Labs]()
https://github.com/avatechai/avatar-graph-comfyui/assets/18395202/e054e1a9-eb20-4ca6-be44-0ba9c7c07d50
[➡️Open in Avatech Labs]()
# Contents
- [Demo](#demo)
- [Custom Nodes](custom-nodes)
- [Workflow Template](workflow-template)
- [Image Preprocess Guide](#image-preprocess-guide)
- [Shape Flow](#shape-flow)
- [Installation](#installation)
- [Development](#development)
# Custom Nodes
Custom nodes for ComfyUI.
Expand to see all the nodes available✨
## Image Segmentation Nodes
| Name | Description | Preview |
| ---------------------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `Segmentation (SAM)` | Integrative SAM node allowing you to directly select and create multiple image segment output. |
|
## Mesh Edit Nodes
| Name | Description | Preview |
| ---------------------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `Create Mesh Layer` | Create a mesh object from the input images (usually a segmented part of the entire image) |
|
| `Join Meshes` | Combine multiple meshes into a single mesh object |
|
| `Match Texture Aspect Ratio` | Since the mesh is created in 1:1 aspect ratio, a re-scale is needed at the end of the operation |
|
| `Plane Texture Unwrap` | Will perform mesh face fill and UV Cube project on the target plane mesh, scaled to bounds. |
|
## Shape Keys Nodes
| Name | Description | Preview |
| ---------------------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `Mesh Modify Shape Key` | Given shape key name & target vertex_group, modify the vertex / all vertex’s transform |
|
| `Create Shape Flow` | Create runtime shape flow graph, allowing interactive inputs affecting shape keys value in runtime |
|
## Avatar Output Nodes
| Name | Description | Preview |
| ---------------------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `Avatar Main Output` | The primary output of the .ava file. The embeded Avatar View will auto update with this node's output |
|
# Workflow Template
## Simple Rig Workflow
To enable the character to blink eyes and talking.
> **🎯Notice**
>
> For optimal results, please input a character image with an open mouth and a minimum resolution of 768x768. This higher resolution will enable the tool to accurately recognize and work with facial features.
[💡Generate new image Guide](#character-gen-prompting-guide)
[💡Make your character mouth open Guide](#mouth-open-guide-inpaint)

### Download: 📂[Simple Rig Template](https://github.com/avatechai/avatar-graph-comfyui/blob/main/workflow_templates/SimpleEye+MouthMovement.json)
# Image Preprocess Guide
### 💡If you want to generate a new character image
> you can download this Template and refer to the Guide!
>
> Character Gen Prompting Guide
>
> # Character Gen Prompting Guide
>> **🎯Notice**
>>
>> We need a character image with an open mouth and enable the tool to easily recognize facial features, so please add to the prompt:
>>
>> ```looking at viewer, detailed face, open mouth, [smile], solo,eye-level angle```
>
>
>
> ### Download: 📂[Character Gen Template](https://github.com/avatechai/avatar-graph-comfyui/blob/main/workflow_templates/SimpleCharacterGen.json)
> Feel free to change any checkpoint model that suits your needs.
>
>
### 💡If you have a character image but it's not mouth open
> you can download this Template and refer to the Guide!
>
> Mouth Open Guide (Inpaint)
>
> # Mouth Open Guide (Inpaint)
> To maintain consistency with the base image, it is recommended to utilize a checkpoint model that aligns with its style.
>
> 
>
> ### Download: 📂[MouthOpen Template](https://github.com/avatechai/avatar-graph-comfyui/blob/main/workflow_templates/MouthOpen_(inpaint).json)
>
> ### Inpaint Demonstration
>
> https://github.com/avatechai/avatar-graph-comfyui/assets/48451938/ff48c3d9-7292-4505-8993-8f117cee34ff
>
> ### Recommend Checkpoint Model List
>
> ##### Anime Style SD1.5
>- https://civitai.com/models/35960/flat-2d-animerge
>- https://civitai.com/models/24149/mistoonanime
>- https://civitai.com/models/22364/kizuki-anime-hentai-checkpoint
>##### Realistic Style SD1.5
>- https://civitai.com/models/4201/realistic-vision-v51
>- https://civitai.com/models/49463/am-i-real
>- https://civitai.com/models/43331/majicmix-realistic
>
>
# Shape Flow

# Installation
1. `cd custom_nodes`
2. `git clone https://github.com/avatechgg/avatar-graph-comfyui.git`
3. Install deps `cd avatar-graph-comfyui && python -m pip install -r requirements.txt`
4. Navigate to in your comfyui installation folder, open `comfyui/main.py` and add the following lines:
```py
def prompt_worker(q, server):
e = execution.PromptExecutor(server)
# This is a workaround since using blender as a python module, the bpy module has to be imported after in the custom thread, otherwise it will cause a segfault if imported in the custom nodes.
# Add next line
import global_bpy
while True:
item, item_id = q.get()
# Add next line
global_bpy.reset_bpy()
```
5. Restart comfyui
6. Run comfyui with enable-cors-header `python main.py --enable-cors-header` or (mac)`python main.py --force-fp16 --enable-cors-header`
# Development
If you are interested in contributing expand to see development details✨
For comfyui frontend extension, frontend js located at `avatar-graph-comfyui/js`
Web stack used: [vanjs](https://github.com/vanjs-org/van) [tailwindcss](https://github.com/tailwindlabs/tailwindcss)
## Install deps
```
pnpm i
```
Run the dev command to start the tailwindcss watcher
```
pnpm dev
```
For each changes, simply refresh the comfyui page to see the changes.
p.s. For tailwind autocomplete, add the following to your vscode settings.json.
```json
{
"tailwindCSS.experimental.classRegex": [
["class\\s?:\\s?([\\s\\S]*)", "(?:\"|')([^\"']*)(?:\"|')"]
]
}
```