Export model selections as project

Closes #2235
This commit is contained in:
JannisX11 2024-12-17 19:51:24 +01:00
parent 2a22e62df5
commit df004986c3
4 changed files with 87 additions and 8 deletions

View File

@ -16,6 +16,7 @@ class Codec extends EventSystem {
Merge.function(this, data, 'overwrite');
Merge.function(this, data, 'export');
Merge.function(this, data, 'exportCollection');
Merge.function(this, data, 'writeCollection');
Merge.function(this, data, 'fileName');
Merge.function(this, data, 'afterSave');
Merge.function(this, data, 'afterDownload');
@ -122,7 +123,8 @@ class Codec extends EventSystem {
custom_writer: isApp ? (a, b) => this.write(a, b) : null,
}, path => this.afterDownload(path))
}
async exportCollection(collection) {
async patchCollectionExport(collection, callback) {
this.context = collection;
let element_export_values = {};
let all = Outliner.elements.concat(Group.all);
for (let node of all) {
@ -133,9 +135,8 @@ class Codec extends EventSystem {
for (let node of collection.getAllChildren()) {
if (node.export == false) node.export = true;
}
this.context = collection;
try {
await this.export();
await callback();
} catch (error) {
throw error;
} finally {
@ -146,6 +147,16 @@ class Codec extends EventSystem {
}
}
}
async exportCollection(collection) {
this.patchCollectionExport(collection, async () => {
await this.export();
})
}
async writeCollection(collection) {
this.patchCollectionExport(collection, async () => {
await this.export();
})
}
fileName() {
if (this.context instanceof Collection) {
return this.context.name;

View File

@ -71,6 +71,7 @@ var codec = new Codec('project', {
name: 'Blockbench Project',
extension: 'bbmodel',
remember: true,
support_partial_export: true,
load_filter: {
type: 'json',
extensions: ['bbmodel']
@ -117,6 +118,38 @@ var codec = new Codec('project', {
} : null,
}, path => this.afterDownload(path))
},
async exportCollection(collection) {
this.context = collection;
Blockbench.export({
resource_id: 'model',
type: this.name,
extensions: [this.extension],
name: this.fileName(),
startpath: this.startPath(),
content: isApp ? null : this.compile({collection_only: collection}),
custom_writer: isApp ? (content, path) => {
// Path needs to be changed before compiling for relative resource paths
let old_save_path = Project.save_path;
Project.save_path = path;
content = this.compile({collection_only: collection});
this.write(content, path);
this.context = null;
Project.save_path = old_save_path;
} : null,
}, path => this.afterDownload(path));
},
async writeCollection(collection) {
if (!collection.export_path) {
console.warn('No path specified');
return;
}
this.context = collection;
let old_save_path = Project.save_path;
let content = this.compile({collection_only: collection});
this.write(content, collection.export_path);
this.context = null;
Project.save_path = old_save_path;
},
compile(options) {
if (!options) options = 0;
var model = {
@ -167,12 +200,37 @@ var codec = new Codec('project', {
}
if (!(Format.id == 'skin' && model.skin_model)) {
model.elements = []
if (options.collection_only) {
var all_collection_children = options.collection_only.getAllChildren();
}
model.elements = [];
elements.forEach(el => {
var obj = el.getSaveCopy(model.meta)
model.elements.push(obj)
if (options.collection_only && !all_collection_children.includes(el)) return;
let copy = el.getSaveCopy(model.meta);
model.elements.push(copy);
})
model.outliner = compileGroups(true)
model.outliner = compileGroups(true);
if (options.collection_only) {
function filterList(list) {
list.forEachReverse(item => {
if (typeof item == 'string') {
if (!all_collection_children.find(node => node.uuid == item)) {
list.remove(item);
}
} else {
if (item.children instanceof Array) {
filterList(item.children);
}
if (item.uuid && !all_collection_children.find(node => node.uuid == item.uuid)) {
if (!item.children || item.children.length == 0) {
list.remove(item);
}
}
}
})
}
filterList(model.outliner);
}
}
model.textures = [];

View File

@ -282,7 +282,7 @@ Collection.prototype.menu = new Menu([
icon: export_action.icon,
description: export_action.description,
click() {
codec.exportCollection(collection);
codec.writeCollection(collection);
}
}
}
@ -306,6 +306,15 @@ Collection.prototype.menu = new Menu([
codec.exportCollection(collection);
}
}
if (id == 'project') {
new_action = {
name: 'menu.collection.export_project',
icon: 'icon-blockbench_file',
click() {
codec.exportCollection(collection);
}
}
}
actions.push(new_action);
}
return actions;

View File

@ -2112,6 +2112,7 @@
"menu.texture_group.resolve": "Resolve",
"menu.collection.export_project": "Export Project",
"menu.collection.export_as": "Export as \"%0\"",
"menu.preview.background": "Background",