feat(P2): USD Foundation — canonical part identity + material overrides
M1 — USD exporter:
- render-worker/scripts/export_step_to_usd.py (631 lines)
Full XCAF traversal, one UsdGeom.Mesh per leaf part,
schaeffler:partKey on every prim, index-space sharpEdgeVertexPairs
- render-worker/Dockerfile: usd-core>=24.11 installed (USD 0.26.3)
M2 — usd_master MediaAsset + pipeline auto-chain:
- migrations 060 (usd_master enum), 061 (3 JSONB columns),
062 (rename tessellation settings keys)
- generate_usd_master_task: runs export_step_to_usd.py, upserts
usd_master MediaAsset, writes resolved_material_assignments to CadFile
- Auto-chained from generate_gltf_geometry_task after every GLB export
- step_tasks.py shim re-exports generate_usd_master_task
M3 — scene-manifest API:
- part_key_service.py: build_scene_manifest(), generate_part_key(),
four-layer material priority resolution with provenance
- SceneManifest / PartEntry Pydantic models in products/schemas.py
- GET /api/cad/{id}/scene-manifest endpoint (graceful fallback to
parsed_objects when USD not yet generated)
- POST /api/cad/{id}/generate-usd-master endpoint
- frontend/src/api/sceneManifest.ts: fetchSceneManifest(),
triggerUsdMasterGeneration()
M4 — manual-material-overrides API:
- GET/PUT /api/cad/{id}/manual-material-overrides endpoints
- CadFile.manual_material_overrides JSONB column (migration 061)
- getManualOverrides() / saveManualOverrides() in cad.ts
M5 — ThreeDViewer partKey integration:
- export_step_to_gltf.py injects partKeyMap into GLB extras
- ThreeDViewer: partKeyMap extraction, resolvePartKey(), effectiveMaterials
merges legacy partMaterials + new manualOverrides (server-side persistence)
- MaterialPanel: dual-path save (partKey vs legacy), provenance badge,
reconciliation panel for unmatched/unassigned parts
Also:
- Admin.tsx: generate-missing-usd-masters + canonical scenes bulk actions
- ProductDetail.tsx: usd_master row in asset table
- vite-env.d.ts: fix ImportMeta.env TypeScript error
- GPUProbeResult: add timestamp/devices/render_time_s fields
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -436,6 +436,58 @@ def _tessellate_with_gmsh(shape, linear_deflection: float, angular_deflection: f
|
||||
)
|
||||
|
||||
|
||||
def _collect_part_key_map(shape_tool, free_labels) -> dict:
|
||||
"""Return {normalized_source_name: part_key_slug} for all leaf parts in the XCAF hierarchy.
|
||||
|
||||
The normalized source name (XCAF label name without _AF\\d+ suffix) is what
|
||||
Three.js sees after normalizeMeshName() strips the OCC assembly suffix from the
|
||||
GLB mesh node name. The slug algorithm matches part_key_service.generate_part_key().
|
||||
"""
|
||||
import re as _re
|
||||
import hashlib as _hashlib
|
||||
from OCP.TDF import TDF_LabelSequence
|
||||
from OCP.TDataStd import TDataStd_Name
|
||||
from OCP.XCAFDoc import XCAFDoc_ShapeTool
|
||||
|
||||
_af_re = _re.compile(r'_AF\d+$', _re.IGNORECASE)
|
||||
|
||||
def _slug(source_name: str, xcaf_path: str = "") -> str:
|
||||
base = _af_re.sub('', source_name) if source_name else ''
|
||||
# camelCase split — same as part_key_service.generate_part_key
|
||||
base = _re.sub(r'([a-z])([A-Z])', r'\1_\2', base)
|
||||
slug = _re.sub(r'[^a-z0-9]+', '_', base.lower()).strip('_')
|
||||
if not slug:
|
||||
slug = f"part_{_hashlib.sha256(xcaf_path.encode()).hexdigest()[:8]}"
|
||||
return slug[:50]
|
||||
|
||||
part_key_map: dict = {}
|
||||
|
||||
def _collect(label, path: str = "") -> None:
|
||||
name_attr = TDataStd_Name()
|
||||
name = ""
|
||||
if label.FindAttribute(TDataStd_Name.GetID_s(), name_attr):
|
||||
name = name_attr.Get().ToExtString()
|
||||
|
||||
components = TDF_LabelSequence()
|
||||
XCAFDoc_ShapeTool.GetComponents_s(label, components)
|
||||
|
||||
xcaf_path = f"{path}/{name}" if name else f"{path}/unnamed"
|
||||
|
||||
if components.Length() == 0:
|
||||
# Leaf node — normalized source name (without _AF suffix) as key
|
||||
normalized = _af_re.sub('', name) if name else ''
|
||||
if normalized:
|
||||
part_key_map[normalized] = _slug(name, xcaf_path)
|
||||
else:
|
||||
for i in range(1, components.Length() + 1):
|
||||
_collect(components.Value(i), xcaf_path)
|
||||
|
||||
for i in range(1, free_labels.Length() + 1):
|
||||
_collect(free_labels.Value(i))
|
||||
|
||||
return part_key_map
|
||||
|
||||
|
||||
def _inject_glb_extras(glb_path: Path, extras: dict) -> None:
|
||||
"""Patch a GLB binary to add/update scenes[0].extras JSON field.
|
||||
|
||||
@@ -514,6 +566,10 @@ def main() -> None:
|
||||
print(f"Found {free_labels.Length()} root shape(s), tessellating "
|
||||
f"(linear={args.linear_deflection}mm, angular={args.angular_deflection}rad) …")
|
||||
|
||||
# Collect partKeyMap before tessellation (XCAF names are stable at this point)
|
||||
part_key_map = _collect_part_key_map(shape_tool, free_labels)
|
||||
print(f"partKeyMap: {len(part_key_map)} unique part names collected")
|
||||
|
||||
engine = getattr(args, "tessellation_engine", "occ")
|
||||
if engine == "gmsh":
|
||||
# GMSH: tessellate each solid individually to cap peak RAM usage.
|
||||
@@ -652,18 +708,25 @@ def main() -> None:
|
||||
|
||||
print(f"GLB exported: {out.name} ({out.stat().st_size // 1024} KB)")
|
||||
|
||||
# --- Inject sharp edge pairs into GLB extras ---
|
||||
# --- Inject sharp edge pairs and partKeyMap into GLB extras ---
|
||||
# Blender 5.0 reads scenes[0].extras as scene custom properties on import,
|
||||
# making the data available to export_gltf.py as bpy.context.scene["key"].
|
||||
if sharp_pairs:
|
||||
try:
|
||||
_inject_glb_extras(out, {
|
||||
"schaeffler_sharp_edge_pairs": sharp_pairs,
|
||||
"schaeffler_sharp_threshold_deg": args.sharp_threshold,
|
||||
})
|
||||
print(f"Injected {len(sharp_pairs)} sharp edge segment pairs into GLB extras")
|
||||
except Exception as _exc:
|
||||
print(f"WARNING: GLB extras injection failed (non-fatal): {_exc}", file=sys.stderr)
|
||||
# partKeyMap is read by Three.js in ThreeDViewer to resolve partKey from mesh name.
|
||||
try:
|
||||
extras_payload: dict = {}
|
||||
if sharp_pairs:
|
||||
extras_payload["schaeffler_sharp_edge_pairs"] = sharp_pairs
|
||||
extras_payload["schaeffler_sharp_threshold_deg"] = args.sharp_threshold
|
||||
if part_key_map:
|
||||
extras_payload["partKeyMap"] = part_key_map
|
||||
if extras_payload:
|
||||
_inject_glb_extras(out, extras_payload)
|
||||
if sharp_pairs:
|
||||
print(f"Injected {len(sharp_pairs)} sharp edge segment pairs into GLB extras")
|
||||
if part_key_map:
|
||||
print(f"Injected partKeyMap ({len(part_key_map)} entries) into GLB extras")
|
||||
except Exception as _exc:
|
||||
print(f"WARNING: GLB extras injection failed (non-fatal): {_exc}", file=sys.stderr)
|
||||
|
||||
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user