refactor: remove dead export_gltf.py, cleanup rendering tasks, improve tessellation UI

- Remove export_gltf.py (Blender-based GLB export replaced by OCC direct)
- Remove unused export_gltf_for_order_line_task
- Add Ultra tessellation preset to Admin settings
- Improve tessellation preset descriptions and styling
- Minor cleanup across media, rendering, and workflow modules

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 10:37:35 +01:00
parent d843162e5f
commit ec667dd56a
14 changed files with 106 additions and 478 deletions
-1
View File
@@ -73,7 +73,6 @@ schaefflerautomat/
│ │ ├── blender_render.py # Entry point (68 lines), delegates to _blender_*.py submodules │ │ ├── blender_render.py # Entry point (68 lines), delegates to _blender_*.py submodules
│ │ ├── export_step_to_gltf.py # OCC/GMSH STEP → GLB tessellation │ │ ├── export_step_to_gltf.py # OCC/GMSH STEP → GLB tessellation
│ │ ├── export_step_to_usd.py # OCC STEP → USD canonical scene │ │ ├── export_step_to_usd.py # OCC STEP → USD canonical scene
│ │ ├── export_gltf.py # Blender: materials, seams, sharp edges on GLB
│ │ ├── import_usd.py # Blender: USD import + primvar restoration │ │ ├── import_usd.py # Blender: USD import + primvar restoration
│ │ ├── still_render.py # Blender still render │ │ ├── still_render.py # Blender still render
│ │ └── turntable_render.py # Blender turntable animation │ │ └── turntable_render.py # Blender turntable animation
+1 -2
View File
@@ -27,8 +27,7 @@ class StepName(StrEnum):
BLENDER_TURNTABLE = "blender_turntable" BLENDER_TURNTABLE = "blender_turntable"
OUTPUT_SAVE = "output_save" OUTPUT_SAVE = "output_save"
# ── GLB / asset export ──────────────────────────────────────────── # ── Asset export ──────────────────────────────────────────────────
EXPORT_GLB_GEOMETRY = "export_glb_geometry"
EXPORT_BLEND = "export_blend" EXPORT_BLEND = "export_blend"
# ── STL cache ──────────────────────────────────────────────────── # ── STL cache ────────────────────────────────────────────────────
+2 -2
View File
@@ -14,8 +14,8 @@ class MediaAssetType(str, enum.Enum):
turntable = "turntable" turntable = "turntable"
stl_low = "stl_low" stl_low = "stl_low"
stl_high = "stl_high" stl_high = "stl_high"
gltf_geometry = "gltf_geometry" # DEPRECATED: use usd_master — viewer GLB auto-generated as part of USD pipeline gltf_geometry = "gltf_geometry"
gltf_production = "gltf_production" # DEPRECATED: use usd_master — high-quality production GLB superseded by USD master gltf_production = "gltf_production" # LEGACY — kept for ORM compatibility with existing DB rows, no longer generated
blend_production = "blend_production" blend_production = "blend_production"
usd_master = "usd_master" usd_master = "usd_master"
-1
View File
@@ -142,7 +142,6 @@ async def browse_media_assets(
# Apply filters # Apply filters
_TECHNICAL_TYPES = ( _TECHNICAL_TYPES = (
MediaAssetType.gltf_geometry, MediaAssetType.gltf_geometry,
MediaAssetType.gltf_production,
MediaAssetType.blend_production, MediaAssetType.blend_production,
MediaAssetType.stl_low, MediaAssetType.stl_low,
MediaAssetType.stl_high, MediaAssetType.stl_high,
-46
View File
@@ -506,52 +506,6 @@ def render_order_line_still_task(self, order_line_id: str, **params) -> dict:
raise self.retry(exc=exc, countdown=30) raise self.retry(exc=exc, countdown=30)
@celery_app.task(
bind=True,
name="app.domains.rendering.tasks.export_gltf_for_order_line_task",
queue="asset_pipeline",
max_retries=1,
)
def export_gltf_for_order_line_task(self, order_line_id: str) -> dict:
"""Export a geometry GLB directly from STEP via OCC (no STL intermediary).
Publishes a MediaAsset with asset_type='gltf_geometry'.
"""
import os
import subprocess
import sys
step_path_str, cad_file_id = _resolve_step_path_for_order_line(order_line_id)
if not step_path_str:
raise RuntimeError(f"Cannot resolve STEP path for order_line {order_line_id}")
step = Path(step_path_str)
output_path = step.parent / f"{step.stem}_geometry.glb"
scripts_dir = Path(os.environ.get("RENDER_SCRIPTS_DIR", "/render-scripts"))
occ_script = scripts_dir / "export_step_to_gltf.py"
if not occ_script.exists():
raise RuntimeError(f"export_step_to_gltf.py not found at {occ_script}")
try:
cmd = [
sys.executable, str(occ_script),
"--step_path", str(step),
"--output_path", str(output_path),
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
if result.returncode != 0:
raise RuntimeError(
f"export_step_to_gltf.py exited {result.returncode}:\n{result.stderr[-500:]}"
)
publish_asset.delay(order_line_id, "gltf_geometry", str(output_path))
logger.info("export_gltf_for_order_line_task completed via OCC: %s", output_path.name)
return {"glb_path": str(output_path), "method": "occ"}
except Exception as exc:
logger.error("export_gltf_for_order_line_task failed for %s: %s", order_line_id, exc)
raise self.retry(exc=exc, countdown=15)
@celery_app.task( @celery_app.task(
bind=True, bind=True,
name="app.domains.rendering.tasks.export_blend_for_order_line_task", name="app.domains.rendering.tasks.export_blend_for_order_line_task",
@@ -64,23 +64,16 @@ def _build_multi_angle(order_line_id: str, params: dict):
def _build_still_with_exports(order_line_id: str, params: dict): def _build_still_with_exports(order_line_id: str, params: dict):
"""Still render + parallel GLB exports (geometry + production quality). """Still render + .blend export.
Pipeline: Pipeline:
render_order_line_still_task → group( render_order_line_still_task → export_blend_for_order_line_task
export_gltf_for_order_line_task,
export_blend_for_order_line_task,
)
""" """
from app.domains.rendering.tasks import ( from app.domains.rendering.tasks import (
render_order_line_still_task, render_order_line_still_task,
export_gltf_for_order_line_task,
export_blend_for_order_line_task, export_blend_for_order_line_task,
) )
return chain( return chain(
render_order_line_still_task.si(order_line_id, **params), render_order_line_still_task.si(order_line_id, **params),
group( export_blend_for_order_line_task.si(order_line_id),
export_gltf_for_order_line_task.si(order_line_id),
export_blend_for_order_line_task.si(order_line_id),
),
) )
@@ -45,8 +45,7 @@ STEP_TASK_MAP: dict[StepName, str] = {
# ── Order line stills & turntables ────────────────────────────────── # ── Order line stills & turntables ──────────────────────────────────
StepName.BLENDER_STILL: "app.domains.rendering.tasks.render_order_line_still_task", StepName.BLENDER_STILL: "app.domains.rendering.tasks.render_order_line_still_task",
StepName.BLENDER_TURNTABLE: "app.domains.rendering.tasks.render_turntable_task", StepName.BLENDER_TURNTABLE: "app.domains.rendering.tasks.render_turntable_task",
# ── GLB / asset export ─────────────────────────────────────────────── # ── Asset export ─────────────────────────────────────────────────────
StepName.EXPORT_GLB_GEOMETRY: "app.domains.rendering.tasks.export_gltf_for_order_line_task",
StepName.EXPORT_BLEND: "app.domains.rendering.tasks.export_blend_for_order_line_task", StepName.EXPORT_BLEND: "app.domains.rendering.tasks.export_blend_for_order_line_task",
# ── Steps without a dedicated standalone task (no mapping) ─────────── # ── Steps without a dedicated standalone task (no mapping) ───────────
# StepName.GLB_BBOX — computed inline inside process_step_file # StepName.GLB_BBOX — computed inline inside process_step_file
@@ -51,7 +51,6 @@ _STEP_CATEGORIES: dict[StepName, StepCategory] = {
StepName.BLENDER_STILL: "rendering", StepName.BLENDER_STILL: "rendering",
StepName.BLENDER_TURNTABLE: "rendering", StepName.BLENDER_TURNTABLE: "rendering",
StepName.OUTPUT_SAVE: "output", StepName.OUTPUT_SAVE: "output",
StepName.EXPORT_GLB_GEOMETRY: "output",
StepName.EXPORT_BLEND: "output", StepName.EXPORT_BLEND: "output",
StepName.STL_CACHE_GENERATE: "processing", StepName.STL_CACHE_GENERATE: "processing",
StepName.NOTIFY: "output", StepName.NOTIFY: "output",
@@ -72,7 +71,6 @@ _STEP_DESCRIPTIONS: dict[StepName, str] = {
StepName.BLENDER_STILL: "Render a production still image (PNG) via Blender HTTP micro-service", StepName.BLENDER_STILL: "Render a production still image (PNG) via Blender HTTP micro-service",
StepName.BLENDER_TURNTABLE: "Render all turntable animation frames via Blender HTTP micro-service", StepName.BLENDER_TURNTABLE: "Render all turntable animation frames via Blender HTTP micro-service",
StepName.OUTPUT_SAVE: "Upload the rendered output file to storage and create a MediaAsset record", StepName.OUTPUT_SAVE: "Upload the rendered output file to storage and create a MediaAsset record",
StepName.EXPORT_GLB_GEOMETRY: "Export a geometry-only GLB for the 3-D viewer (no materials)",
StepName.EXPORT_BLEND: "Save the production .blend file as a downloadable MediaAsset", StepName.EXPORT_BLEND: "Save the production .blend file as a downloadable MediaAsset",
StepName.STL_CACHE_GENERATE: "Convert STEP → STL (low + high quality) and cache next to the STEP file", StepName.STL_CACHE_GENERATE: "Convert STEP → STL (low + high quality) and cache next to the STEP file",
StepName.NOTIFY: "Emit a user notification via the audit-log notification channel", StepName.NOTIFY: "Emit a user notification via the audit-log notification channel",
@@ -102,11 +102,6 @@ def test_render_order_line_still_task_importable():
assert render_order_line_still_task.queue == "asset_pipeline" assert render_order_line_still_task.queue == "asset_pipeline"
def test_export_gltf_for_order_line_task_importable():
from app.domains.rendering.tasks import export_gltf_for_order_line_task
assert export_gltf_for_order_line_task.queue == "asset_pipeline"
def test_export_blend_for_order_line_task_importable(): def test_export_blend_for_order_line_task_importable():
from app.domains.rendering.tasks import export_blend_for_order_line_task from app.domains.rendering.tasks import export_blend_for_order_line_task
assert export_blend_for_order_line_task.queue == "asset_pipeline" assert export_blend_for_order_line_task.queue == "asset_pipeline"
-1
View File
@@ -7,7 +7,6 @@ export type MediaAssetType =
| 'stl_low' | 'stl_low'
| 'stl_high' | 'stl_high'
| 'gltf_geometry' | 'gltf_geometry'
| 'gltf_production'
| 'usd_master' | 'usd_master'
| 'blend_production' | 'blend_production'
+88 -46
View File
@@ -1454,7 +1454,7 @@ export default function AdminPage() {
<div className="p-4 border-b border-border-default"> <div className="p-4 border-b border-border-default">
<h2 className="font-semibold text-content">Tessellation Quality</h2> <h2 className="font-semibold text-content">Tessellation Quality</h2>
<p className="text-sm text-content-muted mt-0.5"> <p className="text-sm text-content-muted mt-0.5">
OCC mesh precision for GLB export. Lower values = finer mesh + larger files + slower export. Controls how STEP geometry is converted to triangle meshes. These settings affect both the 3D viewer and Blender renders.
</p> </p>
</div> </div>
<div className="p-4 space-y-6"> <div className="p-4 space-y-6">
@@ -1463,48 +1463,70 @@ export default function AdminPage() {
const PRESETS = [ const PRESETS = [
{ {
label: 'Draft', label: 'Draft',
description: 'Fast export, visible faceting on large curves', icon: '',
color: 'border-amber-400 text-amber-700', description: 'Fast preview visible faceting on curved surfaces',
useCase: 'Quick checks, large assemblies',
color: 'border-amber-400',
activeColor: 'border-amber-500 ring-2 ring-amber-200',
values: { scene_linear_deflection: 0.2, scene_angular_deflection: 0.3, render_linear_deflection: 0.05, render_angular_deflection: 0.1 }, values: { scene_linear_deflection: 0.2, scene_angular_deflection: 0.3, render_linear_deflection: 0.05, render_angular_deflection: 0.1 },
}, },
{ {
label: 'Standard', label: 'Standard',
description: 'Smooth curves, no fan artifacts recommended', icon: '',
color: 'border-blue-400 text-blue-700', description: 'Smooth curves, good quality-to-size ratio',
useCase: 'Recommended for most parts',
color: 'border-blue-400',
activeColor: 'border-blue-500 ring-2 ring-blue-200',
values: { scene_linear_deflection: 0.1, scene_angular_deflection: 0.1, render_linear_deflection: 0.03, render_angular_deflection: 0.05 }, values: { scene_linear_deflection: 0.1, scene_angular_deflection: 0.1, render_linear_deflection: 0.03, render_angular_deflection: 0.05 },
}, },
{ {
label: 'Fine', label: 'Fine',
description: 'Maximum quality, very large files, slow export', icon: '',
color: 'border-emerald-400 text-emerald-700', description: 'Near-perfect surfaces, 3-5x larger files',
useCase: 'Close-up renders, small precision parts',
color: 'border-emerald-400',
activeColor: 'border-emerald-500 ring-2 ring-emerald-200',
values: { scene_linear_deflection: 0.05, scene_angular_deflection: 0.05, render_linear_deflection: 0.01, render_angular_deflection: 0.02 }, values: { scene_linear_deflection: 0.05, scene_angular_deflection: 0.05, render_linear_deflection: 0.01, render_angular_deflection: 0.02 },
}, },
{
label: 'Ultra',
icon: '',
description: 'Maximum fidelity, very slow export',
useCase: 'Marketing renders, extreme close-ups',
color: 'border-purple-400',
activeColor: 'border-purple-500 ring-2 ring-purple-200',
values: { scene_linear_deflection: 0.02, scene_angular_deflection: 0.02, render_linear_deflection: 0.005, render_angular_deflection: 0.01 },
},
] ]
const isActive = (preset: typeof PRESETS[0]) => const isActive = (preset: typeof PRESETS[0]) =>
tess.scene_linear_deflection === preset.values.scene_linear_deflection && tess.scene_linear_deflection === preset.values.scene_linear_deflection &&
tess.scene_angular_deflection === preset.values.scene_angular_deflection && tess.scene_angular_deflection === preset.values.scene_angular_deflection &&
tess.render_linear_deflection === preset.values.render_linear_deflection && tess.render_linear_deflection === preset.values.render_linear_deflection &&
tess.render_angular_deflection === preset.values.render_angular_deflection tess.render_angular_deflection === preset.values.render_angular_deflection
const isCustom = !PRESETS.some(isActive)
return ( return (
<div> <div>
<p className="text-xs font-semibold text-content-secondary uppercase tracking-wide mb-2">Presets</p> <p className="text-xs font-semibold text-content-secondary uppercase tracking-wide mb-2">Quality Presets</p>
<div className="flex gap-3"> <div className="grid grid-cols-4 gap-3">
{PRESETS.map(preset => ( {PRESETS.map(preset => (
<button <button
key={preset.label} key={preset.label}
onClick={() => setTessellationDraft(preset.values)} onClick={() => setTessellationDraft(preset.values)}
className={`flex-1 p-3 rounded-lg border-2 text-left transition-colors ${isActive(preset) ? preset.color + ' bg-opacity-10' : 'border-border-default text-content hover:border-blue-300'}`} className={`p-3 rounded-lg border-2 text-left transition-all ${isActive(preset) ? preset.activeColor : preset.color + ' opacity-60 hover:opacity-100'}`}
style={isActive(preset) ? { backgroundColor: 'var(--color-bg-surface-alt)' } : undefined} style={isActive(preset) ? { backgroundColor: 'var(--color-bg-surface-alt)' } : undefined}
> >
<div className="font-semibold text-sm">{preset.label}</div> <div className="flex items-center gap-1.5">
<div className="text-xs text-content-muted mt-0.5">{preset.description}</div> <span className="text-base">{preset.icon}</span>
<div className="text-xs font-mono text-content-secondary mt-1 space-y-0.5"> <span className="font-semibold text-sm">{preset.label}</span>
<div>scene: {preset.values.scene_angular_deflection} rad / {preset.values.scene_linear_deflection} mm</div>
<div>render: {preset.values.render_angular_deflection} rad / {preset.values.render_linear_deflection} mm</div>
</div> </div>
<div className="text-xs text-content-muted mt-1">{preset.description}</div>
<div className="text-xs text-content-secondary mt-1.5 italic">{preset.useCase}</div>
</button> </button>
))} ))}
</div> </div>
{isCustom && (
<p className="text-xs text-amber-600 mt-2">Current values don't match any preset (custom configuration)</p>
)}
</div> </div>
) )
})()} })()}
@@ -1512,27 +1534,40 @@ export default function AdminPage() {
{/* Tessellation engine selector */} {/* Tessellation engine selector */}
<div className="space-y-2"> <div className="space-y-2">
<p className="text-xs font-semibold text-content-secondary uppercase tracking-wide">Tessellation Engine</p> <p className="text-xs font-semibold text-content-secondary uppercase tracking-wide">Tessellation Engine</p>
<div className="flex items-start gap-4"> <div className="flex flex-col gap-2">
<div className="flex flex-col gap-2"> {[
{[ { value: 'occ', label: 'OCC BRepMesh', description: 'Default engine. Fast, but produces fan-shaped triangles at cylinder seam lines.' },
{ value: 'occ', label: 'OCC BRepMesh', description: 'Default engine. Fast, but produces fan triangles at cylinder seam edges.' }, { value: 'gmsh', label: 'GMSH Frontal-Delaunay', description: 'Uniform mesh — eliminates fan artifacts on cylindrical parts. 10-30% slower. Recommended for bearings.' },
{ value: 'gmsh', label: 'GMSH Frontal-Delaunay', description: 'Conforming mesh no fan triangles on cylinders. +1030% export time. Recommended for cylindrical parts.' }, ].map(opt => (
].map(opt => ( <label key={opt.value} className="flex items-start gap-3 cursor-pointer p-3 rounded-lg border border-border-default hover:border-blue-400 transition-colors">
<label key={opt.value} className="flex items-start gap-3 cursor-pointer p-3 rounded-lg border border-border-default hover:border-blue-400 transition-colors"> <input
<input type="radio"
type="radio" name="tessellation_engine"
name="tessellation_engine" value={opt.value}
value={opt.value} checked={(tess.tessellation_engine ?? 'occ') === opt.value}
checked={(tess.tessellation_engine ?? 'occ') === opt.value} onChange={() => setTessellationDraft(d => ({ ...d, tessellation_engine: opt.value }))}
onChange={() => setTessellationDraft(d => ({ ...d, tessellation_engine: opt.value }))} className="mt-0.5 shrink-0"
className="mt-0.5 shrink-0" />
/> <div>
<div> <div className="text-sm font-medium">{opt.label}</div>
<div className="text-sm font-medium">{opt.label}</div> <div className="text-xs text-content-muted mt-0.5">{opt.description}</div>
<div className="text-xs text-content-muted mt-0.5">{opt.description}</div> </div>
</div> </label>
</label> ))}
))} </div>
</div>
{/* Explanation of deflection parameters */}
<div className="rounded-lg border border-border-default p-4 space-y-3" style={{ backgroundColor: 'var(--color-bg-surface-alt)' }}>
<p className="text-xs font-semibold text-content-secondary uppercase tracking-wide">How deflection values work</p>
<div className="grid grid-cols-2 gap-4 text-xs text-content-muted">
<div>
<p className="font-medium text-content-secondary mb-1">Linear deflection (mm)</p>
<p>Maximum allowed distance between the original curved surface and the generated triangles. A value of 0.1 mm means no triangle edge can deviate more than 0.1 mm from the true surface. Lower values produce smoother curves but more triangles.</p>
</div>
<div>
<p className="font-medium text-content-secondary mb-1">Angular deflection (rad)</p>
<p>Maximum angle between adjacent triangle normals. Controls how finely curved regions are subdivided. A value of 0.1 rad (~6°) means neighboring triangles can differ by at most ~6°. Primarily affects small fillets and tight curvatures.</p>
</div> </div>
</div> </div>
</div> </div>
@@ -1542,14 +1577,17 @@ export default function AdminPage() {
className="text-xs text-accent hover:underline flex items-center gap-1 mt-1" className="text-xs text-accent hover:underline flex items-center gap-1 mt-1"
> >
{showAdvancedTess ? <ChevronDown size={12} /> : <ChevronRight size={12} />} {showAdvancedTess ? <ChevronDown size={12} /> : <ChevronRight size={12} />}
{showAdvancedTess ? 'Hide manual values' : 'Advanced: manual deflection values'} {showAdvancedTess ? 'Hide manual values' : 'Advanced: edit values manually'}
</button> </button>
{/* Manual inputs */} {/* Manual inputs */}
{showAdvancedTess && (<> {showAdvancedTess && (<>
<div className="grid grid-cols-2 gap-6"> <div className="grid grid-cols-2 gap-6">
<div className="space-y-4"> <div className="space-y-4">
<p className="text-xs font-semibold text-content-secondary uppercase tracking-wide">Scene (USD Master)</p> <div>
<p className="text-xs font-semibold text-content-secondary uppercase tracking-wide">3D Viewer + USD Master</p>
<p className="text-xs text-content-muted mt-0.5">Used for the interactive 3D viewer GLB and the canonical USD scene file. Optimized for real-time display.</p>
</div>
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<label className="text-sm text-content-secondary w-36 shrink-0">Linear deflection</label> <label className="text-sm text-content-secondary w-36 shrink-0">Linear deflection</label>
<input <input
@@ -1576,10 +1614,12 @@ export default function AdminPage() {
/> />
<span className="text-sm text-content-muted">rad</span> <span className="text-sm text-content-muted">rad</span>
</div> </div>
<p className="text-xs text-content-muted">Used for the USD master + 3D viewer GLB (canonical scene). Smaller = smoother surfaces.</p>
</div> </div>
<div className="space-y-4"> <div className="space-y-4">
<p className="text-xs font-semibold text-content-secondary uppercase tracking-wide">Render output</p> <div>
<p className="text-xs font-semibold text-content-secondary uppercase tracking-wide">Blender Render Output</p>
<p className="text-xs text-content-muted mt-0.5">Used for final Blender renders (stills, turntables). Higher quality since render time matters more than file size.</p>
</div>
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<label className="text-sm text-content-secondary w-36 shrink-0">Linear deflection</label> <label className="text-sm text-content-secondary w-36 shrink-0">Linear deflection</label>
<input <input
@@ -1606,7 +1646,6 @@ export default function AdminPage() {
/> />
<span className="text-sm text-content-muted">rad</span> <span className="text-sm text-content-muted">rad</span>
</div> </div>
<p className="text-xs text-content-muted">Used for final render output. Smaller = smoother surfaces, larger file sizes.</p>
</div> </div>
</div> </div>
</>)} </>)}
@@ -1983,11 +2022,14 @@ function AssetLibraryPanel() {
<div> <div>
<p className="text-xs font-medium text-content-muted mb-1">Materials</p> <p className="text-xs font-medium text-content-muted mb-1">Materials</p>
<div className="flex flex-wrap gap-1"> <div className="flex flex-wrap gap-1">
{lib.catalog.materials.map((m) => ( {lib.catalog.materials.map((m) => {
<span key={m} className="text-xs px-2 py-0.5 rounded bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200"> const name = typeof m === 'string' ? m : m.name
{m} return (
</span> <span key={name} className="text-xs px-2 py-0.5 rounded bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200">
))} {name}
</span>
)
})}
</div> </div>
</div> </div>
)} )}
+1 -3
View File
@@ -30,7 +30,6 @@ const TYPE_COLORS: Partial<Record<MediaAssetType, string>> = {
stl_low: 'badge-yellow', stl_low: 'badge-yellow',
stl_high: 'badge-orange', stl_high: 'badge-orange',
gltf_geometry: 'badge-green', gltf_geometry: 'badge-green',
gltf_production: 'badge-teal',
blend_production: 'badge-purple', blend_production: 'badge-purple',
} }
@@ -43,7 +42,6 @@ const ASSET_TYPES_MEDIA = [
const ASSET_TYPES_TECHNICAL = [ const ASSET_TYPES_TECHNICAL = [
{ value: 'gltf_geometry', label: 'glTF Geometry' }, { value: 'gltf_geometry', label: 'glTF Geometry' },
{ value: 'gltf_production', label: 'glTF Production' },
{ value: 'blend_production', label: 'Blend (.blend)' }, { value: 'blend_production', label: 'Blend (.blend)' },
{ value: 'stl_low', label: 'STL Low' }, { value: 'stl_low', label: 'STL Low' },
{ value: 'stl_high', label: 'STL High' }, { value: 'stl_high', label: 'STL High' },
@@ -76,7 +74,7 @@ function TypeIcon({ type, size = 32 }: { type: MediaAssetType; size?: number })
if (type === 'still' || type === 'thumbnail') return <Image size={size} className="text-content-muted" /> if (type === 'still' || type === 'thumbnail') return <Image size={size} className="text-content-muted" />
if (type === 'turntable') return <Film size={size} className="text-content-muted" /> if (type === 'turntable') return <Film size={size} className="text-content-muted" />
if (type === 'stl_low' || type === 'stl_high') return <Box size={size} className="text-content-muted" /> if (type === 'stl_low' || type === 'stl_high') return <Box size={size} className="text-content-muted" />
if (type === 'gltf_geometry' || type === 'gltf_production') return <FileCode2 size={size} className="text-content-muted" /> if (type === 'gltf_geometry') return <FileCode2 size={size} className="text-content-muted" />
return <Layers size={size} className="text-content-muted" /> return <Layers size={size} className="text-content-muted" />
} }
-356
View File
@@ -1,356 +0,0 @@
"""Blender headless script: export a STEP-derived scene as a production GLB.
Usage:
blender --background --python export_gltf.py -- \\
--stl_path /path/to/file.stl \\
--output_path /path/to/output.glb \\
[--asset_library_blend /path/to/library.blend] \\
[--material_map '{"SrcMat": "LibMat"}']
The script:
1. Imports the STL file (with mm→m scale).
2. Optionally applies asset library materials from a .blend.
3. Exports as GLB (Draco-compressed if available, otherwise standard).
"""
from __future__ import annotations
import argparse
import json
import sys
import traceback
FAILED_MATERIAL_NAME = "SCHAEFFLER_059999_FailedMaterial"
def parse_args() -> argparse.Namespace:
argv = sys.argv
if "--" not in argv:
print("No arguments after --", file=sys.stderr)
sys.exit(1)
rest = argv[argv.index("--") + 1:]
parser = argparse.ArgumentParser()
parser.add_argument("--glb_path", required=True,
help="Geometry GLB from export_step_to_gltf.py (already in metres)")
parser.add_argument("--output_path", required=True)
parser.add_argument("--asset_library_blend", default=None)
parser.add_argument("--material_map", default="{}")
parser.add_argument("--smooth_angle", type=float, default=30.0,
help="Auto-smooth angle in degrees (default 30)")
parser.add_argument("--mesh_attributes", default="{}",
help="JSON dict from cad_file.mesh_attributes (sharp_edge_pairs etc.)")
return parser.parse_args(rest)
def _apply_sharp_edges_from_occ(mesh_objects: list, sharp_edge_pairs: list) -> None:
"""Mark edges sharp using OCC vertex-pair data (same approach as blender_render.py).
sharp_edge_pairs: [[x0,y0,z0],[x1,y1,z1]] in mm.
Blender mesh coords are in metres (×0.001 scale already applied by OCC export).
"""
if not sharp_edge_pairs:
return
import bmesh
import mathutils
SCALE = 0.001 # mm → m
TOL = 0.0005 # 0.5 mm tolerance in metres
# OCC STEP space (Z-up, mm) → Blender (Z-up, m):
# RWGltf applies Z→Y-up, Blender import applies Y→Z-up.
# Net: Blender(X, Y, Z) = OCC(X*0.001, -Z*0.001, Y*0.001)
occ_pairs = []
for pair in sharp_edge_pairs:
v0 = mathutils.Vector((pair[0][0] * SCALE, -pair[0][2] * SCALE, pair[0][1] * SCALE))
v1 = mathutils.Vector((pair[1][0] * SCALE, -pair[1][2] * SCALE, pair[1][1] * SCALE))
occ_pairs.append((v0, v1))
marked_total = 0
for obj in mesh_objects:
bm = bmesh.new()
bm.from_mesh(obj.data)
bm.verts.ensure_lookup_table()
bm.edges.ensure_lookup_table()
# Build KD-tree in WORLD space — OCC pairs are world coords, but mesh
# vertices are in local space (assembly node transform in GLB hierarchy).
world_mat = obj.matrix_world
kd = mathutils.kdtree.KDTree(len(bm.verts))
for v in bm.verts:
kd.insert(world_mat @ v.co, v.index)
kd.balance()
marked = 0
for v0_occ, v1_occ in occ_pairs:
_co0, idx0, dist0 = kd.find(v0_occ)
_co1, idx1, dist1 = kd.find(v1_occ)
if dist0 > TOL or dist1 > TOL:
continue
if idx0 == idx1:
continue # degenerate — both endpoints map to same vertex
bv0, bv1 = bm.verts[idx0], bm.verts[idx1]
edge = bm.edges.get((bv0, bv1)) or bm.edges.get((bv1, bv0))
if edge is not None:
# Mark sharp (for normal splitting) AND seam (for UV unwrap).
# Both are needed: sharp controls glTF vertex splits / shading;
# seam defines UV island boundaries for correct UV unwrapping.
edge.smooth = False
edge.seam = True
marked += 1
bm.to_mesh(obj.data)
bm.free()
marked_total += marked
print(f"OCC sharp edges applied: {marked_total} edges marked across {len(mesh_objects)} objects")
def main() -> None:
args = parse_args()
material_map: dict = json.loads(args.material_map)
mesh_attributes: dict = json.loads(args.mesh_attributes)
import bpy # type: ignore[import]
import math as _math
import re as _re
# Clean scene
bpy.ops.wm.read_factory_settings(use_empty=True)
# Import geometry GLB from export_step_to_gltf.py (already in metres, Y-up)
bpy.ops.import_scene.gltf(filepath=args.glb_path)
mesh_objects = [o for o in bpy.data.objects if o.type == "MESH"]
print(f"Imported geometry GLB: {args.glb_path} ({len(mesh_objects)} mesh objects)")
# Read OCC sharp edge pairs embedded by export_step_to_gltf.py into GLB extras.
# Blender 5.0 maps glTF scenes[0].extras as scene custom properties on import.
# These take priority over the mesh_attributes CLI argument (which only has 2
# endpoints per edge — see V02 refactor for why this matters).
glb_sharp_pairs = bpy.context.scene.get("schaeffler_sharp_edge_pairs") or []
if glb_sharp_pairs:
print(f"Loaded {len(glb_sharp_pairs)} OCC sharp edge pairs from GLB extras")
# Remove OCC-baked custom normals from the geometry GLB.
# RWGltf_CafWriter embeds per-corner normals from OCC tessellation as a
# 'custom_normal' attribute (CORNER, INT16_2D). If left in place, Blender's
# glTF exporter re-exports these pre-baked normals unchanged, ignoring our
# shade_smooth_by_angle processing and sharp edge marks entirely.
# Removing this attribute forces Blender to recompute normals from scratch.
cleared_normals = 0
for obj in mesh_objects:
if "custom_normal" in obj.data.attributes:
obj.data.attributes.remove(obj.data.attributes["custom_normal"])
cleared_normals += 1
if cleared_normals:
print(f"Cleared OCC custom_normal attribute from {cleared_normals} mesh objects")
# Mark sharp edges and seams using the configured angle threshold.
# We use Blender's edit-mode operators (mark_sharp + mark_seam) rather than
# shade_smooth_by_angle alone, because:
# 1. mark_sharp() sets the sharp_edge boolean attribute on edges — the glTF
# exporter creates vertex splits (duplicate vertices with different normals)
# at sharp edges, which is how glTF encodes hard edges.
# 2. mark_seam() ensures UV splits at the same edges (stepper-addon behaviour).
# Note: calc_normals_split() was removed in Blender 5.0 — not needed here
# because export_apply=True triggers vertex splitting automatically.
smooth_rad = _math.radians(args.smooth_angle)
print(f"Marking sharp edges + seams at {args.smooth_angle}° ({smooth_rad:.3f} rad)")
bpy.ops.object.select_all(action='DESELECT')
total_sharp = 0
for obj in mesh_objects:
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
# Set all faces smooth
bpy.ops.object.mode_set(mode='OBJECT')
for poly in obj.data.polygons:
poly.use_smooth = True
# Enter edit mode, deselect, select sharp edges by angle, mark sharp+seam
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.edges_select_sharp(sharpness=smooth_rad)
bpy.ops.mesh.mark_sharp()
bpy.ops.mesh.mark_seam()
bpy.ops.object.mode_set(mode='OBJECT')
# Count how many edges were marked
n_sharp = sum(1 for e in obj.data.edges if e.use_edge_sharp)
total_sharp += n_sharp
obj.select_set(False)
print(f"Marked {total_sharp} sharp/seam edges across {len(mesh_objects)} objects")
# Apply OCC sharp edges from GLB extras (V02: dense tessellation segment pairs).
# Prefer GLB-embedded pairs over mesh_attributes CLI argument — the GLB extras
# contain the full tessellated polyline for each sharp B-rep edge (all intermediate
# points), while mesh_attributes only has 2 endpoints per edge (too sparse for
# reliable KD-tree matching). Fall back to mesh_attributes if GLB extras absent.
occ_pairs = list(glb_sharp_pairs) or (mesh_attributes.get("sharp_edge_pairs") or [])
if occ_pairs:
_apply_sharp_edges_from_occ(mesh_objects, occ_pairs)
# Apply asset library materials if provided.
# link=False (append) is required: the GLTF exporter can only traverse
# local (appended) Principled BSDF node trees to extract PBR values.
#
# Matching strategy (mirrors blender_render.py):
# Build mat_map_lower with BOTH the original key AND the _AF-stripped key,
# so keys like "RingOuter_AF0" match object names "RingOuter" and vice-versa.
# Object names from RWGltf_CafWriter preserve the original STEP part name
# (including any _AF suffixes), so we strip from both sides.
if args.asset_library_blend and material_map:
mat_map_lower: dict = {}
for k, v in material_map.items():
kl = k.lower().strip()
mat_map_lower[kl] = v
# Also add the _AF-stripped version so either form matches
stripped = kl
prev = None
while prev != stripped:
prev = stripped
stripped = _re.sub(r'_af\d+$', '', stripped)
if stripped != kl:
mat_map_lower.setdefault(stripped, v)
needed = set(mat_map_lower.values())
# Append materials from library (link=False so glTF exporter can read nodes)
appended: dict = {}
for mat_name in needed:
try:
bpy.ops.wm.append(
filepath=f"{args.asset_library_blend}/Material/{mat_name}",
directory=f"{args.asset_library_blend}/Material/",
filename=mat_name,
link=False,
)
if mat_name in bpy.data.materials:
appended[mat_name] = bpy.data.materials[mat_name]
print(f"Appended material: {mat_name}")
else:
print(f"WARNING: material '{mat_name}' not found in library after append",
file=sys.stderr)
except Exception as exc:
print(f"WARNING: failed to append material '{mat_name}': {exc}", file=sys.stderr)
if appended:
assigned = 0
assigned_names: set = set()
for obj in mesh_objects:
# Strip Blender's .001/.002 deduplication suffix
base_name = _re.sub(r'\.\d{3}$', '', obj.name)
# Also strip _AF suffix from object name so both directions match
prev = None
while prev != base_name:
prev = base_name
base_name = _re.sub(r'_AF\d+$', '', base_name, flags=_re.IGNORECASE)
lower_base = base_name.lower().strip()
mat_name = mat_map_lower.get(lower_base)
# Prefix fallback for sub-assembly nodes
if not mat_name:
for key, val in sorted(mat_map_lower.items(), key=lambda x: len(x[0]), reverse=True):
if len(key) >= 3 and len(lower_base) >= 3 and (
lower_base.startswith(key) or key.startswith(lower_base)
):
mat_name = val
break
if mat_name and mat_name in appended:
# Make mesh data single-user before modifying material slots;
# otherwise clearing materials on a shared data block removes
# slots from ALL objects that share it.
if obj.data.users > 1:
obj.data = obj.data.copy()
obj.data.materials.clear()
obj.data.materials.append(appended[mat_name])
assigned += 1
assigned_names.add(obj.name)
else:
pass # unmatched → will receive FailedMaterial sentinel below
print(f"Material substitution: {assigned}/{len(mesh_objects)} mesh objects assigned")
# Universal FailedMaterial sentinel: assign SCHAEFFLER_059999_FailedMaterial
# to every mesh object that was not matched by name-based lookup above.
# Replaces the old single-material fallback that only fired when len(appended)==1.
failed_mat = None
try:
bpy.ops.wm.append(
filepath=f"{args.asset_library_blend}/Material/{FAILED_MATERIAL_NAME}",
directory=f"{args.asset_library_blend}/Material/",
filename=FAILED_MATERIAL_NAME,
link=False,
)
if FAILED_MATERIAL_NAME in bpy.data.materials:
failed_mat = bpy.data.materials[FAILED_MATERIAL_NAME]
print(f"Appended sentinel material: {FAILED_MATERIAL_NAME}")
else:
print(f"WARNING: sentinel '{FAILED_MATERIAL_NAME}' not found in library — "
f"creating in-memory magenta fallback", file=sys.stderr)
except Exception as exc:
print(f"WARNING: failed to append sentinel '{FAILED_MATERIAL_NAME}': {exc}",
file=sys.stderr)
if failed_mat is None:
# Library append failed: create in-memory magenta so export is never silently wrong
failed_mat = bpy.data.materials.new(name=FAILED_MATERIAL_NAME)
failed_mat.use_nodes = True
bsdf = failed_mat.node_tree.nodes.get("Principled BSDF")
if bsdf:
bsdf.inputs["Base Color"].default_value = (1.0, 0.0, 1.0, 1.0) # magenta
fallback_count = 0
for obj in mesh_objects:
if obj.name not in assigned_names:
if obj.data.users > 1:
obj.data = obj.data.copy()
obj.data.materials.clear()
obj.data.materials.append(failed_mat)
fallback_count += 1
if fallback_count:
print(f"FailedMaterial sentinel: assigned '{FAILED_MATERIAL_NAME}' "
f"to {fallback_count} unmatched objects")
# Purge orphan data-blocks (palette materials mat_0/mat_1/... from the geometry
# GLB that now have users=0 after library material substitution).
# This prevents stale materials from appearing as duplicates in the export.
try:
bpy.ops.outliner.orphans_purge(do_recursive=True)
except Exception:
pass # non-critical; export proceeds regardless
# Store the sharp angle in the scene so it is embedded in the GLB extras.
# After importing the production GLB in Blender, running restore_sharp_marks.py
# reads this value and re-applies mark_sharp()+mark_seam() on all mesh objects.
bpy.context.scene["schaeffler_sharp_angle_deg"] = args.smooth_angle
# Export production GLB with full PBR material data.
# export_extras=True embeds scene custom properties (incl. schaeffler_sharp_angle_deg)
# in the glTF scenes[0].extras JSON field, surviving the round-trip intact.
try:
bpy.ops.export_scene.gltf(
filepath=args.output_path,
export_format="GLB",
export_apply=True,
use_selection=False,
export_materials="EXPORT",
export_image_format="AUTO",
export_extras=True,
)
except Exception as exc:
print(f"GLB export failed: {exc}", file=sys.stderr)
sys.exit(1)
print(f"Production GLB exported to {args.output_path}")
try:
main()
except SystemExit:
raise
except Exception:
traceback.print_exc()
sys.exit(1)
+10 -1
View File
@@ -468,8 +468,17 @@ def _collect_part_key_map(shape_tool, free_labels) -> dict:
if label.FindAttribute(TDataStd_Name.GetID_s(), name_attr): if label.FindAttribute(TDataStd_Name.GetID_s(), name_attr):
name = name_attr.Get().ToExtString() name = name_attr.Get().ToExtString()
# Dereference component references to their definition label
# (the definition may itself be an assembly with sub-components)
from OCP.TDF import TDF_Label as _TDF_Label
actual_label = label
if XCAFDoc_ShapeTool.IsReference_s(label):
ref_label = _TDF_Label()
if XCAFDoc_ShapeTool.GetReferredShape_s(label, ref_label):
actual_label = ref_label
components = TDF_LabelSequence() components = TDF_LabelSequence()
XCAFDoc_ShapeTool.GetComponents_s(label, components) XCAFDoc_ShapeTool.GetComponents_s(actual_label, components)
xcaf_path = f"{path}/{name}" if name else f"{path}/unnamed" xcaf_path = f"{path}/{name}" if name else f"{path}/unnamed"