sd-anatomy-fix / docs /ARCHITECTURE.md
Kazzze's picture
Update to current version
3c9bc24 verified
|
Raw History Blame Contribute Delete
6.01 kB

Architecture

Anatomy Fix is one repository for two hosts: the AUTOMATIC1111 family (AUTOMATIC1111, Forge, reForge), which loads scripts/, and ComfyUI, which imports the package root (__init__.py). Both show the same React editor and run the same core.

Layers

Layer Where Knows about
Core anatomy_fix/ except web.py numpy, OpenCV, Pillow. RGB uint8 in, RGB uint8 out. No host, no GPU.
Server side of the editor anatomy_fix/web.py The core, and FastAPI or aiohttp, imported only inside register and register_aiohttp.
A1111-family host scripts/anatomy_fix_script.py, javascript/anatomy_fix.js, style.css The WebUI's modules: processing, scripts, the built-in ControlNet.
ComfyUI host __init__.py, comfy_nodes.py, comfy_web/anatomy_fix.js ComfyUI's nodes, samplers and ControlNet.
Editor ui/ (built into ui/dist) Its own server routes, and the host page through postMessage.

Nothing in the core imports a host module. A host gives the server side what only it can do through the Host class in web.py: the model pass (inpaint), saving, the samplers and ControlNet models it has.

The core

Module What it does
bones.py Bones: tapered fingers, toes and limbs drawn along strokes; the hand's color; the strength map.
fan.py Five bones of a hand or a foot from one line.
erase.py Painting an area over with what surrounds it, a color kept out of the fill.
split.py A crease between fused fingers, a notch at their tip.
donor.py A fragment carried from one line to another, mirrored or not; the likeness of a copy.
eye.py The eye Template, the gaze, the symmetry of two eyes.
teeth.py The teeth Template.
model3d.py The 3D hand and foot: built from slabs and capsules, posed, rasterized with a depth buffer.
guide.py Depth, skeleton and outline guides for a ControlNet, from the same marks.
variants.py Counting fingertips on a result.
passes.py Turning a strength map into passes; "only masked" cropping and pasting; the context around small areas.
job.py The editor's job built into a draft, a strength map and what was erased; run makes the variants and ranks them.

A job, from the marks to the variants

  1. The editor holds the marks (mask strokes, bones, creases, donors, eyes, mouths, 3D models) and the settings, and sends them as one plain JSON job.
  2. job.build draws the draft: it erases what the mask covers, then each tool draws its part. Every tool adds a strength map (how strongly the model may repaint each pixel) and says what it erased.
  3. passes.plan turns that into passes. The erased place gets a background pass first, at high strength, with a prompt that names only what lies behind it and hands, people and bare skin in the negative. The drawn parts get one subject pass at their own strength, with the guide when there is one.
  4. The host runs each pass as a masked inpaint. One pass's result is the next pass's input.
  5. job.run repeats this for each seed and ranks the variants: by fingertip count when there are finger bones, by the likeness of two eyes, or by the likeness of copies to their fragment. The ranking is a hint, not a judge.

The two hosts

A1111 family. The script registers the routes on the WebUI's FastAPI app (on_app_started). It runs a pass with StableDiffusionProcessingImg2Img (only masked, the loaded checkpoint, the host's queue lock). A pass with a guide gets a script runner holding a copy of the built-in ControlNet script and nothing else; a pass without one gets an empty runner, since reForge reads p.scripts. With Soft edges on, the mask of each pass is passes.soft_mask (the strength map as a share of the pass's strength, in gray) and the runner also holds a fresh copy of the host's soft inpainting script, so each pixel is repainted as strongly as the map says. The hidden "to gallery" button is the one Gradio part.

ComfyUI. __init__.py registers the same routes on the PromptServer (aiohttp); the page's files come from their own static route, not from WEB_DIRECTORY, which ComfyUI would run. The node keeps the editor's state in its hidden layout widget and runs the passes itself with the workflow's model, CLIP, VAE and ControlNet; with differential_diffusion on, the model is wrapped by ComfyUI's Differential Diffusion and each pass's noise mask is the strength map, not the area. Anatomy Fix Draft opens the same editor and runs no pass: it returns the draft, the mask and the strength map for a pass of the user's own. The editor's Run is off there.

The editor and the host page

The page is served by the extension itself (/anatomy_fix/ui), in an iframe over the host's page. Its asset addresses get a fresh ?v= on each request, so a new build is never hidden by the cache. The page and the host page talk with postMessage, same origin only:

Message From What
AF_INIT host The theme, and the gallery image (A1111 family) or the node and its state (ComfyUI).
AF_READY editor The page is up: the host sends AF_INIT.
AF_CLOSE editor Close the overlay.
AF_TO_GALLERY editor A1111 family: the host clicks the hidden button that puts the result into the gallery.
AF_STATE editor ComfyUI: the marks, the settings and the job, written into the node.

Where state lives

What Where
The image being fixed, the last draft, the last run's variants In memory on the server, a few sessions at a time (web.Session).
The marks, the tool settings In the page; in a ComfyUI node's layout widget.
The look, the strength levels, the model settings user_settings.json next to the extension (not committed).
A node's last image In memory (web.NODE_IMAGES), for the editor opened on that node.