forked from JannisX11/blockbench-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation_to_java.js
More file actions
249 lines (233 loc) · 9.48 KB
/
Copy pathanimation_to_java.js
File metadata and controls
249 lines (233 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
(function () {
let menuButton;
Plugin.register("animation_to_java", {
title: "Animation to Java Converter",
author: "MG, Vincent_Huto(PR), DailyCraft(PR), MarcusSlover(PR)",
description:
"Converts Blockbench animations to Java code for the new 1.19+ keyframe system.",
icon: "fa-cube",
version: "1.2.0",
max_version: "4.12.9",
variant: "both",
about:
"This plugin exports your Blockbench animations as Java code to be used for the new 1.19+ keyframe system. Please note that this system does not support Molang or step interpolation.",
tags: ["Deprecated", "Minecraft: Java Edition"],
deprecation_note: 'This feature is now built into Blockbench, the plugin is no longer required.',
onload() {
Formats.modded_entity.animation_mode = true
menuButton = new Action("export_animation_to_java", {
name: "Export Animations to Java",
description: "Exports animations to Java code",
icon: "fa-file-export",
condition: () => Format.animation_mode,
click() {
mapToExport();
},
});
MenuBar.addAction(menuButton, "file.export");
},
onunload() {
Formats.modded_entity.animation_mode = false
menuButton.delete();
},
});
/**
* The mappings for the mojang mappings.
*/
const animation_mojang_mappings = {
definition: {
name: "AnimationDefinition",
builder: "Builder",
},
add: "addAnimation",
loop: "looping",
length: "withLength",
channel: {
name: "AnimationChannel",
targets: {
position: {
name: "Targets.POSITION",
vector: "KeyframeAnimations.posVec"
},
rotation: {
name: "Targets.ROTATION",
vector: "KeyframeAnimations.degreeVec"
},
scale: {
name: "Targets.SCALE",
vector: "KeyframeAnimations.scaleVec"
}
},
interpolations: {
linear: "Interpolations.LINEAR",
cubic: "Interpolations.CATMULLROM"
}
}
}
/**
* The mappings for the yarn mappings.
*/
const animation_yarn_mappings = {
definition: {
name: "Animation",
builder: "Builder",
},
add: "addBoneAnimation",
loop: "looping",
length: "create",
channel: {
name: "Transformation",
targets: {
position: {
name: "Targets.TRANSLATE",
vector: "AnimationHelper.createTranslationalVector"
},
rotation: {
name: "Targets.ROTATE",
vector: "AnimationHelper.createRotationalVector"
},
scale: {
name: "Targets.SCALE",
vector: "AnimationHelper.createScalingVector"
}
},
interpolations: {
linear: "Interpolations.LINEAR",
cubic: "Interpolations.CUBIC"
}
}
}
/**
* Asks the user for the mappings to use and then generates the file.
*/
function mapToExport() {
new Dialog("select_mappings", {
id: "select_mappings",
title: "Select Mappings",
form: {
mappings: {
label: "Mappings",
type: "select",
options: {
mojang: "Mojang",
yarn: "Yarn",
},
default: "mojang",
},
},
onConfirm(form) {
let data;
if (form.mappings === "mojang") {
data = generateFile(animation_mojang_mappings);
} else {
data = generateFile(animation_yarn_mappings);
}
Blockbench.export({
type: "Text File",
extensions: ["txt"],
savetype: "text",
content: data,
});
this.hide();
},
}).show();
}
/**
* Transforms the interpolation to the correct format.
* @param interpolation The interpolation to transform.
* @param mappings The mappings to use.
* @returns {string|*} The transformed interpolation.
*/
function transformInterpolation(interpolation, mappings) {
if (interpolation === "LINEAR") {
return mappings.channel.interpolations.linear;
} else if (interpolation === "CUBIC" || interpolation === "CATMULLROM") {
return mappings.channel.interpolations.cubic;
}
}
/**
* Generates the file from the given mappings.
* @param mapping The mappings to use.
* @returns {string} The generated file.
*/
function generateFile(mapping) {
let outfileText = "";
const definition = mapping.definition.name;
const builder = mapping.definition.builder;
const length = mapping.length;
const loop = mapping.loop;
const add = mapping.add;
const channel = mapping.channel.name;
const targets = mapping.channel.targets;
for (const animation of Animation.all) {
outfileText += `\npublic static final ${definition} ${animation.name
.replaceAll(".", "_")
.replace("animation_", "")
.toUpperCase()} = ${definition}.${builder}.${length}(${animation.length
}f)`;
if (animation.loop === "loop") {
outfileText += `.${loop}()`;
}
for (const id in animation.animators) {
const boneAnimator = animation.animators[id];
if (!(boneAnimator instanceof BoneAnimator)) continue;
let posKeyArray = [];
let rotKeyArray = [];
let scaleKeyArray = [];
// Position
if (boneAnimator.position.length) {
outfileText += `\n.${add}("${boneAnimator.name}",\n\tnew ${channel}(${channel}.${targets.position.name}`;
//Sorts by time to ensure ordering
for (const keyFrame of boneAnimator.position) {
posKeyArray.push(keyFrame);
}
posKeyArray.sort((a, b) => a.time - b.time)
for (const keyFrame of posKeyArray) {
const {x = 0.0, y = 0.0, z = 0.0} = keyFrame.data_points[0];
const interpolation = transformInterpolation(keyFrame.interpolation.toUpperCase(), mapping);
outfileText += `, \n\t\tnew Keyframe(${keyFrame.time
}f, ${targets.position.vector}(${round2(x)}f, ${round2(y)}f, ${round2(z)}f),\n\t\t\t${channel}.${interpolation})`;
}
outfileText += "))";
}
// Rotation
if (boneAnimator.rotation.length) {
outfileText += `\n.${add}("${boneAnimator.name}",\n\tnew ${channel}(${channel}.${targets.rotation.name}`;
//Sorts by time to ensure ordering
for (const keyFrame of boneAnimator.rotation) {
rotKeyArray.push(keyFrame);
}
rotKeyArray.sort((a, b) => a.time - b.time)
for (const keyFrame of rotKeyArray) {
const {x = 0.0, y = 0.0, z = 0.0} = keyFrame.data_points[0];
const interpolation = transformInterpolation(keyFrame.interpolation.toUpperCase(), mapping);
outfileText += `,\n\t\tnew Keyframe(${keyFrame.time
}f, ${targets.rotation.vector}(${round2(x)}f, ${round2(y)}f, ${round2(z)}f),\n\t\t\t${channel}.${interpolation})`;
}
outfileText += "))";
}
// Scale
if (boneAnimator.scale.length) {
outfileText += `\n.${add}("${boneAnimator.name}",\n\tnew ${channel}(${channel}.${targets.scale.name}`;
//Sorts by time to ensure ordering
for (const keyFrame of boneAnimator.scale) {
scaleKeyArray.push(keyFrame);
}
scaleKeyArray.sort((a, b) => a.time - b.time)
for (const keyFrame of scaleKeyArray) {
const {x = 0.0, y = 0.0, z = 0.0} = keyFrame.data_points[0];
const interpolation = transformInterpolation(keyFrame.interpolation.toUpperCase(), mapping);
outfileText += `,\n\t\tnew Keyframe(${keyFrame.time
}f, ${targets.scale.vector}(${round2(x)}f, ${round2(y)}f, ${round2(z)}f),\n\t\t\t${channel}.${interpolation})`;
}
outfileText += "))";
}
}
outfileText += ".build();"
}
return outfileText.replaceAll("66666666666", "67").replaceAll("33333333333", "34");
}
function round2(n) {
return Math.round((Number(n) + Number.EPSILON) * 100) / 100;
}
})();