Last active
November 1, 2022 14:01
-
-
Save ljfa-ag/db7d682785f977081c99e67bcfd8fbad to your computer and use it in GitHub Desktop.
Script for converting ModelBase Java code to the newer LayerDefinition format in Minecraft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Released into Public Domain | |
# A quick and dirty script to convert ModelBase Java code (for example, | |
# generated by Techne) into the LayerDefinition format used by newer Minecraft | |
# versions (1.17+). This might be useful for adapting old Entity or BlockEntity | |
# models. | |
# This script outputs Java code for a static method that returns a | |
# LayerDefinition for the model, as typical for Model subclasses in Minecraft. | |
# Usage: | |
# modelbase_to_obj input.java > output.java | |
use strict; | |
use warnings; | |
my %parts; | |
my @tex_size; | |
while(<>) { | |
if(/textureWidth = (\d+);/) { | |
if(scalar @tex_size == 0) { | |
@tex_size = ($1, 0); | |
} | |
else { | |
$tex_size[0] = $1; | |
} | |
} | |
elsif(/textureHeight = (\d+);/) { | |
if(scalar @tex_size == 0) { | |
@tex_size = (0, $1); | |
} | |
else { | |
$tex_size[1] = $1; | |
} | |
} | |
elsif(/(\w+) = new ModelRenderer\(this, (\d+), (\d+)\);/) { | |
$parts{$1}{base_uv} = [$2, $3]; | |
} | |
elsif(/(\w+)\.addBox\((-?\d*\.?\d*)[Ff]?, (-?\d*\.?\d*)[Ff]?, (-?\d*\.?\d*)[Ff]?, (\d+), (\d+), (\d+)(,.*)?\);/) { | |
$parts{$1}{offset} = [$2, $3, $4]; | |
$parts{$1}{size} = [$5, $6, $7]; | |
$parts{$1}{rotation} = [0, 0, 0]; | |
} | |
elsif(/(\w+)\.setRotationPoint\((-?\d*\.?\d*)[Ff]?, (-?\d*\.?\d*)[Ff]?, (-?\d*\.?\d*)[Ff]?\);/) { | |
$parts{$1}{rot_point} = [$2, $3, $4]; | |
} | |
elsif(/(\w+)\.setTextureSize\((\d+), (\d+)\);/) { | |
if(scalar @tex_size == 0) { | |
@tex_size = ($2, $3); | |
} | |
elsif($2 != $tex_size[0] || $3 != $tex_size[1]) { | |
print STDERR "Warning: Line $.: The texture size has to be the same for all parts\n"; | |
} | |
} | |
elsif(/(\w+)\.mirror = (true|false);/) { | |
$parts{$1}{mirror} = ($2 eq "true"); | |
} | |
elsif(/setRotation\((\w+), (-?\d*\.?\d*)[Ff]?, (-?\d*\.?\d*)[Ff]?, (-?\d*\.?\d*)[Ff]?\)/) { | |
$parts{$1}{rotation} = [$2, $3, $4]; | |
} | |
else { | |
#print STDERR "Ignoring line $.\n"; | |
} | |
} | |
print <<"EOF"; | |
public static LayerDefinition createLayer() { | |
var mesh = new MeshDefinition(); | |
var root = mesh.getRoot(); | |
EOF | |
foreach my $part (sort keys %parts) { | |
my $offset = $parts{$part}{offset}; | |
my $size = $parts{$part}{size}; | |
my $rot_point = $parts{$part}{rot_point}; | |
my $base_uv = $parts{$part}{base_uv}; | |
my $mirror = $parts{$part}{mirror} ? "true" : "false"; | |
my $rotation = $parts{$part}{rotation}; | |
print <<"EOF"; | |
root.addOrReplaceChild("$part", CubeListBuilder.create() | |
.texOffs($base_uv->[0], $base_uv->[1]) | |
.addBox($offset->[0]F, $offset->[1]F, $offset->[2]F, $size->[0]F, $size->[1]F, $size->[2]F, $mirror), | |
EOF | |
if($rotation->[0] == 0 && $rotation->[1] == 0 && $rotation->[2] == 0) { | |
if($rot_point->[0] == 0 && $rot_point->[1] == 0 && $rot_point->[2] == 0) { | |
print <<"EOF"; | |
PartPose.ZERO); | |
EOF | |
} | |
else { | |
print <<"EOF"; | |
PartPose.offset($rot_point->[0]F, $rot_point->[1]F, $rot_point->[2]F)); | |
EOF | |
} | |
} | |
else { | |
print <<"EOF"; | |
PartPose.offsetAndRotation($rot_point->[0]F, $rot_point->[1]F, $rot_point->[2]F, $rotation->[0]F, $rotation->[1]F, $rotation->[2]F)); | |
EOF | |
} | |
} | |
print <<"EOF"; | |
return LayerDefinition.create(mesh, $tex_size[0], $tex_size[1]); | |
} | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment