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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import argparse
|
| 3 |
+
import json
|
| 4 |
+
from datetime import date
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def parse_simple_yaml(path: Path):
|
| 12 |
+
data = {}
|
| 13 |
+
current_key = None
|
| 14 |
+
for raw in path.read_text().splitlines():
|
| 15 |
+
if not raw.strip() or raw.lstrip().startswith("#"):
|
| 16 |
+
continue
|
| 17 |
+
if raw.startswith(" - ") and current_key:
|
| 18 |
+
data.setdefault(current_key, []).append(raw.strip()[2:].strip())
|
| 19 |
+
continue
|
| 20 |
+
if ":" in raw and not raw.startswith(" "):
|
| 21 |
+
key, value = raw.split(":", 1)
|
| 22 |
+
key = key.strip()
|
| 23 |
+
value = value.strip()
|
| 24 |
+
current_key = key
|
| 25 |
+
if value == "":
|
| 26 |
+
data[key] = []
|
| 27 |
+
else:
|
| 28 |
+
data[key] = value.strip('"').strip("'")
|
| 29 |
+
return data
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def yaml_scalar(value):
|
| 33 |
+
if isinstance(value, bool):
|
| 34 |
+
return "true" if value else "false"
|
| 35 |
+
if value is None:
|
| 36 |
+
return "null"
|
| 37 |
+
if isinstance(value, (int, float)):
|
| 38 |
+
return str(value)
|
| 39 |
+
text = str(value)
|
| 40 |
+
if text == "":
|
| 41 |
+
return '""'
|
| 42 |
+
if any(ch in text for ch in [":", "#", "{", "}", "[", "]", ",", "\"", "'", "\n"]) or text.startswith(" ") or text.endswith(" "):
|
| 43 |
+
return json.dumps(text, ensure_ascii=False)
|
| 44 |
+
return text
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def dump_yaml(mapping, indent=0):
|
| 48 |
+
lines = []
|
| 49 |
+
pad = " " * indent
|
| 50 |
+
for key, value in mapping.items():
|
| 51 |
+
if isinstance(value, dict):
|
| 52 |
+
lines.append(f"{pad}{key}:")
|
| 53 |
+
lines.extend(dump_yaml(value, indent + 2))
|
| 54 |
+
elif isinstance(value, list):
|
| 55 |
+
if not value:
|
| 56 |
+
lines.append(f"{pad}{key}: []")
|
| 57 |
+
else:
|
| 58 |
+
lines.append(f"{pad}{key}:")
|
| 59 |
+
for item in value:
|
| 60 |
+
if isinstance(item, dict):
|
| 61 |
+
lines.append(f"{pad} -")
|
| 62 |
+
lines.extend(dump_yaml(item, indent + 4))
|
| 63 |
+
else:
|
| 64 |
+
lines.append(f"{pad} - {yaml_scalar(item)}")
|
| 65 |
+
else:
|
| 66 |
+
lines.append(f"{pad}{key}: {yaml_scalar(value)}")
|
| 67 |
+
return lines
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def main():
|
| 71 |
+
parser = argparse.ArgumentParser(description="Scaffold a derived convex decomposition asset directory.")
|
| 72 |
+
parser.add_argument("raw_asset_dir", help="Path to a raw asset directory under assets/<source>/raw/...")
|
| 73 |
+
parser.add_argument("--variant", default="vhacd_v1", help="Derived asset variant suffix.")
|
| 74 |
+
parser.add_argument("--tool", default="vhacd", help="Convex decomposition tool name.")
|
| 75 |
+
parser.add_argument("--tool-version", default="TBD", help="Convex decomposition tool version.")
|
| 76 |
+
args = parser.parse_args()
|
| 77 |
+
|
| 78 |
+
raw_asset_dir = Path(args.raw_asset_dir).resolve()
|
| 79 |
+
raw_rel = raw_asset_dir.relative_to(ROOT / "assets")
|
| 80 |
+
parts = raw_rel.parts
|
| 81 |
+
if len(parts) < 5 or parts[1] != "raw":
|
| 82 |
+
raise SystemExit("raw_asset_dir must be under assets/<source>/raw/<asset_type>/<category>/<asset_id>")
|
| 83 |
+
|
| 84 |
+
source = parts[0]
|
| 85 |
+
asset_type = parts[2]
|
| 86 |
+
category = parts[3]
|
| 87 |
+
asset_id = parts[4]
|
| 88 |
+
|
| 89 |
+
raw_meta_path = raw_asset_dir / "metadata.yaml"
|
| 90 |
+
raw_meta = parse_simple_yaml(raw_meta_path) if raw_meta_path.exists() else {}
|
| 91 |
+
license_name = raw_meta.get("license", "unknown")
|
| 92 |
+
origin_url = raw_meta.get("origin_url", "")
|
| 93 |
+
source_commit = raw_meta.get("source_commit")
|
| 94 |
+
|
| 95 |
+
derived_asset_id = f"{asset_id}_{args.variant}"
|
| 96 |
+
derived_dir = ROOT / "assets" / source / "derived" / "convex_decompositions" / category / derived_asset_id
|
| 97 |
+
if derived_dir.exists():
|
| 98 |
+
raise SystemExit(f"destination already exists: {derived_dir}")
|
| 99 |
+
|
| 100 |
+
(derived_dir / "meshes").mkdir(parents=True, exist_ok=False)
|
| 101 |
+
(derived_dir / "mjcf").mkdir(parents=True, exist_ok=False)
|
| 102 |
+
(derived_dir / "logs").mkdir(parents=True, exist_ok=False)
|
| 103 |
+
|
| 104 |
+
metadata = {
|
| 105 |
+
"asset_id": f"{source}.convex_decompositions.{category}.{derived_asset_id}",
|
| 106 |
+
"source": source,
|
| 107 |
+
"source_asset_id": asset_id,
|
| 108 |
+
"asset_type": "convex_decompositions",
|
| 109 |
+
"category": category,
|
| 110 |
+
"format": "obj_mjcf_include",
|
| 111 |
+
"entry_file": "mjcf/convex_collision_include.xml",
|
| 112 |
+
"license": license_name,
|
| 113 |
+
"origin_url": origin_url,
|
| 114 |
+
"path": derived_dir.relative_to(ROOT).as_posix(),
|
| 115 |
+
"storage_mode": "derived",
|
| 116 |
+
"derived_from": [raw_asset_dir.relative_to(ROOT).as_posix()],
|
| 117 |
+
"derivation_method": "convex_decomposition",
|
| 118 |
+
"decomposition_tool": args.tool,
|
| 119 |
+
"decomposition_version": args.tool_version,
|
| 120 |
+
"decomposition_params_file": "logs/decomposition.json",
|
| 121 |
+
"validation_status": "scaffolded",
|
| 122 |
+
"tags": [source, "convex_decomposition", "collision"],
|
| 123 |
+
}
|
| 124 |
+
if source_commit:
|
| 125 |
+
metadata["source_commit"] = source_commit
|
| 126 |
+
|
| 127 |
+
if "readiness_level" in raw_meta:
|
| 128 |
+
metadata["readiness_level"] = raw_meta["readiness_level"]
|
| 129 |
+
|
| 130 |
+
(derived_dir / "metadata.yaml").write_text("\n".join(dump_yaml(metadata)) + "\n")
|
| 131 |
+
|
| 132 |
+
source_refs = {
|
| 133 |
+
"raw_asset": raw_asset_dir.relative_to(ROOT).as_posix(),
|
| 134 |
+
"raw_entry_file": (raw_asset_dir / raw_meta.get("entry_file", "model.xml")).relative_to(ROOT).as_posix(),
|
| 135 |
+
}
|
| 136 |
+
(derived_dir / "source_refs.yaml").write_text("\n".join(dump_yaml(source_refs)) + "\n")
|
| 137 |
+
|
| 138 |
+
include_xml = f"""<!-- Scaffolded convex collision include for {raw_asset_dir.relative_to(ROOT).as_posix()} -->
|
| 139 |
+
<mujocoinclude>
|
| 140 |
+
<!-- Replace with convex collision geoms generated by {args.tool} -->
|
| 141 |
+
</mujocoinclude>
|
| 142 |
+
"""
|
| 143 |
+
(derived_dir / "mjcf" / "convex_collision_include.xml").write_text(include_xml)
|
| 144 |
+
|
| 145 |
+
logs = {
|
| 146 |
+
"tool": args.tool,
|
| 147 |
+
"tool_version": args.tool_version,
|
| 148 |
+
"created_at": str(date.today()),
|
| 149 |
+
"raw_asset": raw_asset_dir.relative_to(ROOT).as_posix(),
|
| 150 |
+
"derived_asset": derived_dir.relative_to(ROOT).as_posix(),
|
| 151 |
+
"params": {},
|
| 152 |
+
"inputs": [],
|
| 153 |
+
"outputs": [],
|
| 154 |
+
}
|
| 155 |
+
(derived_dir / "logs" / "decomposition.json").write_text(json.dumps(logs, indent=2, ensure_ascii=False) + "\n")
|
| 156 |
+
|
| 157 |
+
readme = f"""# Convex decomposition scaffold
|
| 158 |
+
|
| 159 |
+
Raw asset:
|
| 160 |
+
|
| 161 |
+
```text
|
| 162 |
+
{raw_asset_dir.relative_to(ROOT).as_posix()}
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
Derived asset:
|
| 166 |
+
|
| 167 |
+
```text
|
| 168 |
+
{derived_dir.relative_to(ROOT).as_posix()}
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
This directory is a scaffold only. Populate `meshes/` and replace the include file with real convex collision geometry before registering it in the manifest.
|
| 172 |
+
"""
|
| 173 |
+
(derived_dir / "README.md").write_text(readme)
|
| 174 |
+
|
| 175 |
+
print(derived_dir.relative_to(ROOT).as_posix())
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
if __name__ == "__main__":
|
| 179 |
+
main()
|
scripts/validate_asset.py
CHANGED
|
@@ -54,6 +54,7 @@ def validate_one(asset_dir, manifest):
|
|
| 54 |
errors = []
|
| 55 |
asset_dir = asset_dir.resolve()
|
| 56 |
rel = asset_dir.relative_to(ROOT).as_posix()
|
|
|
|
| 57 |
|
| 58 |
metadata_path = asset_dir / "metadata.yaml"
|
| 59 |
if not metadata_path.exists():
|
|
@@ -67,8 +68,33 @@ def validate_one(asset_dir, manifest):
|
|
| 67 |
if not entry_path.exists():
|
| 68 |
errors.append(f"missing entry_file: {entry_path}")
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
if rel not in manifest:
|
| 71 |
errors.append(f"missing manifest entry for path: {rel}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
if entry_path.exists() and entry_path.is_file() and entry_path.suffix == ".xml":
|
| 74 |
try:
|
|
|
|
| 54 |
errors = []
|
| 55 |
asset_dir = asset_dir.resolve()
|
| 56 |
rel = asset_dir.relative_to(ROOT).as_posix()
|
| 57 |
+
parts = asset_dir.relative_to(ROOT / "assets").parts
|
| 58 |
|
| 59 |
metadata_path = asset_dir / "metadata.yaml"
|
| 60 |
if not metadata_path.exists():
|
|
|
|
| 68 |
if not entry_path.exists():
|
| 69 |
errors.append(f"missing entry_file: {entry_path}")
|
| 70 |
|
| 71 |
+
if len(parts) < 5:
|
| 72 |
+
errors.append(f"asset path must follow assets/<source>/(raw|derived)/<asset_type>/<category>/<asset_id>: {rel}")
|
| 73 |
+
else:
|
| 74 |
+
source, storage_kind, asset_type, category, asset_id = parts[:5]
|
| 75 |
+
if storage_kind not in {"raw", "derived"}:
|
| 76 |
+
errors.append(f"asset storage kind must be raw or derived: {rel}")
|
| 77 |
+
expected_asset_id = f"{source}.{asset_type}.{category}.{asset_id}"
|
| 78 |
+
if metadata.get("source") != source:
|
| 79 |
+
errors.append(f"metadata source mismatch: {rel}: expected {source}, got {metadata.get('source')}")
|
| 80 |
+
if metadata.get("asset_type") != asset_type:
|
| 81 |
+
errors.append(f"metadata asset_type mismatch: {rel}: expected {asset_type}, got {metadata.get('asset_type')}")
|
| 82 |
+
if metadata.get("category") != category:
|
| 83 |
+
errors.append(f"metadata category mismatch: {rel}: expected {category}, got {metadata.get('category')}")
|
| 84 |
+
if metadata.get("asset_id") != expected_asset_id:
|
| 85 |
+
errors.append(f"metadata asset_id mismatch: {rel}: expected {expected_asset_id}, got {metadata.get('asset_id')}")
|
| 86 |
+
if metadata.get("path") != rel:
|
| 87 |
+
errors.append(f"metadata path mismatch: {rel}: got {metadata.get('path')}")
|
| 88 |
+
if storage_kind == "derived" and metadata.get("storage_mode") not in {"derived", "converted", "generated"}:
|
| 89 |
+
errors.append(f"derived asset missing storage_mode metadata: {rel}")
|
| 90 |
+
|
| 91 |
if rel not in manifest:
|
| 92 |
errors.append(f"missing manifest entry for path: {rel}")
|
| 93 |
+
else:
|
| 94 |
+
row = manifest[rel]
|
| 95 |
+
for key in ("asset_id", "source", "asset_type", "category", "entry_file", "path"):
|
| 96 |
+
if metadata.get(key) is not None and row.get(key) != metadata.get(key):
|
| 97 |
+
errors.append(f"manifest {key} mismatch: {rel}: metadata={metadata.get(key)} manifest={row.get(key)}")
|
| 98 |
|
| 99 |
if entry_path.exists() and entry_path.is_file() and entry_path.suffix == ".xml":
|
| 100 |
try:
|