1 /*************************************************************************/
2 /*  resource_importer_scene.cpp                                          */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "resource_importer_scene.h"
32 
33 #include "core/io/resource_saver.h"
34 #include "editor/editor_node.h"
35 #include "scene/3d/collision_shape.h"
36 #include "scene/3d/mesh_instance.h"
37 #include "scene/3d/navigation.h"
38 #include "scene/3d/physics_body.h"
39 #include "scene/3d/vehicle_body.h"
40 #include "scene/animation/animation_player.h"
41 #include "scene/resources/animation.h"
42 #include "scene/resources/box_shape.h"
43 #include "scene/resources/packed_scene.h"
44 #include "scene/resources/plane_shape.h"
45 #include "scene/resources/ray_shape.h"
46 #include "scene/resources/resource_format_text.h"
47 #include "scene/resources/sphere_shape.h"
48 
get_import_flags() const49 uint32_t EditorSceneImporter::get_import_flags() const {
50 
51 	if (get_script_instance()) {
52 		return get_script_instance()->call("_get_import_flags");
53 	}
54 
55 	ERR_FAIL_V(0);
56 }
get_extensions(List<String> * r_extensions) const57 void EditorSceneImporter::get_extensions(List<String> *r_extensions) const {
58 
59 	if (get_script_instance()) {
60 		Array arr = get_script_instance()->call("_get_extensions");
61 		for (int i = 0; i < arr.size(); i++) {
62 			r_extensions->push_back(arr[i]);
63 		}
64 		return;
65 	}
66 
67 	ERR_FAIL();
68 }
import_scene(const String & p_path,uint32_t p_flags,int p_bake_fps,List<String> * r_missing_deps,Error * r_err)69 Node *EditorSceneImporter::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) {
70 
71 	if (get_script_instance()) {
72 		return get_script_instance()->call("_import_scene", p_path, p_flags, p_bake_fps);
73 	}
74 
75 	ERR_FAIL_V(NULL);
76 }
77 
import_animation(const String & p_path,uint32_t p_flags,int p_bake_fps)78 Ref<Animation> EditorSceneImporter::import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps) {
79 
80 	if (get_script_instance()) {
81 		return get_script_instance()->call("_import_animation", p_path, p_flags);
82 	}
83 
84 	ERR_FAIL_V(NULL);
85 }
86 
87 //for documenters, these functions are useful when an importer calls an external conversion helper (like, fbx2gltf),
88 //and you want to load the resulting file
89 
import_scene_from_other_importer(const String & p_path,uint32_t p_flags,int p_bake_fps)90 Node *EditorSceneImporter::import_scene_from_other_importer(const String &p_path, uint32_t p_flags, int p_bake_fps) {
91 
92 	return ResourceImporterScene::get_singleton()->import_scene_from_other_importer(this, p_path, p_flags, p_bake_fps);
93 }
94 
import_animation_from_other_importer(const String & p_path,uint32_t p_flags,int p_bake_fps)95 Ref<Animation> EditorSceneImporter::import_animation_from_other_importer(const String &p_path, uint32_t p_flags, int p_bake_fps) {
96 
97 	return ResourceImporterScene::get_singleton()->import_animation_from_other_importer(this, p_path, p_flags, p_bake_fps);
98 }
99 
_bind_methods()100 void EditorSceneImporter::_bind_methods() {
101 
102 	ClassDB::bind_method(D_METHOD("import_scene_from_other_importer", "path", "flags", "bake_fps"), &EditorSceneImporter::import_scene_from_other_importer);
103 	ClassDB::bind_method(D_METHOD("import_animation_from_other_importer", "path", "flags", "bake_fps"), &EditorSceneImporter::import_animation_from_other_importer);
104 
105 	BIND_VMETHOD(MethodInfo(Variant::INT, "_get_import_flags"));
106 	BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_extensions"));
107 
108 	MethodInfo mi = MethodInfo(Variant::OBJECT, "_import_scene", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "flags"), PropertyInfo(Variant::INT, "bake_fps"));
109 	mi.return_val.class_name = "Node";
110 	BIND_VMETHOD(mi);
111 	mi = MethodInfo(Variant::OBJECT, "_import_animation", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "flags"), PropertyInfo(Variant::INT, "bake_fps"));
112 	mi.return_val.class_name = "Animation";
113 	BIND_VMETHOD(mi);
114 
115 	BIND_CONSTANT(IMPORT_SCENE);
116 	BIND_CONSTANT(IMPORT_ANIMATION);
117 	BIND_CONSTANT(IMPORT_ANIMATION_DETECT_LOOP);
118 	BIND_CONSTANT(IMPORT_ANIMATION_OPTIMIZE);
119 	BIND_CONSTANT(IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS);
120 	BIND_CONSTANT(IMPORT_ANIMATION_KEEP_VALUE_TRACKS);
121 	BIND_CONSTANT(IMPORT_GENERATE_TANGENT_ARRAYS);
122 	BIND_CONSTANT(IMPORT_FAIL_ON_MISSING_DEPENDENCIES);
123 	BIND_CONSTANT(IMPORT_MATERIALS_IN_INSTANCES);
124 	BIND_CONSTANT(IMPORT_USE_COMPRESSION);
125 }
126 
127 /////////////////////////////////
_bind_methods()128 void EditorScenePostImport::_bind_methods() {
129 
130 	BIND_VMETHOD(MethodInfo(Variant::OBJECT, "post_import", PropertyInfo(Variant::OBJECT, "scene")));
131 	ClassDB::bind_method(D_METHOD("get_source_folder"), &EditorScenePostImport::get_source_folder);
132 	ClassDB::bind_method(D_METHOD("get_source_file"), &EditorScenePostImport::get_source_file);
133 }
134 
post_import(Node * p_scene)135 Node *EditorScenePostImport::post_import(Node *p_scene) {
136 
137 	if (get_script_instance())
138 		return get_script_instance()->call("post_import", p_scene);
139 
140 	return p_scene;
141 }
142 
get_source_folder() const143 String EditorScenePostImport::get_source_folder() const {
144 
145 	return source_folder;
146 }
147 
get_source_file() const148 String EditorScenePostImport::get_source_file() const {
149 
150 	return source_file;
151 }
152 
init(const String & p_source_folder,const String & p_source_file)153 void EditorScenePostImport::init(const String &p_source_folder, const String &p_source_file) {
154 	source_folder = p_source_folder;
155 	source_file = p_source_file;
156 }
157 
EditorScenePostImport()158 EditorScenePostImport::EditorScenePostImport() {
159 }
160 
get_importer_name() const161 String ResourceImporterScene::get_importer_name() const {
162 
163 	return "scene";
164 }
165 
get_visible_name() const166 String ResourceImporterScene::get_visible_name() const {
167 
168 	return "Scene";
169 }
170 
get_recognized_extensions(List<String> * p_extensions) const171 void ResourceImporterScene::get_recognized_extensions(List<String> *p_extensions) const {
172 
173 	for (Set<Ref<EditorSceneImporter> >::Element *E = importers.front(); E; E = E->next()) {
174 		E->get()->get_extensions(p_extensions);
175 	}
176 }
177 
get_save_extension() const178 String ResourceImporterScene::get_save_extension() const {
179 	return "scn";
180 }
181 
get_resource_type() const182 String ResourceImporterScene::get_resource_type() const {
183 
184 	return "PackedScene";
185 }
186 
get_option_visibility(const String & p_option,const Map<StringName,Variant> & p_options) const187 bool ResourceImporterScene::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
188 
189 	if (p_option.begins_with("animation/")) {
190 		if (p_option != "animation/import" && !bool(p_options["animation/import"]))
191 			return false;
192 
193 		if (p_option == "animation/keep_custom_tracks" && int(p_options["animation/storage"]) == 0)
194 			return false;
195 
196 		if (p_option.begins_with("animation/optimizer/") && p_option != "animation/optimizer/enabled" && !bool(p_options["animation/optimizer/enabled"]))
197 			return false;
198 
199 		if (p_option.begins_with("animation/clip_")) {
200 			int max_clip = p_options["animation/clips/amount"];
201 			int clip = p_option.get_slice("/", 1).get_slice("_", 1).to_int() - 1;
202 			if (clip >= max_clip)
203 				return false;
204 		}
205 	}
206 
207 	if (p_option == "materials/keep_on_reimport" && int(p_options["materials/storage"]) == 0) {
208 		return false;
209 	}
210 
211 	if (p_option == "meshes/lightmap_texel_size" && int(p_options["meshes/light_baking"]) < 2) {
212 		return false;
213 	}
214 
215 	return true;
216 }
217 
get_preset_count() const218 int ResourceImporterScene::get_preset_count() const {
219 	return PRESET_MAX;
220 }
get_preset_name(int p_idx) const221 String ResourceImporterScene::get_preset_name(int p_idx) const {
222 
223 	switch (p_idx) {
224 		case PRESET_SINGLE_SCENE: return TTR("Import as Single Scene");
225 		case PRESET_SEPARATE_ANIMATIONS: return TTR("Import with Separate Animations");
226 		case PRESET_SEPARATE_MATERIALS: return TTR("Import with Separate Materials");
227 		case PRESET_SEPARATE_MESHES: return TTR("Import with Separate Objects");
228 		case PRESET_SEPARATE_MESHES_AND_MATERIALS: return TTR("Import with Separate Objects+Materials");
229 		case PRESET_SEPARATE_MESHES_AND_ANIMATIONS: return TTR("Import with Separate Objects+Animations");
230 		case PRESET_SEPARATE_MATERIALS_AND_ANIMATIONS: return TTR("Import with Separate Materials+Animations");
231 		case PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS: return TTR("Import with Separate Objects+Materials+Animations");
232 		case PRESET_MULTIPLE_SCENES: return TTR("Import as Multiple Scenes");
233 		case PRESET_MULTIPLE_SCENES_AND_MATERIALS: return TTR("Import as Multiple Scenes+Materials");
234 	}
235 
236 	return "";
237 }
238 
_teststr(const String & p_what,const String & p_str)239 static bool _teststr(const String &p_what, const String &p_str) {
240 
241 	String what = p_what;
242 
243 	//remove trailing spaces and numbers, some apps like blender add ".number" to duplicates so also compensate for this
244 	while (what.length() && ((what[what.length() - 1] >= '0' && what[what.length() - 1] <= '9') || what[what.length() - 1] <= 32 || what[what.length() - 1] == '.')) {
245 
246 		what = what.substr(0, what.length() - 1);
247 	}
248 
249 	if (what.findn("$" + p_str) != -1) //blender and other stuff
250 		return true;
251 	if (what.to_lower().ends_with("-" + p_str)) //collada only supports "_" and "-" besides letters
252 		return true;
253 	if (what.to_lower().ends_with("_" + p_str)) //collada only supports "_" and "-" besides letters
254 		return true;
255 	return false;
256 }
257 
_fixstr(const String & p_what,const String & p_str)258 static String _fixstr(const String &p_what, const String &p_str) {
259 
260 	String what = p_what;
261 
262 	//remove trailing spaces and numbers, some apps like blender add ".number" to duplicates so also compensate for this
263 	while (what.length() && ((what[what.length() - 1] >= '0' && what[what.length() - 1] <= '9') || what[what.length() - 1] <= 32 || what[what.length() - 1] == '.')) {
264 
265 		what = what.substr(0, what.length() - 1);
266 	}
267 
268 	String end = p_what.substr(what.length(), p_what.length() - what.length());
269 
270 	if (what.findn("$" + p_str) != -1) //blender and other stuff
271 		return what.replace("$" + p_str, "") + end;
272 	if (what.to_lower().ends_with("-" + p_str)) //collada only supports "_" and "-" besides letters
273 		return what.substr(0, what.length() - (p_str.length() + 1)) + end;
274 	if (what.to_lower().ends_with("_" + p_str)) //collada only supports "_" and "-" besides letters
275 		return what.substr(0, what.length() - (p_str.length() + 1)) + end;
276 	return what;
277 }
278 
_gen_shape_list(const Ref<Mesh> & mesh,List<Ref<Shape>> & r_shape_list,bool p_convex)279 static void _gen_shape_list(const Ref<Mesh> &mesh, List<Ref<Shape> > &r_shape_list, bool p_convex) {
280 
281 	if (!p_convex) {
282 
283 		Ref<Shape> shape = mesh->create_trimesh_shape();
284 		r_shape_list.push_back(shape);
285 	} else {
286 
287 		Vector<Ref<Shape> > cd = mesh->convex_decompose();
288 		if (cd.size()) {
289 			for (int i = 0; i < cd.size(); i++) {
290 				r_shape_list.push_back(cd[i]);
291 			}
292 		}
293 	}
294 }
295 
_fix_node(Node * p_node,Node * p_root,Map<Ref<Mesh>,List<Ref<Shape>>> & collision_map,LightBakeMode p_light_bake_mode)296 Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh>, List<Ref<Shape> > > &collision_map, LightBakeMode p_light_bake_mode) {
297 
298 	// children first
299 	for (int i = 0; i < p_node->get_child_count(); i++) {
300 
301 		Node *r = _fix_node(p_node->get_child(i), p_root, collision_map, p_light_bake_mode);
302 		if (!r) {
303 			i--; //was erased
304 		}
305 	}
306 
307 	String name = p_node->get_name();
308 
309 	bool isroot = p_node == p_root;
310 
311 	if (!isroot && _teststr(name, "noimp")) {
312 
313 		memdelete(p_node);
314 		return NULL;
315 	}
316 
317 	if (Object::cast_to<MeshInstance>(p_node)) {
318 
319 		MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
320 
321 		Ref<ArrayMesh> m = mi->get_mesh();
322 
323 		if (m.is_valid()) {
324 
325 			for (int i = 0; i < m->get_surface_count(); i++) {
326 
327 				Ref<SpatialMaterial> mat = m->surface_get_material(i);
328 				if (!mat.is_valid())
329 					continue;
330 
331 				if (_teststr(mat->get_name(), "alpha")) {
332 
333 					mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
334 					mat->set_name(_fixstr(mat->get_name(), "alpha"));
335 				}
336 				if (_teststr(mat->get_name(), "vcol")) {
337 
338 					mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
339 					mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
340 					mat->set_name(_fixstr(mat->get_name(), "vcol"));
341 				}
342 			}
343 		}
344 
345 		if (p_light_bake_mode != LIGHT_BAKE_DISABLED) {
346 
347 			mi->set_flag(GeometryInstance::FLAG_USE_BAKED_LIGHT, true);
348 		}
349 	}
350 
351 	if (Object::cast_to<AnimationPlayer>(p_node)) {
352 		//remove animations referencing non-importable nodes
353 		AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
354 
355 		List<StringName> anims;
356 		ap->get_animation_list(&anims);
357 		for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
358 
359 			Ref<Animation> anim = ap->get_animation(E->get());
360 			ERR_CONTINUE(anim.is_null());
361 			for (int i = 0; i < anim->get_track_count(); i++) {
362 				NodePath path = anim->track_get_path(i);
363 
364 				for (int j = 0; j < path.get_name_count(); j++) {
365 					String node = path.get_name(j);
366 					if (_teststr(node, "noimp")) {
367 						anim->remove_track(i);
368 						i--;
369 						break;
370 					}
371 				}
372 			}
373 		}
374 	}
375 
376 	if (_teststr(name, "colonly") || _teststr(name, "convcolonly")) {
377 
378 		if (isroot)
379 			return p_node;
380 		MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
381 		if (mi) {
382 			Ref<Mesh> mesh = mi->get_mesh();
383 
384 			if (mesh.is_valid()) {
385 				List<Ref<Shape> > shapes;
386 				String fixed_name;
387 				if (collision_map.has(mesh)) {
388 					shapes = collision_map[mesh];
389 				} else if (_teststr(name, "colonly")) {
390 					_gen_shape_list(mesh, shapes, false);
391 					collision_map[mesh] = shapes;
392 				} else if (_teststr(name, "convcolonly")) {
393 					_gen_shape_list(mesh, shapes, true);
394 					collision_map[mesh] = shapes;
395 				}
396 
397 				if (_teststr(name, "colonly")) {
398 					fixed_name = _fixstr(name, "colonly");
399 				} else if (_teststr(name, "convcolonly")) {
400 					fixed_name = _fixstr(name, "convcolonly");
401 				}
402 
403 				ERR_FAIL_COND_V(fixed_name == String(), NULL);
404 
405 				if (shapes.size()) {
406 
407 					StaticBody *col = memnew(StaticBody);
408 					col->set_transform(mi->get_transform());
409 					col->set_name(fixed_name);
410 					p_node->replace_by(col);
411 					memdelete(p_node);
412 					p_node = col;
413 
414 					int idx = 0;
415 					for (List<Ref<Shape> >::Element *E = shapes.front(); E; E = E->next()) {
416 
417 						CollisionShape *cshape = memnew(CollisionShape);
418 						cshape->set_shape(E->get());
419 						col->add_child(cshape);
420 
421 						cshape->set_name("shape" + itos(idx));
422 						cshape->set_owner(col->get_owner());
423 						idx++;
424 					}
425 				}
426 			}
427 
428 		} else if (p_node->has_meta("empty_draw_type")) {
429 			String empty_draw_type = String(p_node->get_meta("empty_draw_type"));
430 			StaticBody *sb = memnew(StaticBody);
431 			sb->set_name(_fixstr(name, "colonly"));
432 			Object::cast_to<Spatial>(sb)->set_transform(Object::cast_to<Spatial>(p_node)->get_transform());
433 			p_node->replace_by(sb);
434 			memdelete(p_node);
435 			p_node = NULL;
436 			CollisionShape *colshape = memnew(CollisionShape);
437 			if (empty_draw_type == "CUBE") {
438 				BoxShape *boxShape = memnew(BoxShape);
439 				boxShape->set_extents(Vector3(1, 1, 1));
440 				colshape->set_shape(boxShape);
441 				colshape->set_name("BoxShape");
442 			} else if (empty_draw_type == "SINGLE_ARROW") {
443 				RayShape *rayShape = memnew(RayShape);
444 				rayShape->set_length(1);
445 				colshape->set_shape(rayShape);
446 				colshape->set_name("RayShape");
447 				Object::cast_to<Spatial>(sb)->rotate_x(Math_PI / 2);
448 			} else if (empty_draw_type == "IMAGE") {
449 				PlaneShape *planeShape = memnew(PlaneShape);
450 				colshape->set_shape(planeShape);
451 				colshape->set_name("PlaneShape");
452 			} else {
453 				SphereShape *sphereShape = memnew(SphereShape);
454 				sphereShape->set_radius(1);
455 				colshape->set_shape(sphereShape);
456 				colshape->set_name("SphereShape");
457 			}
458 			sb->add_child(colshape);
459 			colshape->set_owner(sb->get_owner());
460 		}
461 
462 	} else if (_teststr(name, "rigid") && Object::cast_to<MeshInstance>(p_node)) {
463 
464 		if (isroot)
465 			return p_node;
466 
467 		MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
468 		Ref<Mesh> mesh = mi->get_mesh();
469 
470 		if (mesh.is_valid()) {
471 			List<Ref<Shape> > shapes;
472 			if (collision_map.has(mesh)) {
473 				shapes = collision_map[mesh];
474 			} else {
475 				_gen_shape_list(mesh, shapes, true);
476 			}
477 
478 			RigidBody *rigid_body = memnew(RigidBody);
479 			rigid_body->set_name(_fixstr(name, "rigid"));
480 			p_node->replace_by(rigid_body);
481 			rigid_body->set_transform(mi->get_transform());
482 			p_node = rigid_body;
483 			mi->set_name("mesh");
484 			mi->set_transform(Transform());
485 			rigid_body->add_child(mi);
486 			mi->set_owner(rigid_body->get_owner());
487 
488 			int idx = 0;
489 			for (List<Ref<Shape> >::Element *E = shapes.front(); E; E = E->next()) {
490 
491 				CollisionShape *cshape = memnew(CollisionShape);
492 				cshape->set_shape(E->get());
493 				rigid_body->add_child(cshape);
494 
495 				cshape->set_name("shape" + itos(idx));
496 				cshape->set_owner(p_node->get_owner());
497 				idx++;
498 			}
499 		}
500 
501 	} else if ((_teststr(name, "col") || (_teststr(name, "convcol"))) && Object::cast_to<MeshInstance>(p_node)) {
502 
503 		MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
504 
505 		Ref<Mesh> mesh = mi->get_mesh();
506 
507 		if (mesh.is_valid()) {
508 			List<Ref<Shape> > shapes;
509 			String fixed_name;
510 			if (collision_map.has(mesh)) {
511 				shapes = collision_map[mesh];
512 			} else if (_teststr(name, "col")) {
513 				_gen_shape_list(mesh, shapes, false);
514 				collision_map[mesh] = shapes;
515 			} else if (_teststr(name, "convcol")) {
516 				_gen_shape_list(mesh, shapes, true);
517 				collision_map[mesh] = shapes;
518 			}
519 
520 			if (_teststr(name, "col")) {
521 				fixed_name = _fixstr(name, "col");
522 			} else if (_teststr(name, "convcol")) {
523 				fixed_name = _fixstr(name, "convcol");
524 			}
525 
526 			if (fixed_name != String()) {
527 				if (mi->get_parent() && !mi->get_parent()->has_node(fixed_name)) {
528 					mi->set_name(fixed_name);
529 				}
530 			}
531 
532 			if (shapes.size()) {
533 				StaticBody *col = memnew(StaticBody);
534 				col->set_name("static_collision");
535 				mi->add_child(col);
536 				col->set_owner(mi->get_owner());
537 
538 				int idx = 0;
539 				for (List<Ref<Shape> >::Element *E = shapes.front(); E; E = E->next()) {
540 
541 					CollisionShape *cshape = memnew(CollisionShape);
542 					cshape->set_shape(E->get());
543 					col->add_child(cshape);
544 
545 					cshape->set_name("shape" + itos(idx));
546 					cshape->set_owner(p_node->get_owner());
547 
548 					idx++;
549 				}
550 			}
551 		}
552 
553 	} else if (_teststr(name, "navmesh") && Object::cast_to<MeshInstance>(p_node)) {
554 
555 		if (isroot)
556 			return p_node;
557 
558 		MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
559 
560 		Ref<ArrayMesh> mesh = mi->get_mesh();
561 		ERR_FAIL_COND_V(mesh.is_null(), NULL);
562 		NavigationMeshInstance *nmi = memnew(NavigationMeshInstance);
563 
564 		nmi->set_name(_fixstr(name, "navmesh"));
565 		Ref<NavigationMesh> nmesh = memnew(NavigationMesh);
566 		nmesh->create_from_mesh(mesh);
567 		nmi->set_navigation_mesh(nmesh);
568 		Object::cast_to<Spatial>(nmi)->set_transform(mi->get_transform());
569 		p_node->replace_by(nmi);
570 		memdelete(p_node);
571 		p_node = nmi;
572 	} else if (_teststr(name, "vehicle")) {
573 
574 		if (isroot)
575 			return p_node;
576 
577 		Node *owner = p_node->get_owner();
578 		Spatial *s = Object::cast_to<Spatial>(p_node);
579 		VehicleBody *bv = memnew(VehicleBody);
580 		String n = _fixstr(p_node->get_name(), "vehicle");
581 		bv->set_name(n);
582 		p_node->replace_by(bv);
583 		p_node->set_name(n);
584 		bv->add_child(p_node);
585 		bv->set_owner(owner);
586 		p_node->set_owner(owner);
587 		bv->set_transform(s->get_transform());
588 		s->set_transform(Transform());
589 
590 		p_node = bv;
591 
592 	} else if (_teststr(name, "wheel")) {
593 
594 		if (isroot)
595 			return p_node;
596 
597 		Node *owner = p_node->get_owner();
598 		Spatial *s = Object::cast_to<Spatial>(p_node);
599 		VehicleWheel *bv = memnew(VehicleWheel);
600 		String n = _fixstr(p_node->get_name(), "wheel");
601 		bv->set_name(n);
602 		p_node->replace_by(bv);
603 		p_node->set_name(n);
604 		bv->add_child(p_node);
605 		bv->set_owner(owner);
606 		p_node->set_owner(owner);
607 		bv->set_transform(s->get_transform());
608 		s->set_transform(Transform());
609 
610 		p_node = bv;
611 
612 	} else if (Object::cast_to<MeshInstance>(p_node)) {
613 
614 		//last attempt, maybe collision inside the mesh data
615 
616 		MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
617 
618 		Ref<ArrayMesh> mesh = mi->get_mesh();
619 		if (!mesh.is_null()) {
620 
621 			List<Ref<Shape> > shapes;
622 			if (collision_map.has(mesh)) {
623 				shapes = collision_map[mesh];
624 			} else if (_teststr(mesh->get_name(), "col")) {
625 				_gen_shape_list(mesh, shapes, false);
626 				collision_map[mesh] = shapes;
627 				mesh->set_name(_fixstr(mesh->get_name(), "col"));
628 			} else if (_teststr(mesh->get_name(), "convcol")) {
629 				_gen_shape_list(mesh, shapes, true);
630 				collision_map[mesh] = shapes;
631 				mesh->set_name(_fixstr(mesh->get_name(), "convcol"));
632 			}
633 
634 			if (shapes.size()) {
635 				StaticBody *col = memnew(StaticBody);
636 				col->set_name("static_collision");
637 				p_node->add_child(col);
638 				col->set_owner(p_node->get_owner());
639 
640 				int idx = 0;
641 				for (List<Ref<Shape> >::Element *E = shapes.front(); E; E = E->next()) {
642 
643 					CollisionShape *cshape = memnew(CollisionShape);
644 					cshape->set_shape(E->get());
645 					col->add_child(cshape);
646 
647 					cshape->set_name("shape" + itos(idx));
648 					cshape->set_owner(p_node->get_owner());
649 					idx++;
650 				}
651 			}
652 		}
653 	}
654 
655 	return p_node;
656 }
657 
_create_clips(Node * scene,const Array & p_clips,bool p_bake_all)658 void ResourceImporterScene::_create_clips(Node *scene, const Array &p_clips, bool p_bake_all) {
659 
660 	if (!scene->has_node(String("AnimationPlayer")))
661 		return;
662 
663 	Node *n = scene->get_node(String("AnimationPlayer"));
664 	ERR_FAIL_COND(!n);
665 	AnimationPlayer *anim = Object::cast_to<AnimationPlayer>(n);
666 	ERR_FAIL_COND(!anim);
667 
668 	if (!anim->has_animation("default"))
669 		return;
670 
671 	Ref<Animation> default_anim = anim->get_animation("default");
672 
673 	for (int i = 0; i < p_clips.size(); i += 4) {
674 
675 		String name = p_clips[i];
676 		float from = p_clips[i + 1];
677 		float to = p_clips[i + 2];
678 		bool loop = p_clips[i + 3];
679 		if (from >= to)
680 			continue;
681 
682 		Ref<Animation> new_anim = memnew(Animation);
683 
684 		for (int j = 0; j < default_anim->get_track_count(); j++) {
685 
686 			List<float> keys;
687 			int kc = default_anim->track_get_key_count(j);
688 			int dtrack = -1;
689 			for (int k = 0; k < kc; k++) {
690 
691 				float kt = default_anim->track_get_key_time(j, k);
692 				if (kt >= from && kt < to) {
693 
694 					//found a key within range, so create track
695 					if (dtrack == -1) {
696 						new_anim->add_track(default_anim->track_get_type(j));
697 						dtrack = new_anim->get_track_count() - 1;
698 						new_anim->track_set_path(dtrack, default_anim->track_get_path(j));
699 
700 						if (kt > (from + 0.01) && k > 0) {
701 
702 							if (default_anim->track_get_type(j) == Animation::TYPE_TRANSFORM) {
703 								Quat q;
704 								Vector3 p;
705 								Vector3 s;
706 								default_anim->transform_track_interpolate(j, from, &p, &q, &s);
707 								new_anim->transform_track_insert_key(dtrack, 0, p, q, s);
708 							}
709 							if (default_anim->track_get_type(j) == Animation::TYPE_VALUE) {
710 								Variant var = default_anim->value_track_interpolate(j, from);
711 								new_anim->track_insert_key(dtrack, 0, var);
712 							}
713 						}
714 					}
715 
716 					if (default_anim->track_get_type(j) == Animation::TYPE_TRANSFORM) {
717 						Quat q;
718 						Vector3 p;
719 						Vector3 s;
720 						default_anim->transform_track_get_key(j, k, &p, &q, &s);
721 						new_anim->transform_track_insert_key(dtrack, kt - from, p, q, s);
722 					}
723 					if (default_anim->track_get_type(j) == Animation::TYPE_VALUE) {
724 						Variant var = default_anim->track_get_key_value(j, k);
725 						new_anim->track_insert_key(dtrack, kt - from, var);
726 					}
727 				}
728 
729 				if (dtrack != -1 && kt >= to) {
730 
731 					if (default_anim->track_get_type(j) == Animation::TYPE_TRANSFORM) {
732 						Quat q;
733 						Vector3 p;
734 						Vector3 s;
735 						default_anim->transform_track_interpolate(j, to, &p, &q, &s);
736 						new_anim->transform_track_insert_key(dtrack, to - from, p, q, s);
737 					}
738 					if (default_anim->track_get_type(j) == Animation::TYPE_VALUE) {
739 						Variant var = default_anim->value_track_interpolate(j, to);
740 						new_anim->track_insert_key(dtrack, to - from, var);
741 					}
742 				}
743 			}
744 
745 			if (dtrack == -1 && p_bake_all) {
746 				new_anim->add_track(default_anim->track_get_type(j));
747 				dtrack = new_anim->get_track_count() - 1;
748 				new_anim->track_set_path(dtrack, default_anim->track_get_path(j));
749 				if (default_anim->track_get_type(j) == Animation::TYPE_TRANSFORM) {
750 
751 					Quat q;
752 					Vector3 p;
753 					Vector3 s;
754 					default_anim->transform_track_interpolate(j, from, &p, &q, &s);
755 					new_anim->transform_track_insert_key(dtrack, 0, p, q, s);
756 					default_anim->transform_track_interpolate(j, to, &p, &q, &s);
757 					new_anim->transform_track_insert_key(dtrack, to - from, p, q, s);
758 				}
759 				if (default_anim->track_get_type(j) == Animation::TYPE_VALUE) {
760 					Variant var = default_anim->value_track_interpolate(j, from);
761 					new_anim->track_insert_key(dtrack, 0, var);
762 					Variant to_var = default_anim->value_track_interpolate(j, to);
763 					new_anim->track_insert_key(dtrack, to - from, to_var);
764 				}
765 			}
766 		}
767 
768 		new_anim->set_loop(loop);
769 		new_anim->set_length(to - from);
770 		anim->add_animation(name, new_anim);
771 	}
772 
773 	anim->remove_animation("default"); //remove default (no longer needed)
774 }
775 
_filter_anim_tracks(Ref<Animation> anim,Set<String> & keep)776 void ResourceImporterScene::_filter_anim_tracks(Ref<Animation> anim, Set<String> &keep) {
777 
778 	Ref<Animation> a = anim;
779 	ERR_FAIL_COND(!a.is_valid());
780 
781 	for (int j = 0; j < a->get_track_count(); j++) {
782 
783 		String path = a->track_get_path(j);
784 
785 		if (!keep.has(path)) {
786 			a->remove_track(j);
787 			j--;
788 		}
789 	}
790 }
791 
_filter_tracks(Node * scene,const String & p_text)792 void ResourceImporterScene::_filter_tracks(Node *scene, const String &p_text) {
793 
794 	if (!scene->has_node(String("AnimationPlayer")))
795 		return;
796 	Node *n = scene->get_node(String("AnimationPlayer"));
797 	ERR_FAIL_COND(!n);
798 	AnimationPlayer *anim = Object::cast_to<AnimationPlayer>(n);
799 	ERR_FAIL_COND(!anim);
800 
801 	Vector<String> strings = p_text.split("\n");
802 	for (int i = 0; i < strings.size(); i++) {
803 
804 		strings.write[i] = strings[i].strip_edges();
805 	}
806 
807 	List<StringName> anim_names;
808 	anim->get_animation_list(&anim_names);
809 	for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) {
810 
811 		String name = E->get();
812 		bool valid_for_this = false;
813 		bool valid = false;
814 
815 		Set<String> keep;
816 		Set<String> keep_local;
817 
818 		for (int i = 0; i < strings.size(); i++) {
819 
820 			if (strings[i].begins_with("@")) {
821 
822 				valid_for_this = false;
823 				for (Set<String>::Element *F = keep_local.front(); F; F = F->next()) {
824 					keep.insert(F->get());
825 				}
826 				keep_local.clear();
827 
828 				Vector<String> filters = strings[i].substr(1, strings[i].length()).split(",");
829 				for (int j = 0; j < filters.size(); j++) {
830 
831 					String fname = filters[j].strip_edges();
832 					if (fname == "")
833 						continue;
834 					int fc = fname[0];
835 					bool plus;
836 					if (fc == '+')
837 						plus = true;
838 					else if (fc == '-')
839 						plus = false;
840 					else
841 						continue;
842 
843 					String filter = fname.substr(1, fname.length()).strip_edges();
844 
845 					if (!name.matchn(filter))
846 						continue;
847 					valid_for_this = plus;
848 				}
849 
850 				if (valid_for_this)
851 					valid = true;
852 
853 			} else if (valid_for_this) {
854 
855 				Ref<Animation> a = anim->get_animation(name);
856 				if (!a.is_valid())
857 					continue;
858 
859 				for (int j = 0; j < a->get_track_count(); j++) {
860 
861 					String path = a->track_get_path(j);
862 
863 					String tname = strings[i];
864 					if (tname == "")
865 						continue;
866 					int fc = tname[0];
867 					bool plus;
868 					if (fc == '+')
869 						plus = true;
870 					else if (fc == '-')
871 						plus = false;
872 					else
873 						continue;
874 
875 					String filter = tname.substr(1, tname.length()).strip_edges();
876 
877 					if (!path.matchn(filter))
878 						continue;
879 
880 					if (plus)
881 						keep_local.insert(path);
882 					else if (!keep.has(path)) {
883 						keep_local.erase(path);
884 					}
885 				}
886 			}
887 		}
888 
889 		if (valid) {
890 			for (Set<String>::Element *F = keep_local.front(); F; F = F->next()) {
891 				keep.insert(F->get());
892 			}
893 			_filter_anim_tracks(anim->get_animation(name), keep);
894 		}
895 	}
896 }
897 
_optimize_animations(Node * scene,float p_max_lin_error,float p_max_ang_error,float p_max_angle)898 void ResourceImporterScene::_optimize_animations(Node *scene, float p_max_lin_error, float p_max_ang_error, float p_max_angle) {
899 
900 	if (!scene->has_node(String("AnimationPlayer")))
901 		return;
902 	Node *n = scene->get_node(String("AnimationPlayer"));
903 	ERR_FAIL_COND(!n);
904 	AnimationPlayer *anim = Object::cast_to<AnimationPlayer>(n);
905 	ERR_FAIL_COND(!anim);
906 
907 	List<StringName> anim_names;
908 	anim->get_animation_list(&anim_names);
909 	for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) {
910 
911 		Ref<Animation> a = anim->get_animation(E->get());
912 		a->optimize(p_max_lin_error, p_max_ang_error, Math::deg2rad(p_max_angle));
913 	}
914 }
915 
_make_extname(const String & p_str)916 static String _make_extname(const String &p_str) {
917 
918 	String ext_name = p_str.replace(".", "_");
919 	ext_name = ext_name.replace(":", "_");
920 	ext_name = ext_name.replace("\"", "_");
921 	ext_name = ext_name.replace("<", "_");
922 	ext_name = ext_name.replace(">", "_");
923 	ext_name = ext_name.replace("/", "_");
924 	ext_name = ext_name.replace("|", "_");
925 	ext_name = ext_name.replace("\\", "_");
926 	ext_name = ext_name.replace("?", "_");
927 	ext_name = ext_name.replace("*", "_");
928 
929 	return ext_name;
930 }
931 
_find_meshes(Node * p_node,Map<Ref<ArrayMesh>,Transform> & meshes)932 void ResourceImporterScene::_find_meshes(Node *p_node, Map<Ref<ArrayMesh>, Transform> &meshes) {
933 
934 	List<PropertyInfo> pi;
935 	p_node->get_property_list(&pi);
936 
937 	MeshInstance *mi = Object::cast_to<MeshInstance>(p_node);
938 
939 	if (mi) {
940 
941 		Ref<ArrayMesh> mesh = mi->get_mesh();
942 
943 		if (mesh.is_valid() && !meshes.has(mesh)) {
944 			Spatial *s = mi;
945 			Transform transform;
946 			while (s) {
947 				transform = transform * s->get_transform();
948 				s = s->get_parent_spatial();
949 			}
950 
951 			meshes[mesh] = transform;
952 		}
953 	}
954 	for (int i = 0; i < p_node->get_child_count(); i++) {
955 
956 		_find_meshes(p_node->get_child(i), meshes);
957 	}
958 }
959 
_make_external_resources(Node * p_node,const String & p_base_path,bool p_make_animations,bool p_animations_as_text,bool p_keep_animations,bool p_make_materials,bool p_materials_as_text,bool p_keep_materials,bool p_make_meshes,bool p_meshes_as_text,Map<Ref<Animation>,Ref<Animation>> & p_animations,Map<Ref<Material>,Ref<Material>> & p_materials,Map<Ref<ArrayMesh>,Ref<ArrayMesh>> & p_meshes)960 void ResourceImporterScene::_make_external_resources(Node *p_node, const String &p_base_path, bool p_make_animations, bool p_animations_as_text, bool p_keep_animations, bool p_make_materials, bool p_materials_as_text, bool p_keep_materials, bool p_make_meshes, bool p_meshes_as_text, Map<Ref<Animation>, Ref<Animation> > &p_animations, Map<Ref<Material>, Ref<Material> > &p_materials, Map<Ref<ArrayMesh>, Ref<ArrayMesh> > &p_meshes) {
961 
962 	List<PropertyInfo> pi;
963 
964 	if (p_make_animations) {
965 		if (Object::cast_to<AnimationPlayer>(p_node)) {
966 			AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
967 
968 			List<StringName> anims;
969 			ap->get_animation_list(&anims);
970 			for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
971 
972 				Ref<Animation> anim = ap->get_animation(E->get());
973 				ERR_CONTINUE(anim.is_null());
974 
975 				if (!p_animations.has(anim)) {
976 					// Tracks from source file should be set as imported, anything else is a custom track.
977 					for (int i = 0; i < anim->get_track_count(); i++) {
978 						anim->track_set_imported(i, true);
979 					}
980 
981 					String ext_name;
982 
983 					if (p_animations_as_text) {
984 						ext_name = p_base_path.plus_file(_make_extname(E->get()) + ".tres");
985 					} else {
986 						ext_name = p_base_path.plus_file(_make_extname(E->get()) + ".anim");
987 					}
988 
989 					if (FileAccess::exists(ext_name) && p_keep_animations) {
990 						// Copy custom animation tracks from previously imported files.
991 						Ref<Animation> old_anim = ResourceLoader::load(ext_name, "Animation", true);
992 						if (old_anim.is_valid()) {
993 							for (int i = 0; i < old_anim->get_track_count(); i++) {
994 								if (!old_anim->track_is_imported(i)) {
995 									old_anim->copy_track(i, anim);
996 								}
997 							}
998 							anim->set_loop(old_anim->has_loop());
999 						}
1000 					}
1001 
1002 					anim->set_path(ext_name, true); // Set path to save externally.
1003 					ResourceSaver::save(ext_name, anim, ResourceSaver::FLAG_CHANGE_PATH);
1004 					p_animations[anim] = anim;
1005 				}
1006 			}
1007 		}
1008 	}
1009 
1010 	p_node->get_property_list(&pi);
1011 
1012 	for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
1013 
1014 		if (E->get().type == Variant::OBJECT) {
1015 
1016 			Ref<Material> mat = p_node->get(E->get().name);
1017 
1018 			if (p_make_materials && mat.is_valid() && mat->get_name() != "") {
1019 
1020 				if (!p_materials.has(mat)) {
1021 
1022 					String ext_name;
1023 
1024 					if (p_materials_as_text) {
1025 						ext_name = p_base_path.plus_file(_make_extname(mat->get_name()) + ".tres");
1026 					} else {
1027 						ext_name = p_base_path.plus_file(_make_extname(mat->get_name()) + ".material");
1028 					}
1029 
1030 					if (p_keep_materials && FileAccess::exists(ext_name)) {
1031 						//if exists, use it
1032 						p_materials[mat] = ResourceLoader::load(ext_name);
1033 					} else {
1034 
1035 						ResourceSaver::save(ext_name, mat, ResourceSaver::FLAG_CHANGE_PATH);
1036 						p_materials[mat] = ResourceLoader::load(ext_name, "", true); // disable loading from the cache.
1037 					}
1038 				}
1039 
1040 				if (p_materials[mat] != mat) {
1041 
1042 					p_node->set(E->get().name, p_materials[mat]);
1043 				}
1044 			} else {
1045 
1046 				Ref<ArrayMesh> mesh = p_node->get(E->get().name);
1047 
1048 				if (mesh.is_valid()) {
1049 
1050 					bool mesh_just_added = false;
1051 
1052 					if (p_make_meshes) {
1053 
1054 						if (!p_meshes.has(mesh)) {
1055 
1056 							//meshes are always overwritten, keeping them is not practical
1057 							String ext_name;
1058 
1059 							if (p_meshes_as_text) {
1060 								ext_name = p_base_path.plus_file(_make_extname(mesh->get_name()) + ".tres");
1061 							} else {
1062 								ext_name = p_base_path.plus_file(_make_extname(mesh->get_name()) + ".mesh");
1063 							}
1064 
1065 							ResourceSaver::save(ext_name, mesh, ResourceSaver::FLAG_CHANGE_PATH);
1066 							p_meshes[mesh] = ResourceLoader::load(ext_name);
1067 							p_node->set(E->get().name, p_meshes[mesh]);
1068 							mesh_just_added = true;
1069 						}
1070 					}
1071 
1072 					if (p_make_materials) {
1073 
1074 						if (mesh_just_added || !p_meshes.has(mesh)) {
1075 
1076 							for (int i = 0; i < mesh->get_surface_count(); i++) {
1077 								mat = mesh->surface_get_material(i);
1078 
1079 								if (!mat.is_valid())
1080 									continue;
1081 								if (mat->get_name() == "")
1082 									continue;
1083 
1084 								if (!p_materials.has(mat)) {
1085 									String ext_name;
1086 
1087 									if (p_materials_as_text) {
1088 										ext_name = p_base_path.plus_file(_make_extname(mat->get_name()) + ".tres");
1089 									} else {
1090 										ext_name = p_base_path.plus_file(_make_extname(mat->get_name()) + ".material");
1091 									}
1092 
1093 									if (p_keep_materials && FileAccess::exists(ext_name)) {
1094 										//if exists, use it
1095 										p_materials[mat] = ResourceLoader::load(ext_name);
1096 									} else {
1097 
1098 										ResourceSaver::save(ext_name, mat, ResourceSaver::FLAG_CHANGE_PATH);
1099 										p_materials[mat] = ResourceLoader::load(ext_name, "", true); // disable loading from the cache.
1100 									}
1101 								}
1102 
1103 								if (p_materials[mat] != mat) {
1104 
1105 									mesh->surface_set_material(i, p_materials[mat]);
1106 
1107 									//re-save the mesh since a material is now assigned
1108 									if (p_make_meshes) {
1109 
1110 										String ext_name;
1111 
1112 										if (p_meshes_as_text) {
1113 											ext_name = p_base_path.plus_file(_make_extname(mesh->get_name()) + ".tres");
1114 										} else {
1115 											ext_name = p_base_path.plus_file(_make_extname(mesh->get_name()) + ".mesh");
1116 										}
1117 
1118 										ResourceSaver::save(ext_name, mesh, ResourceSaver::FLAG_CHANGE_PATH);
1119 										p_meshes[mesh] = ResourceLoader::load(ext_name);
1120 									}
1121 								}
1122 							}
1123 
1124 							if (!p_make_meshes) {
1125 								p_meshes[mesh] = Ref<ArrayMesh>(); //save it anyway, so it won't be checked again
1126 							}
1127 						}
1128 					}
1129 				}
1130 			}
1131 		}
1132 	}
1133 
1134 	for (int i = 0; i < p_node->get_child_count(); i++) {
1135 
1136 		_make_external_resources(p_node->get_child(i), p_base_path, p_make_animations, p_animations_as_text, p_keep_animations, p_make_materials, p_materials_as_text, p_keep_materials, p_make_meshes, p_meshes_as_text, p_animations, p_materials, p_meshes);
1137 	}
1138 }
1139 
get_import_options(List<ImportOption> * r_options,int p_preset) const1140 void ResourceImporterScene::get_import_options(List<ImportOption> *r_options, int p_preset) const {
1141 
1142 	r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/root_type", PROPERTY_HINT_TYPE_STRING, "Node"), "Spatial"));
1143 	r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/root_name"), "Scene Root"));
1144 
1145 	List<String> script_extentions;
1146 	ResourceLoader::get_recognized_extensions_for_type("Script", &script_extentions);
1147 
1148 	String script_ext_hint;
1149 
1150 	for (List<String>::Element *E = script_extentions.front(); E; E = E->next()) {
1151 		if (script_ext_hint != "")
1152 			script_ext_hint += ",";
1153 		script_ext_hint += "*." + E->get();
1154 	}
1155 
1156 	bool materials_out = p_preset == PRESET_SEPARATE_MATERIALS || p_preset == PRESET_SEPARATE_MESHES_AND_MATERIALS || p_preset == PRESET_MULTIPLE_SCENES_AND_MATERIALS || p_preset == PRESET_SEPARATE_MATERIALS_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS;
1157 	bool meshes_out = p_preset == PRESET_SEPARATE_MESHES || p_preset == PRESET_SEPARATE_MESHES_AND_MATERIALS || p_preset == PRESET_SEPARATE_MESHES_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS;
1158 	bool scenes_out = p_preset == PRESET_MULTIPLE_SCENES || p_preset == PRESET_MULTIPLE_SCENES_AND_MATERIALS;
1159 	bool animations_out = p_preset == PRESET_SEPARATE_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MATERIALS_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS;
1160 
1161 	r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "nodes/root_scale", PROPERTY_HINT_RANGE, "0.001,1000,0.001"), 1.0));
1162 	r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/custom_script", PROPERTY_HINT_FILE, script_ext_hint), ""));
1163 	r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "nodes/storage", PROPERTY_HINT_ENUM, "Single Scene,Instanced Sub-Scenes"), scenes_out ? 1 : 0));
1164 	r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "materials/location", PROPERTY_HINT_ENUM, "Node,Mesh"), (meshes_out || materials_out) ? 1 : 0));
1165 	r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "materials/storage", PROPERTY_HINT_ENUM, "Built-In,Files (.material),Files (.tres)", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), materials_out ? 1 : 0));
1166 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "materials/keep_on_reimport"), materials_out));
1167 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "meshes/compress"), true));
1168 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "meshes/ensure_tangents"), true));
1169 	r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "meshes/storage", PROPERTY_HINT_ENUM, "Built-In,Files (.mesh),Files (.tres)"), meshes_out ? 1 : 0));
1170 	r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "meshes/light_baking", PROPERTY_HINT_ENUM, "Disabled,Enable,Gen Lightmaps", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0));
1171 	r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "meshes/lightmap_texel_size", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 0.1));
1172 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "skins/use_named_skins"), true));
1173 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "external_files/store_in_subdir"), false));
1174 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), true));
1175 	r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "animation/fps", PROPERTY_HINT_RANGE, "1,120,1"), 15));
1176 	r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "animation/filter_script", PROPERTY_HINT_MULTILINE_TEXT), ""));
1177 	r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/storage", PROPERTY_HINT_ENUM, "Built-In,Files (.anim),Files (.tres)", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), animations_out));
1178 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/keep_custom_tracks"), animations_out));
1179 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/optimizer/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), true));
1180 	r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "animation/optimizer/max_linear_error"), 0.05));
1181 	r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "animation/optimizer/max_angular_error"), 0.01));
1182 	r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "animation/optimizer/max_angle"), 22));
1183 	r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/optimizer/remove_unused_tracks"), true));
1184 	r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/clips/amount", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0));
1185 	for (int i = 0; i < 256; i++) {
1186 		r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "animation/clip_" + itos(i + 1) + "/name"), ""));
1187 		r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/clip_" + itos(i + 1) + "/start_frame"), 0));
1188 		r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/clip_" + itos(i + 1) + "/end_frame"), 0));
1189 		r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/clip_" + itos(i + 1) + "/loops"), false));
1190 	}
1191 }
1192 
_replace_owner(Node * p_node,Node * p_scene,Node * p_new_owner)1193 void ResourceImporterScene::_replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner) {
1194 
1195 	if (p_node != p_new_owner && p_node->get_owner() == p_scene) {
1196 		p_node->set_owner(p_new_owner);
1197 	}
1198 
1199 	for (int i = 0; i < p_node->get_child_count(); i++) {
1200 		Node *n = p_node->get_child(i);
1201 		_replace_owner(n, p_scene, p_new_owner);
1202 	}
1203 }
1204 
import_scene_from_other_importer(EditorSceneImporter * p_exception,const String & p_path,uint32_t p_flags,int p_bake_fps)1205 Node *ResourceImporterScene::import_scene_from_other_importer(EditorSceneImporter *p_exception, const String &p_path, uint32_t p_flags, int p_bake_fps) {
1206 
1207 	Ref<EditorSceneImporter> importer;
1208 	String ext = p_path.get_extension().to_lower();
1209 
1210 	for (Set<Ref<EditorSceneImporter> >::Element *E = importers.front(); E; E = E->next()) {
1211 
1212 		if (E->get().ptr() == p_exception)
1213 			continue;
1214 		List<String> extensions;
1215 		E->get()->get_extensions(&extensions);
1216 
1217 		for (List<String>::Element *F = extensions.front(); F; F = F->next()) {
1218 
1219 			if (F->get().to_lower() == ext) {
1220 
1221 				importer = E->get();
1222 				break;
1223 			}
1224 		}
1225 
1226 		if (importer.is_valid())
1227 			break;
1228 	}
1229 
1230 	ERR_FAIL_COND_V(!importer.is_valid(), NULL);
1231 
1232 	List<String> missing;
1233 	Error err;
1234 	return importer->import_scene(p_path, p_flags, p_bake_fps, &missing, &err);
1235 }
1236 
import_animation_from_other_importer(EditorSceneImporter * p_exception,const String & p_path,uint32_t p_flags,int p_bake_fps)1237 Ref<Animation> ResourceImporterScene::import_animation_from_other_importer(EditorSceneImporter *p_exception, const String &p_path, uint32_t p_flags, int p_bake_fps) {
1238 
1239 	Ref<EditorSceneImporter> importer;
1240 	String ext = p_path.get_extension().to_lower();
1241 
1242 	for (Set<Ref<EditorSceneImporter> >::Element *E = importers.front(); E; E = E->next()) {
1243 
1244 		if (E->get().ptr() == p_exception)
1245 			continue;
1246 		List<String> extensions;
1247 		E->get()->get_extensions(&extensions);
1248 
1249 		for (List<String>::Element *F = extensions.front(); F; F = F->next()) {
1250 
1251 			if (F->get().to_lower() == ext) {
1252 
1253 				importer = E->get();
1254 				break;
1255 			}
1256 		}
1257 
1258 		if (importer.is_valid())
1259 			break;
1260 	}
1261 
1262 	ERR_FAIL_COND_V(!importer.is_valid(), NULL);
1263 
1264 	return importer->import_animation(p_path, p_flags, p_bake_fps);
1265 }
1266 
import(const String & p_source_file,const String & p_save_path,const Map<StringName,Variant> & p_options,List<String> * r_platform_variants,List<String> * r_gen_files,Variant * r_metadata)1267 Error ResourceImporterScene::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
1268 
1269 	const String &src_path = p_source_file;
1270 
1271 	Ref<EditorSceneImporter> importer;
1272 	String ext = src_path.get_extension().to_lower();
1273 
1274 	EditorProgress progress("import", TTR("Import Scene"), 104);
1275 	progress.step(TTR("Importing Scene..."), 0);
1276 
1277 	for (Set<Ref<EditorSceneImporter> >::Element *E = importers.front(); E; E = E->next()) {
1278 
1279 		List<String> extensions;
1280 		E->get()->get_extensions(&extensions);
1281 
1282 		for (List<String>::Element *F = extensions.front(); F; F = F->next()) {
1283 
1284 			if (F->get().to_lower() == ext) {
1285 
1286 				importer = E->get();
1287 				break;
1288 			}
1289 		}
1290 
1291 		if (importer.is_valid())
1292 			break;
1293 	}
1294 
1295 	ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_UNRECOGNIZED);
1296 
1297 	float fps = p_options["animation/fps"];
1298 
1299 	int import_flags = EditorSceneImporter::IMPORT_ANIMATION_DETECT_LOOP;
1300 	if (!bool(p_options["animation/optimizer/remove_unused_tracks"]))
1301 		import_flags |= EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS;
1302 
1303 	if (bool(p_options["animation/import"]))
1304 		import_flags |= EditorSceneImporter::IMPORT_ANIMATION;
1305 
1306 	if (int(p_options["meshes/compress"]))
1307 		import_flags |= EditorSceneImporter::IMPORT_USE_COMPRESSION;
1308 
1309 	if (bool(p_options["meshes/ensure_tangents"]))
1310 		import_flags |= EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS;
1311 
1312 	if (int(p_options["materials/location"]) == 0)
1313 		import_flags |= EditorSceneImporter::IMPORT_MATERIALS_IN_INSTANCES;
1314 
1315 	if (bool(p_options["skins/use_named_skins"]))
1316 		import_flags |= EditorSceneImporter::IMPORT_USE_NAMED_SKIN_BINDS;
1317 
1318 	Error err = OK;
1319 	List<String> missing_deps; // for now, not much will be done with this
1320 	Node *scene = importer->import_scene(src_path, import_flags, fps, &missing_deps, &err);
1321 	if (!scene || err != OK) {
1322 		return err;
1323 	}
1324 
1325 	String root_type = p_options["nodes/root_type"];
1326 	root_type = root_type.split(" ")[0]; // full root_type is "ClassName (filename.gd)" for a script global class.
1327 
1328 	Ref<Script> root_script = NULL;
1329 	if (ScriptServer::is_global_class(root_type)) {
1330 		root_script = ResourceLoader::load(ScriptServer::get_global_class_path(root_type));
1331 		root_type = ScriptServer::get_global_class_base(root_type);
1332 	}
1333 
1334 	if (root_type != "Spatial") {
1335 		Node *base_node = Object::cast_to<Node>(ClassDB::instance(root_type));
1336 
1337 		if (base_node) {
1338 
1339 			scene->replace_by(base_node);
1340 			memdelete(scene);
1341 			scene = base_node;
1342 		}
1343 	}
1344 
1345 	if (root_script.is_valid()) {
1346 		scene->set_script(Variant(root_script));
1347 	}
1348 
1349 	if (Object::cast_to<Spatial>(scene)) {
1350 		float root_scale = p_options["nodes/root_scale"];
1351 		Object::cast_to<Spatial>(scene)->scale(Vector3(root_scale, root_scale, root_scale));
1352 	}
1353 
1354 	if (p_options["nodes/root_name"] != "Scene Root")
1355 		scene->set_name(p_options["nodes/root_name"]);
1356 	else
1357 		scene->set_name(p_save_path.get_file().get_basename());
1358 
1359 	err = OK;
1360 
1361 	String animation_filter = String(p_options["animation/filter_script"]).strip_edges();
1362 
1363 	bool use_optimizer = p_options["animation/optimizer/enabled"];
1364 	float anim_optimizer_linerr = p_options["animation/optimizer/max_linear_error"];
1365 	float anim_optimizer_angerr = p_options["animation/optimizer/max_angular_error"];
1366 	float anim_optimizer_maxang = p_options["animation/optimizer/max_angle"];
1367 	int light_bake_mode = p_options["meshes/light_baking"];
1368 
1369 	Map<Ref<Mesh>, List<Ref<Shape> > > collision_map;
1370 
1371 	scene = _fix_node(scene, scene, collision_map, LightBakeMode(light_bake_mode));
1372 
1373 	if (use_optimizer) {
1374 		_optimize_animations(scene, anim_optimizer_linerr, anim_optimizer_angerr, anim_optimizer_maxang);
1375 	}
1376 
1377 	Array animation_clips;
1378 	{
1379 
1380 		int clip_count = p_options["animation/clips/amount"];
1381 
1382 		for (int i = 0; i < clip_count; i++) {
1383 			String name = p_options["animation/clip_" + itos(i + 1) + "/name"];
1384 			int from_frame = p_options["animation/clip_" + itos(i + 1) + "/start_frame"];
1385 			int end_frame = p_options["animation/clip_" + itos(i + 1) + "/end_frame"];
1386 			bool loop = p_options["animation/clip_" + itos(i + 1) + "/loops"];
1387 
1388 			animation_clips.push_back(name);
1389 			animation_clips.push_back(from_frame / fps);
1390 			animation_clips.push_back(end_frame / fps);
1391 			animation_clips.push_back(loop);
1392 		}
1393 	}
1394 	if (animation_clips.size()) {
1395 		_create_clips(scene, animation_clips, !bool(p_options["animation/optimizer/remove_unused_tracks"]));
1396 	}
1397 
1398 	if (animation_filter != "") {
1399 		_filter_tracks(scene, animation_filter);
1400 	}
1401 
1402 	bool external_animations = int(p_options["animation/storage"]) == 1 || int(p_options["animation/storage"]) == 2;
1403 	bool external_animations_as_text = int(p_options["animation/storage"]) == 2;
1404 	bool keep_custom_tracks = p_options["animation/keep_custom_tracks"];
1405 	bool external_materials = int(p_options["materials/storage"]) == 1 || int(p_options["materials/storage"]) == 2;
1406 	bool external_materials_as_text = int(p_options["materials/storage"]) == 2;
1407 	bool external_meshes = int(p_options["meshes/storage"]) == 1 || int(p_options["meshes/storage"]) == 2;
1408 	bool external_meshes_as_text = int(p_options["meshes/storage"]) == 2;
1409 	bool external_scenes = int(p_options["nodes/storage"]) == 1;
1410 
1411 	String base_path = p_source_file.get_base_dir();
1412 
1413 	if (external_animations || external_materials || external_meshes || external_scenes) {
1414 
1415 		if (bool(p_options["external_files/store_in_subdir"])) {
1416 			String subdir_name = p_source_file.get_file().get_basename();
1417 			DirAccess *da = DirAccess::open(base_path);
1418 			Error err2 = da->make_dir(subdir_name);
1419 			memdelete(da);
1420 			ERR_FAIL_COND_V_MSG(err2 != OK && err2 != ERR_ALREADY_EXISTS, err2, "Cannot make directory '" + subdir_name + "'.");
1421 			base_path = base_path.plus_file(subdir_name);
1422 		}
1423 	}
1424 
1425 	if (light_bake_mode == 2 /* || generate LOD */) {
1426 
1427 		Map<Ref<ArrayMesh>, Transform> meshes;
1428 		_find_meshes(scene, meshes);
1429 
1430 		if (light_bake_mode == 2) {
1431 
1432 			float texel_size = p_options["meshes/lightmap_texel_size"];
1433 			texel_size = MAX(0.001, texel_size);
1434 
1435 			EditorProgress progress2("gen_lightmaps", TTR("Generating Lightmaps"), meshes.size());
1436 			int step = 0;
1437 			for (Map<Ref<ArrayMesh>, Transform>::Element *E = meshes.front(); E; E = E->next()) {
1438 
1439 				Ref<ArrayMesh> mesh = E->key();
1440 				String name = mesh->get_name();
1441 				if (name == "") { //should not happen but..
1442 					name = "Mesh " + itos(step);
1443 				}
1444 
1445 				progress2.step(TTR("Generating for Mesh: ") + name + " (" + itos(step) + "/" + itos(meshes.size()) + ")", step);
1446 
1447 				Error err2 = mesh->lightmap_unwrap(E->get(), texel_size);
1448 				if (err2 != OK) {
1449 					EditorNode::add_io_error("Mesh '" + name + "' failed lightmap generation. Please fix geometry.");
1450 				}
1451 				step++;
1452 			}
1453 		}
1454 	}
1455 
1456 	if (external_animations || external_materials || external_meshes) {
1457 		Map<Ref<Animation>, Ref<Animation> > anim_map;
1458 		Map<Ref<Material>, Ref<Material> > mat_map;
1459 		Map<Ref<ArrayMesh>, Ref<ArrayMesh> > mesh_map;
1460 
1461 		bool keep_materials = bool(p_options["materials/keep_on_reimport"]);
1462 
1463 		_make_external_resources(scene, base_path, external_animations, external_animations_as_text, keep_custom_tracks, external_materials, external_materials_as_text, keep_materials, external_meshes, external_meshes_as_text, anim_map, mat_map, mesh_map);
1464 	}
1465 
1466 	progress.step(TTR("Running Custom Script..."), 2);
1467 
1468 	String post_import_script_path = p_options["nodes/custom_script"];
1469 	Ref<EditorScenePostImport> post_import_script;
1470 
1471 	if (post_import_script_path != "") {
1472 		Ref<Script> scr = ResourceLoader::load(post_import_script_path);
1473 		if (!scr.is_valid()) {
1474 			EditorNode::add_io_error(TTR("Couldn't load post-import script:") + " " + post_import_script_path);
1475 		} else {
1476 
1477 			post_import_script = Ref<EditorScenePostImport>(memnew(EditorScenePostImport));
1478 			post_import_script->set_script(scr.get_ref_ptr());
1479 			if (!post_import_script->get_script_instance()) {
1480 				EditorNode::add_io_error(TTR("Invalid/broken script for post-import (check console):") + " " + post_import_script_path);
1481 				post_import_script.unref();
1482 				return ERR_CANT_CREATE;
1483 			}
1484 		}
1485 	}
1486 
1487 	if (post_import_script.is_valid()) {
1488 		post_import_script->init(base_path, p_source_file);
1489 		scene = post_import_script->post_import(scene);
1490 		if (!scene) {
1491 			EditorNode::add_io_error(
1492 					TTR("Error running post-import script:") + " " + post_import_script_path + "\n" +
1493 					TTR("Did you return a Node-derived object in the `post_import()` method?"));
1494 			return err;
1495 		}
1496 	}
1497 
1498 	progress.step(TTR("Saving..."), 104);
1499 
1500 	if (external_scenes) {
1501 		//save sub-scenes as instances!
1502 		for (int i = 0; i < scene->get_child_count(); i++) {
1503 			Node *child = scene->get_child(i);
1504 			if (child->get_owner() != scene)
1505 				continue; //not a real child probably created by scene type (ig, a scrollbar)
1506 			_replace_owner(child, scene, child);
1507 
1508 			String cn = String(child->get_name()).strip_edges().replace(".", "_").replace(":", "_");
1509 			if (cn == String()) {
1510 				cn = "ChildNode" + itos(i);
1511 			}
1512 			String path = base_path.plus_file(cn + ".scn");
1513 			child->set_filename(path);
1514 
1515 			Ref<PackedScene> packer = memnew(PackedScene);
1516 			packer->pack(child);
1517 			err = ResourceSaver::save(path, packer); //do not take over, let the changed files reload themselves
1518 			ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save scene to file '" + path + "'.");
1519 		}
1520 	}
1521 
1522 	Ref<PackedScene> packer = memnew(PackedScene);
1523 	packer->pack(scene);
1524 	print_verbose("Saving scene to: " + p_save_path + ".scn");
1525 	err = ResourceSaver::save(p_save_path + ".scn", packer); //do not take over, let the changed files reload themselves
1526 	ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save scene to file '" + p_save_path + ".scn'.");
1527 
1528 	memdelete(scene);
1529 
1530 	//this is not the time to reimport, wait until import process is done, import file is saved, etc.
1531 	//EditorNode::get_singleton()->reload_scene(p_source_file);
1532 
1533 	return OK;
1534 }
1535 
1536 ResourceImporterScene *ResourceImporterScene::singleton = NULL;
1537 
ResourceImporterScene()1538 ResourceImporterScene::ResourceImporterScene() {
1539 	singleton = this;
1540 }
1541 ///////////////////////////////////////
1542 
get_import_flags() const1543 uint32_t EditorSceneImporterESCN::get_import_flags() const {
1544 	return IMPORT_SCENE;
1545 }
get_extensions(List<String> * r_extensions) const1546 void EditorSceneImporterESCN::get_extensions(List<String> *r_extensions) const {
1547 	r_extensions->push_back("escn");
1548 }
import_scene(const String & p_path,uint32_t p_flags,int p_bake_fps,List<String> * r_missing_deps,Error * r_err)1549 Node *EditorSceneImporterESCN::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) {
1550 
1551 	Error error;
1552 	Ref<PackedScene> ps = ResourceFormatLoaderText::singleton->load(p_path, p_path, &error);
1553 	ERR_FAIL_COND_V_MSG(!ps.is_valid(), NULL, "Cannot load scene as text resource from path '" + p_path + "'.");
1554 
1555 	Node *scene = ps->instance();
1556 	ERR_FAIL_COND_V(!scene, NULL);
1557 
1558 	return scene;
1559 }
import_animation(const String & p_path,uint32_t p_flags,int p_bake_fps)1560 Ref<Animation> EditorSceneImporterESCN::import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps) {
1561 	ERR_FAIL_V(Ref<Animation>());
1562 }
1563