diff --git a/doc/classes/EditorScenePostImport.xml b/doc/classes/EditorScenePostImport.xml index 664ea33dd6a..f95c26c2b00 100644 --- a/doc/classes/EditorScenePostImport.xml +++ b/doc/classes/EditorScenePostImport.xml @@ -1,20 +1,56 @@ + Post process scenes after import + The imported scene can be automatically modified right after import by specifying a 'custom script' that inherits from this class. The [method post_import]-method receives the imported scene's root-node and returns the modified version of the scene + http://docs.godotengine.org/en/latest/learning/workflow/assets/importing_scenes.html?highlight=post%20import + [codeblock] +tool # needed so it runs in editor +extends EditorScenePostImport + +# This sample changes all node names + +# get called right after the scene is imported and gets the root-node +func post_import(scene): + # change all node names to "modified_[oldnodename]" + iterate(scene) + return scene # remember to return the imported scene + +func iterate(node): + if node!=null: + node.name = "modified_"+node.name + for child in node.get_children(): + iterate(child) +[/codeblock] + + + + + Returns the source-file-path which got imported (e.g. [code]res://scene.dae[/code] ) + + + + + + + Returns the resource-folder the imported scene-file is located in + + - + + Gets called after the scene got imported and has to return the modified version of the scene diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index fdbf66f6560..6bdf76fbc8d 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -130,7 +130,9 @@ void EditorSceneImporter::_bind_methods() { ///////////////////////////////// void EditorScenePostImport::_bind_methods() { - BIND_VMETHOD(MethodInfo("post_import", PropertyInfo(Variant::OBJECT, "scene"))); + BIND_VMETHOD(MethodInfo(Variant::OBJECT, "post_import", PropertyInfo(Variant::OBJECT, "scene"))); + ClassDB::bind_method(D_METHOD("get_source_folder"), &EditorScenePostImport::get_source_folder); + ClassDB::bind_method(D_METHOD("get_source_file"), &EditorScenePostImport::get_source_file); } Node *EditorScenePostImport::post_import(Node *p_scene) { @@ -141,6 +143,21 @@ Node *EditorScenePostImport::post_import(Node *p_scene) { return p_scene; } +String EditorScenePostImport::get_source_folder() const { + + return source_folder; +} + +String EditorScenePostImport::get_source_file() const { + + return source_file; +} + +void EditorScenePostImport::init(const String &p_source_folder, const String &p_source_file) { + source_folder = p_source_folder; + source_file = p_source_file; +} + EditorScenePostImport::EditorScenePostImport() { } @@ -1346,6 +1363,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p } if (post_import_script.is_valid()) { + post_import_script->init(base_path, p_source_file); scene = post_import_script->post_import(scene); if (!scene) { EditorNode::add_io_error(TTR("Error running post-import script:") + " " + post_import_script_path); diff --git a/editor/import/resource_importer_scene.h b/editor/import/resource_importer_scene.h index 9c3ec7a29b2..2bde9432fc0 100644 --- a/editor/import/resource_importer_scene.h +++ b/editor/import/resource_importer_scene.h @@ -75,11 +75,17 @@ class EditorScenePostImport : public Reference { GDCLASS(EditorScenePostImport, Reference); + String source_folder; + String source_file; + protected: static void _bind_methods(); public: + String get_source_folder() const; + String get_source_file() const; virtual Node *post_import(Node *p_scene); + virtual void init(const String &p_scene_folder, const String &p_scene_path); EditorScenePostImport(); };