Fix animation import/export

This commit is contained in:
JannisX11 2020-09-27 22:11:37 +02:00
parent 3900ac3f94
commit 12ca538252
6 changed files with 61 additions and 57 deletions

View File

@ -237,7 +237,8 @@ class Animation {
name: (Project.geometry_name||'model')+'.animation',
startpath: this.path,
content: compileJSON(content),
}, () => {
}, (real_path) => {
this.path == real_path;
this.saved = true;
})
}
@ -1309,14 +1310,13 @@ const Animator = {
}
return new_animations
},
buildFile(options) {
if (typeof options !== 'object') {
options = false
}
buildFile(path) {
var animations = {}
Animator.animations.forEach(function(a) {
let ani_tag = a.compileBedrockAnimation();
animations[a.name] = ani_tag;
if (a.path == path) {
let ani_tag = a.compileBedrockAnimation();
animations[a.name] = ani_tag;
}
})
return {
format_version: '1.8.0',
@ -1354,6 +1354,44 @@ const Animator = {
}
})
dialog.show();
},
exportAnimationFile(path) {
let filter_path = path || '';
let content = Animator.buildFile(filter_path, true);
if (isApp && !path) {
path = ModelMeta.export_path
var exp = new RegExp(osfs.replace('\\', '\\\\')+'models'+osfs.replace('\\', '\\\\'))
var m_index = path.search(exp)
if (m_index > 3) {
path = path.substr(0, m_index) + osfs + 'animations' + osfs + pathToName(ModelMeta.export_path, true)
}
path = path.replace(/(\.geo)?\.json$/, '.animation.json')
}
if (isApp && path && fs.existsSync(path)) {
Animator.animations.forEach(function(a) {
if (a.path == filter_path) {
a.save();
}
})
} else {
Blockbench.export({
resource_id: 'animation',
type: 'JSON Animation',
extensions: ['json'],
name: (Project.geometry_name||'model')+'.animation',
startpath: path,
content: autoStringify(content),
}, new_path => {
Animator.animations.forEach(function(a) {
if (a.path == filter_path) {
a.path = new_path;
a.saved = true;
}
})
})
}
}
}
@ -1453,37 +1491,17 @@ BARS.defineActions(function() {
})
}
})
new Action('export_animation_file', {
new Action('save_all_animations', {
icon: 'save',
category: 'animation',
click: function () {
var content = autoStringify(Animator.buildFile())
var path = ModelMeta.animation_path
if (isApp && !path) {
path = ModelMeta.export_path
var exp = new RegExp(osfs.replace('\\', '\\\\')+'models'+osfs.replace('\\', '\\\\'))
var m_index = path.search(exp)
if (m_index > 3) {
path = path.substr(0, m_index) + osfs + 'animations' + osfs + pathToName(ModelMeta.export_path, true)
}
if (path.match(/\.geo\.json$/)) {
path = path.replace(/\.geo\.json$/, '.animation.json')
} else {
path = path.replace(/\.json$/, '.animation.json')
}
}
Blockbench.export({
resource_id: 'animation',
type: 'JSON Animation',
extensions: ['json'],
name: (Project.geometry_name||'model')+'.animation',
startpath: path,
content: content,
}, (real_path) => {
ModelMeta.animation_path = real_path;
let paths = [];
Animation.all.forEach(animation => {
paths.safePush(animation.path);
})
paths.forEach(path => {
Animator.exportAnimationFile(path);
})
}
})
@ -1543,14 +1561,8 @@ Interface.definePanels(function() {
this.files_folded[key] = !this.files_folded[key];
this.$forceUpdate();
},
saveFile(key, file) {
if (key && isApp) {
file.animations.forEach(animation => {
animation.save();
})
} else {
}
saveFile(path) {
Animator.exportAnimationFile(path)
},
addAnimation(path) {
let other_animation = Animation.all.find(a => a.path == path)

View File

@ -569,7 +569,7 @@ class NumSlider extends Widget {
if (typeof this.onBefore === 'function') {
this.onBefore()
}
text = text.replace(/(?<=\d),(?=\d)/, '.');
if (text.match(/^-?\d*(\.\d+)?$/gm)) {
var number = parseFloat(text);
if (isNaN(number)) {

View File

@ -618,7 +618,7 @@ const MenuBar = {
'select_effect_animator',
'_',
'load_animation_file',
'export_animation_file',
'save_all_animations',
], () => Animator.open)

View File

@ -103,7 +103,6 @@ class ModelFormat {
Undo.history.length = 0;
Undo.index = 0;
ModelMeta.export_path = '';
ModelMeta.animation_path = '';
var old_format = Format
this.select(true)
@ -192,7 +191,6 @@ class ModelFormat {
//Animation Mode
if (!Format.animation_mode && old_format.animation_mode) {
Animator.animations.length = 0;
ModelMeta.animation_path = '';
}
Canvas.updateAllPositions()
Canvas.updateAllBones()
@ -886,14 +884,8 @@ BARS.defineActions(function() {
Format.codec.export()
}
}
if (Format.animation_mode) {
if (ModelMeta.animation_path) {
Blockbench.writeFile(ModelMeta.animation_path, {
content: autoStringify(Animator.buildFile())
})
} else if (Animator.animations.length) {
BarItems.export_animation_file.trigger()
}
if (Format.animation_mode && Animation.all.length) {
BarItems.save_all_animations.trigger();
}
} else {
saveTextures()

View File

@ -63,7 +63,7 @@ class ModelProject {
this.overrides = null;
Blockbench.display_settings = display = {};
ModelMeta.save_path = ModelMeta.export_path = ModelMeta.animation_path = ModelMeta.name = '';
ModelMeta.save_path = ModelMeta.export_path = ModelMeta.name = '';
ModelMeta.saved = true;
Prop.project_saved = true;
Prop.added_models = 0;

View File

@ -952,8 +952,8 @@
"action.load_animation_file.desc": "Import an animation file",
"action.play_animation": "Play Animation",
"action.play_animation.desc": "Preview the selected animation",
"action.export_animation_file": "Export Animations",
"action.export_animation_file.desc": "Export a json file with the current animations",
"action.save_all_animations": "Save All Animations",
"action.save_all_animations.desc": "Save all currently loaded animations",
"action.slider_animation_length": "Animation Length",
"action.slider_animation_length.desc": "Change the length of the selected animation",
"action.slider_keyframe_time": "Timecode",