/**************************************************************************/1/* editor_script.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_script.h"3132#include "editor/editor_interface.h"33#include "editor/editor_node.h"34#include "editor/editor_undo_redo_manager.h"35#include "editor/scene/editor_scene_tabs.h"36#include "scene/main/node.h"37#include "scene/resources/packed_scene.h"3839void EditorScript::add_root_node(Node *p_node) {40if (!EditorNode::get_singleton()) {41EditorNode::add_io_error("EditorScript::add_root_node: " + TTR("Write your logic in the _run() method."));42return;43}4445if (EditorNode::get_singleton()->get_edited_scene()) {46EditorNode::add_io_error("EditorScript::add_root_node: " + TTR("The current scene already has a root node."));47return;48}4950const String &scene_path = p_node->get_scene_file_path();51if (!scene_path.is_empty()) {52Ref<PackedScene> scene = ResourceLoader::load(scene_path);53if (scene.is_valid()) {54memfree(scene->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE)); // Ensure node cache.5556p_node->set_scene_inherited_state(scene->get_state());57p_node->set_scene_file_path(String());58}59}6061EditorNode::get_singleton()->set_edited_scene(p_node);62EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(EditorNode::get_editor_data().get_current_edited_scene_history_id());63EditorSceneTabs::get_singleton()->update_scene_tabs();64}6566Node *EditorScript::get_scene() const {67if (!EditorNode::get_singleton()) {68EditorNode::add_io_error("EditorScript::get_scene: " + TTR("Write your logic in the _run() method."));69return nullptr;70}7172return EditorNode::get_singleton()->get_edited_scene();73}7475EditorInterface *EditorScript::get_editor_interface() const {76return EditorInterface::get_singleton();77}7879void EditorScript::run() {80GDVIRTUAL_CALL(_run);81}8283void EditorScript::_bind_methods() {84ClassDB::bind_method(D_METHOD("add_root_node", "node"), &EditorScript::add_root_node);85ClassDB::bind_method(D_METHOD("get_scene"), &EditorScript::get_scene);86ClassDB::bind_method(D_METHOD("get_editor_interface"), &EditorScript::get_editor_interface);8788GDVIRTUAL_BIND(_run);89}909192