Worm bin model

This commit is contained in:
2026-10-12 18:58:38 -03:00
parent 99bdcaeffc
commit 038b6d2caa
16 changed files with 447 additions and 39 deletions
+2 -1
View File
@@ -30,7 +30,8 @@ kinds:
options:
--output PATH the file (for one input file) or the folder to write. one model without it is printed
--namespace NS the namespace, when the input isn't inside assets/<namespace>/, or the only one to take from a
folder with several
folder with several. a file outside assets/ is otherwise the mod's that --output is in, and its
bare names (a texture "gear") are that mod's too, the way blockbench exports them
--parents FOLDER another folder to follow parent models through, like another mod's resources. can be repeated
--remap FILE json that renames locations before they become ids, longest match first, and leaves out
locations that match a "skip" pattern:
+1 -1
View File
@@ -76,7 +76,7 @@ const CUBES = [
export function import_blocks(input: string, options: ImportOptions = {}): ImportReport {
if (options.output === undefined) throw new Error("--output is needed for blocks");
const remaps = options.remaps ?? DEFAULT_REMAPS;
const files = input_files(input, "blockstates", ".json", options.namespace);
const files = input_files(input, "blockstates", ".json", options.namespace, options.output);
// models say how a block is drawn (a full cube or not, cutout or translucent) and whether it has an item
const read = model_source(models_near(input, files.map(({ root }) => root), options));
const report = empty_report();
+1 -1
View File
@@ -31,7 +31,7 @@ export function import_items(input: string, options: ImportOptions = {}): Import
if (options.output === undefined) throw new Error("--output is needed for items");
const remaps = options.remaps ?? DEFAULT_REMAPS;
// an item is a model directly in models/item, the folders under it are parts of models
const files = input_files(input, "models", ".json", options.namespace)
const files = input_files(input, "models", ".json", options.namespace, options.output)
.filter(({ root, file }) => /^item\/[^/]+$/.test(location_of(root, file, ".json").path));
const roots = [...new Set(files.map(({ root }) => root))];
const read = model_source(models_near(input, roots, options));
+25 -12
View File
@@ -44,7 +44,6 @@ const FACE_NAMES: Record<string, ModelFace> = {
west: "west",
east: "east",
};
const ANGLES = [-45, -22.5, 0, 22.5, 45];
// item models that are a flat sprite or drawn by code, not boxes, and so is anything that has one as a parent
const NOT_BOXES: Record<string, string> = {
@@ -74,7 +73,7 @@ export function import_models(input: string, options: ImportOptions = {}): Impor
const remaps = options.remaps ?? DEFAULT_REMAPS;
const single = is_file(input);
if (!single && options.output === undefined) throw new Error("--output is needed to convert a folder");
const files = input_files(input, "models", ".json", options.namespace);
const files = input_files(input, "models", ".json", options.namespace, options.output);
const read = model_source([
...new Set(files.map(({ root }) => root)),
...(options.parents ?? []).flatMap((dir) => find_roots(dir, "models")),
@@ -87,7 +86,8 @@ export function import_models(input: string, options: ImportOptions = {}): Impor
const where = location_of(root, file, ".json");
const location = location_string(where);
if (skips(location, remaps, report)) continue;
const result = convert_model(location, read, remaps);
// a loose file's bare texture names are its own, like blockbench writes them
const result = convert_model(location, read, remaps, root.loose ? root.namespace : "minecraft");
if (result.kind === "skipped") report.skipped.push({ location, reason: result.reason });
else if (result.kind === "error") report.errors.push({ location, problems: result.problems });
else if (claim(result.id, location)) {
@@ -124,7 +124,8 @@ export function model_source(roots: ResourceRoot[]): ModelSource {
};
}
export function convert_model(location: string, read: ModelSource, remaps: Remaps): ModelResult {
// bare is the namespace of textures named without one
export function convert_model(location: string, read: ModelSource, remaps: Remaps, bare = "minecraft"): ModelResult {
const json = read(location_string(parse_location(location)));
if (!json) return { kind: "error", problems: [`${location} wasn't found`] };
const made = made_by(location, read);
@@ -137,7 +138,7 @@ export function convert_model(location: string, read: ModelSource, remaps: Remap
}
const texture = (value: string, where: string) => {
if (value.startsWith("#")) return value;
const id = to_id(value, remaps.textures);
const id = to_id(value, remaps.textures, bare);
if (!is_valid_id(id)) problems.push(`${where}: "${value}" doesn't make a valid texture id (${id})`);
return id;
};
@@ -208,16 +209,28 @@ function convert_element(
};
const out = { from: point("from"), to: point("to") } as ModelElementJson;
// { axis, angle }, or blockbench's newer { x, y, z } with a turn around each
const rotation = element.rotation as Record<string, unknown> | undefined;
if (rotation && rotation.angle !== 0) {
if (!ANGLES.includes(rotation.angle as number)) {
problems.push(`${where}.rotation: an angle of ${rotation.angle} isn't one of ${ANGLES.join(", ")}`);
const turns = rotation === undefined
? []
: typeof rotation.axis === "string"
? [[rotation.axis, rotation.angle]]
: ["x", "y", "z"].map((axis) => [axis, rotation[axis] ?? 0]);
const turned = turns.filter(([, angle]) => angle !== 0);
if (turned.length > 1) {
problems.push(
`${where}.rotation turns around ${turned.map(([axis]) => axis).join(" and ")} at once, bworld only one`,
);
} else if (turned.length === 1) {
const [[axis, angle]] = turned;
if (typeof angle !== "number" || Math.abs(angle) > 180) {
problems.push(`${where}.rotation: ${angle} isn't an angle from -180 to 180`);
}
out.rotation = {
origin: rotation.origin as [number, number, number],
axis: rotation.axis as "x" | "y" | "z",
angle: rotation.angle as number,
...(rotation.rescale === true ? { rescale: true } : {}),
origin: rotation!.origin as [number, number, number],
axis: axis as "x" | "y" | "z",
angle: angle as number,
...(rotation!.rescale === true ? { rescale: true } : {}),
};
}
if (element.shade === false) out.shade = false;
+35 -12
View File
@@ -12,10 +12,11 @@ export interface Location {
path: string;
}
export function parse_location(value: string): Location {
// bare is the namespace of a location written without one: minecraft's, in a mod's resources
export function parse_location(value: string, bare = "minecraft"): Location {
const colon = value.indexOf(":");
return colon < 0
? { namespace: "minecraft", path: value }
? { namespace: bare, path: value }
: { namespace: value.slice(0, colon), path: value.slice(colon + 1) };
}
@@ -53,8 +54,8 @@ function template_remaps(): Remap {
);
}
export function remap_location(location: string, remap: Remap = {}): Location {
const full = location_string(parse_location(location));
export function remap_location(location: string, remap: Remap = {}, bare = "minecraft"): Location {
const full = location_string(parse_location(location, bare));
for (const from of Object.keys(remap).sort((a, b) => b.length - a.length)) {
const prefix = from.endsWith("/") || from.endsWith(":");
if (prefix ? full.startsWith(from) : full === from) {
@@ -64,8 +65,8 @@ export function remap_location(location: string, remap: Remap = {}): Location {
return parse_location(full);
}
export function to_id(location: string, remap: Remap = {}): string {
const { namespace, path } = remap_location(location, remap);
export function to_id(location: string, remap: Remap = {}, bare = "minecraft"): string {
const { namespace, path } = remap_location(location, remap, bare);
return `${namespace}:${path.replaceAll("/", "_")}`;
}
@@ -77,6 +78,9 @@ export function is_valid_id(id: string): boolean {
export interface ResourceRoot {
namespace: string;
dir: string;
// not in a mod's assets/ but a loose folder, like blockbench's exports. what it names without a namespace is its
// own, not minecraft's: a texture "gear" next to a model is that model's texture
loose?: boolean;
}
// the roots of a kind inside a folder: every assets/<namespace>/<kind> under it (only namespace's, when it's given), or
@@ -93,7 +97,7 @@ export function find_roots(input: string, kind: string, namespace?: string): Res
}
}
}
if (roots.length === 0) return namespace ? [{ namespace, dir }] : [];
if (roots.length === 0) return namespace ? [{ namespace, dir, loose: true }] : [];
return namespace ? roots.filter((root) => root.namespace === namespace) : roots;
}
@@ -103,12 +107,21 @@ export interface InputFile {
file: string;
}
// what to convert: one file, or every file with the extension in a folder's roots of a kind
export function input_files(input: string, kind: string, extension: string, namespace?: string): InputFile[] {
// what to convert: one file, or every file with the extension in a folder's roots of a kind. a file outside any
// assets/ folder is the namespace's, or the mod's that output is in
export function input_files(
input: string,
kind: string,
extension: string,
namespace?: string,
output?: string,
): InputFile[] {
if (is_file(input)) {
const root = enclosing_root(input, kind) ?? (namespace && { namespace, dir: dirname(resolve(input)) });
if (!root) throw new Error(`can't tell ${input}'s namespace: pass --namespace`);
return [{ root, file: resolve(input) }];
const enclosing = enclosing_root(input, kind);
if (enclosing) return [{ root: enclosing, file: resolve(input) }];
const loose = namespace ?? (output && mod_namespace(output));
if (!loose) throw new Error(`can't tell ${input}'s namespace: pass --namespace, or an --output in a mod`);
return [{ root: { namespace: loose, dir: dirname(resolve(input)), loose: true }, file: resolve(input) }];
}
const roots = find_roots(input, kind, namespace);
if (roots.length === 0) {
@@ -169,6 +182,16 @@ function read_dir(dir: string): Deno.DirEntry[] {
}
}
// the id of the mod a path is in, from the manifest.json of the nearest folder up that has one
export function mod_namespace(path: string): string | undefined {
for (let dir = dirname(resolve(path)); dir !== dirname(dir); dir = dirname(dir)) {
if (!is_file(`${dir}/manifest.json`)) continue;
const id = JSON.parse(Deno.readTextFileSync(`${dir}/manifest.json`)).id;
return typeof id === "string" ? id : undefined;
}
return undefined;
}
export function is_file(path: string): boolean {
try {
return Deno.statSync(path).isFile;
+1 -1
View File
@@ -60,7 +60,7 @@ export function import_textures(input: string, options: ImportOptions = {}): Imp
const single = is_file(input);
const report = empty_report();
const claim = id_claims(report);
const files = input_files(input, "textures", ".png", options.namespace);
const files = input_files(input, "textures", ".png", options.namespace, options.output);
// a texture gets its namespace from the mod it's in, so two namespaces' textures can't share one folder
const namespaces = [...new Set(files.map(({ root }) => root.namespace))].filter((ns) => ns !== "minecraft");
if (namespaces.length > 1) {