Datasets:
Download tool/query.py from codepawl/asset-index: direct link, hf CLI and curl.
- Browser
- Download file 6.4 kB
-
https://huggingface.co/datasets/codepawl/asset-index/resolve/main/tool/query.py
- Command line
-
hf download hf://datasets/codepawl/asset-index/tool/query.py
-
curl -L -o query.py https://huggingface.co/datasets/codepawl/asset-index/resolve/main/tool/query.py
6.4 kB
| """Find assets in the CodePawl asset index from a plain request. | |
| Usage: query.py <metadata.jsonl or .parquet> "wooden crate, 0.5 m, game tier" [--top 10] [--source kenney] | |
| [--free] [--trainable] [--rigged] [--json] | |
| A size like "0.5 m" or "30 cm" keeps assets whose scale is within half to double that size. "game", "web", "mobile" | |
| or "film" (with or without "tier") picks the use whose recommended tier is shown. "material", "surface", "model", | |
| "decal", "hdri" or "character" narrows the kind; "tileable" or "seamless" keeps repeating surfaces. "lowpoly", "stylized" or "photoreal" narrows the style. Every other | |
| word is matched against search_text; rows rank by how many words match, then by real size known, then by texel density. | |
| --free keeps assets whose files may be shipped (CC0), --trainable drops rows Fab flags NoAI, --rigged keeps rigged ones. | |
| Each result says how to get it: a download URL for CC0 assets, or the Fab page plus the optimizer command for Megascans. | |
| """ | |
| import json | |
| import re | |
| import sys | |
| from pathlib import Path | |
| USES = {"game", "web", "mobile", "film"} | |
| KIND_WORDS = {"material": "material", "surface": "material", "texture": "material", "model": "model", "prop": "model", | |
| "decal": "decal", "hdri": "hdri", "skybox": "hdri", "brush": "brush", "atlas": "atlas"} | |
| STYLE_WORDS = {"lowpoly": "lowpoly", "stylized": "stylized", "stylised": "stylized", "photoreal": "photoreal", | |
| "realistic": "photoreal", "scanned": "photoreal"} | |
| STOP_WORDS = {"a", "an", "the", "tier", "for", "of", "with", "and", "in", "size", "about", "need", "want", "i"} | |
| JSON_COLUMNS = ("recommended_tier", "files", "get") | |
| def load_rows(path): | |
| path = Path(path) | |
| if path.suffix == ".parquet": | |
| import pyarrow.parquet as parquet | |
| rows = parquet.read_table(path).to_pylist() | |
| for row in rows: | |
| for column in JSON_COLUMNS: | |
| row[column] = json.loads(row[column]) if row.get(column) else None | |
| return rows | |
| return [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line.strip()] | |
| def parse_request(request): | |
| text = request.lower() | |
| wanted = {"size_m": None, "use": None, "kind": None, "style": None, "character": False, "tileable": False, "words": []} | |
| size = re.search(r"(\d+(?:\.\d+)?)\s*(cm|m)\b", text) | |
| if size: | |
| wanted["size_m"] = float(size.group(1)) / (100 if size.group(2) == "cm" else 1) | |
| text = text.replace(size.group(0), " ") | |
| for word in re.findall(r"[a-z]+", text.replace("low poly", "lowpoly")): | |
| if word in USES: | |
| wanted["use"] = word | |
| elif word in KIND_WORDS: | |
| wanted["kind"] = KIND_WORDS[word] | |
| elif word in STYLE_WORDS: | |
| wanted["style"] = STYLE_WORDS[word] | |
| elif word in ("character", "characters", "rigged"): | |
| wanted["character"] = True | |
| elif word in ("tileable", "seamless", "tiling"): | |
| wanted["tileable"] = True | |
| elif word not in STOP_WORDS: | |
| wanted["words"].append(word.rstrip("s") if len(word) > 3 else word) | |
| return wanted | |
| def score(row, wanted): | |
| if wanted["kind"] and row["kind"] != wanted["kind"]: | |
| return None | |
| if wanted["style"] and row["style"] != wanted["style"]: | |
| return None | |
| if wanted["character"] and not row.get("rigged"): | |
| return None | |
| if wanted["tileable"] and not row.get("tileable"): | |
| return None | |
| if wanted["size_m"] is not None: | |
| scale = row.get("scale_m") | |
| if scale is None or not (wanted["size_m"] / 2 <= scale <= wanted["size_m"] * 2): | |
| return None | |
| text = row["search_text"] | |
| matched = sum(1 for word in wanted["words"] if re.search(rf"\b{re.escape(word)}", text)) | |
| if wanted["words"] and matched == 0: | |
| return None | |
| title = (row.get("title") or "").lower() | |
| in_title = sum(1 for word in wanted["words"] if re.search(rf"\b{re.escape(word)}", title)) | |
| return (matched, in_title, row.get("scale_m") is not None, row.get("texel_density_px_m") or 0) | |
| def how_to_get(row, use): | |
| get = row.get("get") or {} | |
| files = row.get("files") or {} | |
| tier = (row.get("recommended_tier") or {}).get(use) | |
| if files: | |
| tier_key = (tier or "web").split(" ")[0] | |
| return files.get(tier_key) or files.get("web") | |
| if get.get("how") == "claim on Fab": | |
| return f"claim {get['url']} then: {get['then']}" | |
| if get.get("file_in_kit"): | |
| return f"{get['url']} (kit zip), file {get['file_in_kit']}" | |
| return get.get("url") | |
| def main(): | |
| sys.stdout.reconfigure(encoding="utf-8", errors="replace") | |
| arguments = sys.argv[1:] | |
| top = int(arguments[arguments.index("--top") + 1]) if "--top" in arguments else 10 | |
| rows = load_rows(arguments[0]) | |
| wanted = parse_request(arguments[1]) | |
| if "--source" in arguments: | |
| source = arguments[arguments.index("--source") + 1] | |
| rows = [row for row in rows if row["source"] == source] | |
| if "--free" in arguments: | |
| rows = [row for row in rows if row["redistributable"]] | |
| if "--trainable" in arguments: | |
| rows = [row for row in rows if row["redistributable"] or row.get("no_ai") is False] | |
| if "--rigged" in arguments: | |
| wanted["character"] = True | |
| ranked = [(row_score, row) for row in rows if (row_score := score(row, wanted)) is not None] | |
| ranked.sort(key=lambda pair: pair[0], reverse=True) | |
| use = wanted["use"] or "game" | |
| results = [{ | |
| "title": row["title"], "source": row["source"], "kind": row["kind"], "style": row["style"], | |
| "scale_m": row["scale_m"], "licence": row["licence"], "free_to_ship": row["redistributable"], | |
| "use": use, "tier": (row.get("recommended_tier") or {}).get(use), "get": how_to_get(row, use), | |
| "credit": row.get("credit"), "no_ai": row.get("no_ai"), "page": row["source_url"], | |
| } for _, row in ranked[:top]] | |
| if "--json" in arguments: | |
| print(json.dumps({"request": wanted, "matches": len(ranked), "results": results}, ensure_ascii=False, indent=1)) | |
| return | |
| print(f"{len(ranked)} matches for {arguments[1]!r}") | |
| for result in results: | |
| size = f"{result['scale_m']} m" if result["scale_m"] else "size ?" | |
| print(f"- {result['title']} ({result['source']}, {result['kind']}, {size}, {result['use']}: {result['tier']})\n get: {result['get']}") | |
| if __name__ == "__main__": | |
| main() | |