Esempio n. 1
0
void ScriptCreateDialog::_path_changed(const String& p_path) {

	path_valid=false;
	String p =p_path;

	if (p=="")	 {

		path_error_label->set_text(TTR("Path is empty"));
		path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
		return;

	}

	p = Globals::get_singleton()->localize_path(p);
	if (!p.begins_with("res://")) {

		path_error_label->set_text(TTR("Path is not local"));
		path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
		return;
	}

	if (p.find("/") || p.find("\\")) {
		DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);

		if (d->change_dir(p.get_base_dir())!=OK) {

			path_error_label->set_text(TTR("Invalid base path"));
			path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
			memdelete(d);
			return;

		}
		memdelete(d);
	}

	FileAccess *f = FileAccess::create(FileAccess::ACCESS_RESOURCES);
	create_new=!f->file_exists(p);
	memdelete(f);

	String extension=p.extension();
	List<String> extensions;

	// get all possible extensions for script
	for (int l=0;l<language_menu->get_item_count();l++) {
		ScriptServer::get_language( l )->get_recognized_extensions(&extensions);
	}

	bool found=false;
	int index=0;
	for(List<String>::Element *E=extensions.front();E;E=E->next()) {
		if (E->get().nocasecmp_to(extension)==0) {
			language_menu->select(index); // change Language option by extension
			found=true;
			break;
		}
		index++;
	}

	if (!found) {
		path_error_label->set_text(TTR("Invalid extension"));
		path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
		return;
	}

	_update_controls();

	path_error_label->add_color_override("font_color",Color(0,1.0,0.8,0.8));

	path_valid=true;

}
Esempio n. 2
0
Error EditorSceneImportPlugin::import2(Node *scene, const String& p_dest_path, const Ref<ResourceImportMetadata>& p_from) {

	Error err=OK;
	Ref<ResourceImportMetadata> from=p_from;
	String src_path=EditorImportPlugin::expand_source_path(from->get_source_path(0));
	int animation_flags=p_from->get_option("animation_flags");
	int scene_flags = from->get_option("flags");

	EditorProgress progress("import","Import Scene",104);
	progress.step("Importing Scene..",2);


	bool merge = !bool(from->get_option("reimport"));
	from->set_source_md5(0,FileAccess::get_md5(src_path));
	from->set_editor(get_name());

	from->set_option("reimport",false);
	String target_res_path=p_dest_path.get_base_dir();

	Map<Ref<Mesh>,Ref<Shape> > collision_map;

	Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);

	Set< Ref<ImageTexture> > imagemap;

	scene=_fix_node(scene,scene,collision_map,scene_flags,imagemap);


	/// BEFORE ANYTHING, RUN SCRIPT

	progress.step("Running Custom Script..",2);

	String post_import_script_path = from->get_option("post_import_script");
	Ref<EditorScenePostImport>  post_import_script;

	if (post_import_script_path!="") {
		post_import_script_path = EditorImportPlugin::expand_source_path(post_import_script_path);
		Ref<Script> scr = ResourceLoader::load(post_import_script_path);
		if (!scr.is_valid()) {
			EditorNode::add_io_error("Couldn't load post-import script: '"+post_import_script_path);
		} else {

			post_import_script = Ref<EditorScenePostImport>( memnew( EditorScenePostImport ) );
			post_import_script->set_script(scr.get_ref_ptr());
			if (!post_import_script->get_script_instance()) {
				EditorNode::add_io_error("Invalid/Broken Script for Post-Import: '"+post_import_script_path);
				post_import_script.unref();
			}
		}
	}


	if (post_import_script.is_valid()) {
		err = post_import_script->post_import(scene);
		if (err) {
			EditorNode::add_io_error("Error running Post-Import script: '"+post_import_script_path);
			return err;
		}
	}

	/// IMPORT IMAGES


	int idx=0;

	int image_format = from->get_option("texture_format");
	int image_flags =  from->get_option("texture_flags");
	float image_quality = from->get_option("texture_quality");

	for (Set< Ref<ImageTexture> >::Element *E=imagemap.front();E;E=E->next()) {

		//texture could be converted to something more useful for 3D, that could load individual mipmaps and stuff
		//but not yet..

		Ref<ImageTexture> texture = E->get();

		ERR_CONTINUE(!texture.is_valid());

		String path = texture->get_path();
		String fname= path.get_file();
		String target_path = Globals::get_singleton()->localize_path(target_res_path.plus_file(fname));
		progress.step("Import Img: "+fname,3+(idx)*100/imagemap.size());

		idx++;

		if (path==target_path) {

			EditorNode::add_io_error("Can't import a file over itself: '"+target_path);
			continue;
		}

		if (!target_path.begins_with("res://")) {
			EditorNode::add_io_error("Couldn't localize path: '"+target_path+"' (already local)");
			continue;
		}


		{


			target_path=target_path.basename()+".tex";

			Ref<ResourceImportMetadata> imd = memnew( ResourceImportMetadata );
			print_line("flags: "+itos(image_flags));
			imd->set_option("flags",image_flags);
			imd->set_option("format",image_format);
			imd->set_option("quality",image_quality);
			imd->set_option("atlas",false);
			imd->add_source(EditorImportPlugin::validate_source_path(path));


			if (FileAccess::exists(target_path)) {

				 Ref<ResourceImportMetadata> rimdex = ResourceLoader::load_import_metadata(target_path);
				 if (rimdex.is_valid()) {
					//make sure the options are the same, otherwise re-import
					List<String> opts;
					imd->get_options(&opts);
					bool differ=false;
					for (List<String>::Element *E=opts.front();E;E=E->next()) {
						if (!(rimdex->get_option(E->get())==imd->get_option(E->get()))) {
							differ=true;
							break;
						}
					}

					if (!differ) {
						texture->set_path(target_path);
						continue; //already imported
					}
				}
			}

			Error err = EditorTextureImportPlugin::get_singleton(EditorTextureImportPlugin::MODE_TEXTURE_3D)->import(target_path,imd);

		}
	}


	/// BEFORE SAVING - MERGE


	if (merge) {

		progress.step("Merging..",103);

		FileAccess *fa = FileAccess::create(FileAccess::ACCESS_FILESYSTEM);
		if (fa->file_exists(p_dest_path)) {

			//try to merge

			Ref<PackedScene> s = ResourceLoader::load(p_dest_path);
			if (s.is_valid()) {

				Node *existing = s->instance(true);

				if (existing) {

					_merge_scenes(scene,existing);

					memdelete(scene);
					scene=existing;
				}
			}

		}

		memdelete(fa);
	}


	progress.step("Saving..",104);

	Ref<PackedScene> packer = memnew( PackedScene );
	packer->pack(scene);
	packer->set_path(p_dest_path);
	packer->set_import_metadata(from);

	print_line("SAVING TO: "+p_dest_path);
	err = ResourceSaver::save(p_dest_path,packer);

	//EditorFileSystem::get_singleton()->update_resource(packer);

	memdelete(scene);
	/*
	scene->set_filename(p_dest_path);
	if (r_scene) {
		*r_scene=scene;
	} else {
		memdelete(scene);
	}

	String sp;
	if (p_post_import.is_valid() && !p_post_import->get_script().is_null()) {
		Ref<Script> scr = p_post_import->get_script();
		if (scr.is_valid())
			sp=scr->get_path();
	}

	String op=_getrelpath(p_path,p_dest_path);

	*/


	return err;

}
Esempio n. 3
0
void ScriptCreateDialog::_path_changed(const String& p_path) {

	path_valid=false;
	String p =p_path;

	if (p=="")	 {

		path_error_label->set_text("Path is Empty");
		path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
		return;

	}

	p = Globals::get_singleton()->localize_path(p);
	if (!p.begins_with("res://")) {

		path_error_label->set_text("Path is not local");
		path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
		return;
	}

	if (p.find("/") || p.find("\\")) {
		DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);

		if (d->change_dir(p.get_base_dir())!=OK) {

			path_error_label->set_text("Base Path Invalid");
			path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
			memdelete(d);
			return;

		}
		memdelete(d);
	}



	FileAccess *f = FileAccess::create(FileAccess::ACCESS_RESOURCES);

	if (f->file_exists(p)) {

		path_error_label->set_text("File Exists");
		path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
		memdelete(f);
		return;
	}

	memdelete(f);

	String extension=p.extension();
	List<String> extensions;

	int l=language_menu->get_selected();
	ScriptServer::get_language( l )->get_recognized_extensions(&extensions);

	bool found=false;
	for(List<String>::Element *E=extensions.front();E;E=E->next()) {
		if (E->get().nocasecmp_to(extension)==0) {
			found=true;
			break;
		}
	}

	if (!found) {

		path_error_label->set_text("Invalid Extension");
		path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
		return;
	}


	path_error_label->set_text("Path is Valid");
	path_error_label->add_color_override("font_color",Color(0,1.0,0.8,0.8));

	path_valid=true;

}