Skip to content

Commit

Permalink
Add support for importing and exporting .blend files
Browse files Browse the repository at this point in the history
Lets you drag or place .blend files in the project folder and it will import the files.

An editor setting sets the location of the blender binary. A empty setting means the feature is disabled.

Checks for Blender 3.0's gltf2 keep texture option.
  • Loading branch information
fire committed Nov 12, 2021
1 parent 7d2900e commit 5156331
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/gltf_blend/SCsub
@@ -0,0 +1,10 @@
#!/usr/bin/env python

Import("env")
Import("env_modules")

env_blend = env_modules.Clone()
env_blend.Prepend(CPPPATH=["."])

# Godot's own source files
env_blend.add_source_files(env.modules_sources, "*.cpp")
17 changes: 17 additions & 0 deletions modules/gltf_blend/config.py
@@ -0,0 +1,17 @@
def can_build(env, platform):
return not env["disable_3d"]


def configure(env):
pass


def get_doc_classes():
return [
"EditorSceneFormatImporterBlend",
"SceneExporterBlendPlugin",
]


def get_doc_path():
return "doc_classes"
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="EditorSceneFormatImporterBlend" inherits="EditorSceneFormatImporter" version="4.0">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
</class>
124 changes: 124 additions & 0 deletions modules/gltf_blend/editor_scene_exporter_blend_plugin.cpp
@@ -0,0 +1,124 @@
/*************************************************************************/
/* editor_scene_exporter_blend_plugin.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#if TOOLS_ENABLED
#include "editor_scene_exporter_blend_plugin.h"
#include "core/config/project_settings.h"
#include "core/core_bind.h"
#include "core/error/error_list.h"
#include "core/object/object.h"
#include "core/templates/vector.h"
#include "editor/editor_file_system.h"
#include "editor/editor_node.h"
#include "modules/gltf/gltf_document.h"
#include "modules/gltf/gltf_state.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/gui/check_box.h"
#include "scene/main/node.h"

String SceneExporterBlendPlugin::get_name() const {
return "ConvertBlend";
}

bool SceneExporterBlendPlugin::has_main_screen() const {
return false;
}

SceneExporterBlendPlugin::SceneExporterBlendPlugin(EditorNode *p_node) {
editor = p_node;
file_export_lib = memnew(EditorFileDialog);
editor->get_gui_base()->add_child(file_export_lib);
file_export_lib->connect("file_selected", callable_mp(this, &SceneExporterBlendPlugin::_blend_dialog_action));
file_export_lib->set_title(TTR("Export Library"));
file_export_lib->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
file_export_lib->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
file_export_lib->clear_filters();
file_export_lib->add_filter("*.blend");
file_export_lib->set_title(TTR("Export Mesh Blend"));
String gltf_scene_name = TTR("Export Blend...");
add_tool_menu_item(gltf_scene_name, callable_mp(this, &SceneExporterBlendPlugin::convert_scene_to_blend));
}

void SceneExporterBlendPlugin::_blend_dialog_action(String p_file) {
Node *root = editor->get_tree()->get_edited_scene_root();
if (!root) {
editor->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
return;
}
List<String> deps;
Ref<GLTFDocument> doc;
doc.instantiate();
String blend_to_scene_setting = "import bpy, os, sys;bpy.context.scene.render.fps=30.0;bpy.ops.import_scene.gltf(filepath='GODOT_SOURCE', bone_heuristic='BLENDER');bpy.ops.wm.save_mainfile(filepath='GODOT_SINK');";
String script = blend_to_scene_setting;
String source = "res://.godot/imported/" + p_file.get_file().get_basename().camelcase_to_underscore() + "-" + p_file.md5_text() + ".glb";
int32_t flags = EditorSceneFormatImporter::IMPORT_GENERATE_TANGENT_ARRAYS | EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS;
Error err = doc->save_scene(root, source, source, flags, 30.0f, Ref<GLTFState>());
if (err != OK) {
ERR_PRINT(vformat("Blend save gltf scene error %s.", itos(err)));
}
String source_global = ProjectSettings::get_singleton()->globalize_path(source);
source_global = source_global.c_escape();
script = script.replace("GODOT_SOURCE", source_global);
String sink = ProjectSettings::get_singleton()->globalize_path(p_file);
String sink_global = ProjectSettings::get_singleton()->globalize_path(sink);
sink_global = sink_global.c_escape();
script = script.replace("GODOT_SINK", sink_global);
Array standard_out;
Vector<String> args;
args.push_back("--background");
args.push_back("--python-expr");
print_line(script);
args.push_back(script);
String addon_path = EditorSettings::get_singleton()->get_setting("filesystem/blend/blender_path");
int32_t ret = core_bind::OS::get_singleton()->execute(addon_path, args, standard_out, true);
for (int32_t line_i = 0; line_i < standard_out.size(); line_i++) {
print_line(standard_out[line_i]);
}
if (ret != OK) {
print_error("Blender returned " + itos(ret));
return;
}
}

void SceneExporterBlendPlugin::convert_scene_to_blend() {
Node *root = editor->get_tree()->get_edited_scene_root();
if (!root) {
editor->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
return;
}
String filename = String(root->get_scene_file_path().get_file().get_basename());
if (filename.is_empty()) {
filename = root->get_name();
}
file_export_lib->set_current_file(filename + String(".blend"));
file_export_lib->popup_centered_ratio();
}

#endif // TOOLS_ENABLED
52 changes: 52 additions & 0 deletions modules/gltf_blend/editor_scene_exporter_blend_plugin.h
@@ -0,0 +1,52 @@
/*************************************************************************/
/* editor_scene_exporter_blend_plugin.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef EDITOR_SCENE_EXPORTER_BLEND_PLUGIN_H
#define EDITOR_SCENE_EXPORTER_BLEND_PLUGIN_H

#if TOOLS_ENABLED
#include "editor/editor_plugin.h"

#include "editor_scene_importer_blend.h"

class SceneExporterBlendPlugin : public EditorPlugin {
GDCLASS(SceneExporterBlendPlugin, EditorPlugin);
EditorNode *editor = nullptr;
EditorFileDialog *file_export_lib = nullptr;
void _blend_dialog_action(String p_file);
void convert_scene_to_blend();

public:
virtual String get_name() const override;
bool has_main_screen() const override;
SceneExporterBlendPlugin(class EditorNode *p_node);
};
#endif // TOOLS_ENABLED
#endif // EDITOR_SCENE_EXPORTER_BLEND_PLUGIN_H
116 changes: 116 additions & 0 deletions modules/gltf_blend/editor_scene_importer_blend.cpp
@@ -0,0 +1,116 @@
/*************************************************************************/
/* editor_scene_importer_blend.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#if TOOLS_ENABLED
#include "editor_scene_importer_blend.h"

#include "core/core_bind.h"
#include "core/object/ref_counted.h"
#include "modules/gltf/editor_scene_importer_gltf.h"
#include "scene/3d/node_3d.h"
#include "scene/animation/animation_player.h"
#include "scene/resources/animation.h"

uint32_t EditorSceneFormatImporterBlend::get_import_flags() const {
return ImportFlags::IMPORT_SCENE;
}

void EditorSceneFormatImporterBlend::get_extensions(List<String> *r_extensions) const {
r_extensions->push_back("blend");
}

Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path,
uint32_t p_flags, int p_bake_fps,
List<String> *r_missing_deps,
Error *r_err) {
// TODO: fire 2021-11-11 Handle OCTAHEDRAL_COMPRESSION
String addon_path = EditorSettings::get_singleton()->get_setting("filesystem/blend/blender_path");
String scene_to_blend_setting = "import bpy, os, sys;"
"bpy.ops.wm.open_mainfile(filepath='GODOT_SOURCE');"
"bpy.ops.export_scene.gltf("
"filepath='GODOT_SINK',"
"export_format='GLTF_SEPARATE',"
"export_keep_originals=True,"
"export_texture_dir='GODOT_TEXTURE_DIR',"
"export_colors=True,"
"export_all_influences=True,"
"export_extras=True,"
"export_cameras=True,"
"export_lights=True) if bpy.app.version >= (3, 0, 0) else "
"bpy.ops.export_scene.gltf("
"filepath='GODOT_SINK',"
"export_format='GLTF_SEPARATE',"
"export_texture_dir='GODOT_TEXTURE_DIR',"
"export_colors=True,"
"export_all_influences=True,"
"export_extras=True,"
"export_cameras=True,"
"export_lights=True);";
String script = scene_to_blend_setting;
String source = ProjectSettings::get_singleton()->globalize_path(p_path);
String source_global = ProjectSettings::get_singleton()->globalize_path(source);
source_global = source_global.c_escape();
core_bind::Directory dir;
String texture_global = "res://.godot/imported/" + p_path.get_file().get_basename() + "-" + p_path.md5_text() + "_textures";
texture_global = ProjectSettings::get_singleton()->globalize_path(texture_global);
dir.make_dir(texture_global);
texture_global = texture_global.c_escape();
script = script.replace("GODOT_TEXTURE_DIR", texture_global);
script = script.replace("GODOT_SOURCE", source_global);
String sink = "res://.godot/imported/" + p_path.get_file().get_basename() + "-" + p_path.md5_text() + ".gltf";
String sink_global = ProjectSettings::get_singleton()->globalize_path(sink);
sink_global = sink_global.c_escape();
script = script.replace("GODOT_SINK", sink_global);
Vector<String> args;
args.push_back("--background");
args.push_back("--python-expr");
args.push_back(script);
print_line(script);
Array standard_out;
int32_t ret = core_bind::OS::get_singleton()->execute(addon_path, args, standard_out, true);
for (int32_t line_i = 0; line_i < standard_out.size(); line_i++) {
print_line(standard_out[line_i]);
}
if (ret != OK) {
print_error("Blender returned " + itos(ret));
return nullptr;
}
Ref<EditorSceneFormatImporterGLTF> gltf_importer;
gltf_importer.instantiate();
return gltf_importer->call("import_scene_from_other_importer", sink_global, p_flags, p_bake_fps);
}

Ref<Animation> EditorSceneFormatImporterBlend::import_animation(const String &p_path,
uint32_t p_flags,
int p_bake_fps) {
return Ref<Animation>();
}

#endif // TOOLS_ENABLED
53 changes: 53 additions & 0 deletions modules/gltf_blend/editor_scene_importer_blend.h
@@ -0,0 +1,53 @@
/*************************************************************************/
/* editor_scene_importer_blend.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef EDITOR_SCENE_IMPORTER_BLEND_H
#define EDITOR_SCENE_IMPORTER_BLEND_H
#ifdef TOOLS_ENABLED

#include "editor/editor_settings.h"
#include "editor/import/resource_importer_scene.h"
#include "scene/main/node.h"
#include "scene/resources/packed_scene.h"

class Animation;

class EditorSceneFormatImporterBlend : public EditorSceneFormatImporter {
GDCLASS(EditorSceneFormatImporterBlend, EditorSceneFormatImporter);

public:
virtual uint32_t get_import_flags() const override;
virtual void get_extensions(List<String> *r_extensions) const override;
virtual Node *import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err = nullptr) override;
virtual Ref<Animation> import_animation(const String &p_path,
uint32_t p_flags, int p_bake_fps) override;
};
#endif // TOOLS_ENABLED
#endif // EDITOR_SCENE_IMPORTER_BLEND_H

0 comments on commit 5156331

Please sign in to comment.