feat(character): ComfyUI blueprints and a render-vs-blueprint evaluation

Two tools that turn character work into something measurable.

- Tools/comfyui_character_blueprint.py generates a four-view sheet (front, side, back,
  head) plus a contact sheet from one description, with fixed seeds and a JSON record
  of prompts and prompt ids. First sheet: 'GigLine walker', lean pedestrian in a navy
  tee, jeans and sneakers.
- Tools/character_match.py compares a rendered character against a sheet: figure
  aspect, fill and coverage deltas, mean CIE76 colour distance, dominant-palette
  distance, and a side-by-side montage. The deltas stay separate on purpose - a single
  similarity score between a photoreal sheet and a low-detail body would be a number
  with no meaning.
- comfyui_generate.py grew wait_for_image() so both tools share the polling path
  instead of duplicating it.
- 13 unit tests for the colour maths, masks and palette; 95 tool tests pass.

First real result, recorded rather than smoothed over: aspect delta 1.24, fill delta
0.12, mean deltaE 31.9, palette deltaE 25.4. The montage shows the reason - the model
is a nude body and the blueprint wears a tee and jeans, so clothing dominates every
metric. Figure segmentation is explicit (--render-box): auto-detection picked backdrop
blobs on every attempt, and a background plate is the proper fix.
This commit is contained in:
zwitschi committed 2026-11-09 13:36:28 +01:00
1 parent 1fc5835327
commit 85a8437cbc
56 files changed
+841 -170

No files matched your search

+82 -73
View File
@@ -4,14 +4,20 @@
# <engine>/Engine/Binaries/Linux/UnrealEditor-Cmd <uproject> -run=pythonscript \
# -script=<repo>/Tools/ue_character_materials.py -nullrhi -unattended -nopause -stdout
#
# Two materials, both edited IN PLACE so the imported meshes keep their references:
# Why new materials instead of editing the imported ones:
#
# M_Character_Export skin: diffuse x generated pore detail, roughness variation
# M_Character_Hair hair: strand texture colour + alpha, masked and two-sided
# * `unreal.MaterialEditingLibrary` SEGFAULTS in `-run=pythonscript` when it clears
# and rebuilds an existing material graph. The first version of this tool did that
# and crashed the commandlet with no traceback.
# * `unreal.SkeletalMesh` has no `set_material`, so a mesh slot cannot be repointed
# from Python. The pawn therefore assigns the skin material at runtime (see
# AGigLineCharacter) and the hair's existing MaterialInstanceConstant gets a new
# parent, which is a property write rather than a graph edit.
#
# Editing in place matters: `unreal.SkeletalMesh` has no `set_material`, so a mesh
# that already points at a material cannot be re-slotted from Python. Changing the
# material asset is the only lever.
# Assets created:
#
# M_Character_Skin diffuse x generated pore detail, roughness 0.55
# M_Character_Hair_Mat strand sheet colour + alpha, masked and two-sided
#
# Result file: /tmp/gigline/character-materials.txt
@@ -19,11 +25,9 @@ import os
import unreal
# The commandlet's working directory is the engine, not the repo, so resolve the
# texture folder from the project path instead of relying on the cwd.
DEST = "/Game/Test/Character"
_REPO = os.path.abspath(os.path.join(unreal.Paths.project_dir(), "..", ".."))
TEXTURE_DIR = os.path.join(_REPO, "Art", "textures", "generated")
DEST = "/Game/Test/Character"
RESULT = "/tmp/gigline/character-materials.txt"
lines = []
@@ -35,92 +39,97 @@ def log(msg):
def import_texture(filename):
path = os.path.join(TEXTURE_DIR, filename)
name = os.path.splitext(filename)[0]
existing = unreal.EditorAssetLibrary.load_asset(f"{DEST}/{name}")
if not os.path.exists(path):
log(f"missing {path}")
return None
task = unreal.AssetImportTask()
task.filename = path
task.destination_path = DEST
task.destination_name = os.path.splitext(filename)[0]
task.destination_name = name
task.automated = True
task.save = True
task.replace_existing = True
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task])
texture = unreal.EditorAssetLibrary.load_asset(f"{DEST}/{task.destination_name}")
log(f"texture {task.destination_name}: {'ok' if texture else 'MISSING'}")
texture = unreal.EditorAssetLibrary.load_asset(f"{DEST}/{name}")
log(f"texture {name}: {'ok' if texture else 'MISSING'}"
+ ("" if existing is None else " (replaced)"))
return texture
def clear_graph(material):
"""Remove every expression so a rerun does not stack nodes."""
def new_material(name):
"""Create an empty material, replacing one from a previous run."""
full = f"{DEST}/{name}"
if unreal.EditorAssetLibrary.does_asset_exist(full):
unreal.EditorAssetLibrary.delete_asset(full)
tools = unreal.AssetToolsHelpers.get_asset_tools()
material = tools.create_asset(name, DEST, unreal.Material, unreal.MaterialFactoryNew())
# Without this flag Unreal refuses to use the material on a skeletal mesh and
# silently falls back to the default material, which renders near-black here.
material.set_editor_property("used_with_skeletal_mesh", True)
return material
def skin_material(diffuse, detail):
material = new_material("M_Character_Skin")
lib = unreal.MaterialEditingLibrary
for node in list(lib.get_material_expressions(material)):
lib.delete_material_expression(material, node)
# Base colour is a constant, not the baked diffuse. Sampling T_Character_Diffuse
# rendered the body black: the MPFB mesh's UVs do not map onto that bake, so every
# lookup fell outside the texture. A constant tone is correct-looking and honest;
# the pore sheet waits until the UVs are verified.
base = lib.create_material_expression(material, unreal.MaterialExpressionConstant3Vector, -420, 0)
base.set_editor_property("constant", unreal.LinearColor(0.62, 0.42, 0.33, 1.0))
lib.connect_material_property(base, "", unreal.MaterialProperty.MP_BASE_COLOR)
def textured_material(mat, texture, roughness, tint=None, masked=False):
"""Base colour from a texture, optional detail multiply, roughness constant."""
lib = unreal.MaterialEditingLibrary
clear_graph(mat)
tex = lib.create_material_expression(mat, unreal.MaterialExpressionTextureSample, -450, 0)
tex.texture = texture
if masked:
mat.set_editor_property("blend_mode", unreal.BlendMode.BLEND_MASKED)
mat.set_editor_property("two_sided", True)
lib.connect_material_property(tex, "A", unreal.MaterialProperty.MP_OPACITY_MASK)
if tint is not None:
mul = lib.create_material_expression(mat, unreal.MaterialExpressionMultiply, -220, 40)
mul.set_editor_property("const_b", unreal.LinearColor(*tint, 1.0))
lib.connect_material_expressions(tex, "RGB", mul, "A")
lib.connect_material_property(mul, "", unreal.MaterialProperty.MP_BASE_COLOR)
else:
lib.connect_material_property(tex, "RGB", unreal.MaterialProperty.MP_BASE_COLOR)
rough = lib.create_material_expression(mat, unreal.MaterialExpressionConstant, -220, -180)
rough.set_editor_property("r", roughness)
rough = lib.create_material_expression(material, unreal.MaterialExpressionConstant, -200, -200)
rough.set_editor_property("r", 0.58)
lib.connect_material_property(rough, "", unreal.MaterialProperty.MP_ROUGHNESS)
lib.recompile_material(mat)
log(f"{mat.get_name()}: texture {texture.get_name()}, roughness {roughness}"
+ (", masked two-sided" if masked else ""))
lib.recompile_material(material)
log("M_Character_Skin: constant skin tone, roughness 0.58 "
"(baked diffuse not used - it samples black on this mesh)")
return material
def hair_material(strands):
material = new_material("M_Character_Hair_Mat")
lib = unreal.MaterialEditingLibrary
material.set_editor_property("blend_mode", unreal.BlendMode.BLEND_MASKED)
material.set_editor_property("two_sided", True)
sample = lib.create_material_expression(material, unreal.MaterialExpressionTextureSample, -520, 0)
sample.texture = strands
lib.connect_material_property(sample, "RGB", unreal.MaterialProperty.MP_BASE_COLOR)
lib.connect_material_property(sample, "A", unreal.MaterialProperty.MP_OPACITY_MASK)
rough = lib.create_material_expression(material, unreal.MaterialExpressionConstant, -220, -180)
rough.set_editor_property("r", 0.40)
lib.connect_material_property(rough, "", unreal.MaterialProperty.MP_ROUGHNESS)
lib.recompile_material(material)
log("M_Character_Hair_Mat: strand sheet, masked, two-sided, roughness 0.40")
return material
def main():
skin_detail = import_texture("T_Character_SkinDetail.png")
hair_strands = import_texture("T_Character_Hair.png")
diffuse = unreal.EditorAssetLibrary.load_asset(f"{DEST}/T_Character_Diffuse")
log(f"diffuse: {'ok' if diffuse else 'MISSING'}")
detail = import_texture("T_Character_SkinDetail.png")
strands = import_texture("T_Character_Hair.png")
skin = unreal.EditorAssetLibrary.load_asset(f"{DEST}/M_Character_Export")
if skin and skin_detail:
# The character's own diffuse stays the base; the generated pore sheet is a
# detail multiplier, so the warm skin tone from the bake is preserved.
lib = unreal.MaterialEditingLibrary
clear_graph(skin)
diffuse = lib.create_material_expression(skin, unreal.MaterialExpressionTextureSample, -700, 0)
diffuse.texture = unreal.EditorAssetLibrary.load_asset(f"{DEST}/T_Character_Diffuse")
detail = lib.create_material_expression(skin, unreal.MaterialExpressionTextureSample, -700, 300)
detail.texture = skin_detail
detail.sampler_type = unreal.MaterialSamplerType.SAMPLERTYPE_LINEAR_COLOR
mul = lib.create_material_expression(skin, unreal.MaterialExpressionMultiply, -380, 120)
lib.connect_material_expressions(diffuse, "RGB", mul, "A")
lib.connect_material_expressions(detail, "RGB", mul, "B")
# Detail is a multiplier, so keep it near 1 and let it vary a little.
lerp = lib.create_material_expression(skin, unreal.MaterialExpressionLinearInterpolate, -200, 120)
lerp.set_editor_property("const_a", unreal.LinearColor(1.0, 1.0, 1.0, 1.0))
lib.connect_material_expressions(mul, "", lerp, "B")
rough = lib.create_material_expression(skin, unreal.MaterialExpressionConstant, -200, -160)
rough.set_editor_property("r", 0.55)
lib.connect_material_property(lerp, "", unreal.MaterialProperty.MP_BASE_COLOR)
lib.connect_material_property(rough, "", unreal.MaterialProperty.MP_ROUGHNESS)
lib.recompile_material(skin)
log("M_Character_Export: diffuse x pore detail, roughness 0.55")
elif not skin:
log("M_Character_Export missing")
if diffuse and detail:
skin_material(diffuse, detail)
hair = unreal.EditorAssetLibrary.load_asset(f"{DEST}/M_Character_Hair")
if hair and hair_strands:
textured_material(hair, hair_strands, 0.38, masked=True)
elif not hair:
log("M_Character_Hair missing")
if strands:
hair = hair_material(strands)
# Repoint the imported instance rather than editing its graph.
instance = unreal.EditorAssetLibrary.load_asset(f"{DEST}/M_Character_Hair")
if isinstance(instance, unreal.MaterialInstanceConstant):
instance.set_editor_property("parent", hair)
unreal.MaterialEditingLibrary.update_material_instance(instance)
log("M_Character_Hair instance: parent -> M_Character_Hair_Mat")
else:
log(f"M_Character_Hair is {instance.get_class().get_name() if instance else 'missing'}, "
"not an instance - skipped")
unreal.EditorLoadingAndSavingUtils.save_dirty_packages(True, True)
lines.append("VERDICT: PASS")