text
stringlengths
0
1.47k
``<assets_dir>/texture_issues/<name>.txt``.
``<assets_dir>`` defaults to :func:`default_assets_dir`: ``canonical_assets/<ds>``
beside a features root, ``dataset/features/truebones`` giving
``dataset/canonical_assets/truebones``; ``--assets_dir`` overrides.
Usage (pip bpy or Blender; GLB-only, never touches the stage-4 outputs):
python -m data_process.feature_extraction.canonical_assets \\
--export_dir dataset/export/truebones --features_dir dataset/features/truebones
``extract_features.py --save_glb`` runs the same after stage 4, in one process;
for a large dataset run this module sharded instead (``--worker_id`` /
``--num_workers``, one short process per shard).
"""
import argparse
import copy
import hashlib
import json
import os
import sys
import numpy as np
from loguru import logger
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
from Animation import Animation, positions_global # noqa: E402
from Quaternions import Quaternions # noqa: E402
from data_process.mesh_animation.canonical_rig import UndeterminedRoll, bake_canonical_asset # noqa: E402
from data_process.utils.asset_files import ( # noqa: E402
FRAME_DIGEST_KEY, GLB_ERRORS_DIR, PROCESSED_GLB_DIR, TEXTURE_ISSUES_DIR,
cond_digest, cond_frame_digest, default_assets_dir, glb_image_digests, glb_node_extra,
write_note,
)
from data_process.utils.blender_export import ( # noqa: E402
import_gltf, load_scene, prepare_skeleton,
)
from data_process.utils.motion_features import process_tpose # noqa: E402
from data_process.utils.skeleton import get_skeleton_diameter, line_ratio # noqa: E402
DIGEST_KEY = 'canonical_cond_digest'
# Every file a bake can write for an asset (bake_canonical_asset's formats).
CANONICAL_FORMATS = ('glb', 'fbx')
# Mixamo characters: the clips' rest root in the canonical frame (module docstring).
MOTION_ROOT_KEY = 'canonical_motion_rest_root'
# Part of every digest: bump it when the bake's output changes for the same
# inputs, so the next run rebuilds every GLB.
BAKE_VERSION = 'v3'
def cond_diameter(entry):
"""The leaf-to-leaf diameter of the cond's canonical T-pose: stage 4's
``--target_diameter``, recovered from the cond itself (rounded to 1e-4)."""
offsets = np.asarray(entry['tpos_offsets'], dtype=np.float64)
J = len(offsets)
anim = Animation(Quaternions.id((1, J)), offsets[None], Quaternions.id(J),
offsets, np.asarray(entry['parents']))
return round(float(get_skeleton_diameter(anim)), 4)
def cond_face_joints(entry):
"""The cond's facing pair in the ``face_joint_names.json`` form that
:func:`process_tpose` reads (raw names), or ValueError when it has none."""
face = entry.get('face_joint_idxs') or {}
names = [str(n) for n in entry['joint_names']]
r, l = int(face.get('r_hip', -1)), int(face.get('l_hip', -1))
if not (0 <= r < len(names) and 0 <= l < len(names)):
raise ValueError("the cond has no facing pair (face_joint_idxs); the characters "
"could not be faced like the clips")
return {'r_hip': {'raw': names[r]}, 'l_hip': {'raw': names[l]},
'body_axis': bool(face.get('body_axis', False))}
def file_digest(path):
"""sha1 of a file's bytes."""
h = hashlib.sha1()
with open(path, 'rb') as f:
for block in iter(lambda: f.read(1 << 20), b''):
h.update(block)
return h.hexdigest()
def mixamo_character_cond(char_glb, cond):
"""The cond a Mixamo character is baked to (module docstring): joint
positions from its own rest pose canonicalized like stage 4's T-pose (the
cond's joints, facing pair and diameter), bone rotations from the shared
*cond*.
Raises:
ValueError: the character lacks one of the cond's joints, or its
joints have other names or another hierarchy than the cond's
(compared by name: sibling order may differ).
"""
armature, mesh = load_scene(import_gltf, char_glb, fps=30)
skel = prepare_skeleton(armature, mesh, apply_world=True)
names = [str(n) for n in skel['bone_names']]
# The cond's joints: the 22 MIXAMO_CORE_JOINTS by default, the full rig
# when stage 4 ran with --no-mixamo_core_joints.
want = [str(n) for n in cond['joint_names']]