Browse Source

tools: fbx: only create .texture if source image exists

Daniele Bartolini 10 tháng trước cách đây
mục cha
commit
1a8121165b
2 tập tin đã thay đổi với 26 bổ sung17 xóa
  1. 1 0
      docs/changelog.rst
  2. 25 17
      tools/resource/mesh_resource_fbx.vala

+ 1 - 0
docs/changelog.rst

@@ -14,6 +14,7 @@ Changelog
 * Fixed an issue that caused large levels to load slowly.
 * Fixed an issue that prevented importing DCC data with funny extensions.
 * Fixed revealing resources while the Project Browser was hidden.
+* Fixed importing FBX files with references to non-existing textures.
 
 **Runtime**
 

+ 25 - 17
tools/resource/mesh_resource_fbx.vala

@@ -589,8 +589,7 @@ public class FBXImporter
 					textures_path = textures_file.get_path();
 				}
 
-				// Extract embedded texture data or copy external
-				// texture files into textures_path.
+				// Import textures.
 				for (size_t i = 0; i < scene.textures.data.length; ++i) {
 					unowned ufbx.Texture texture = scene.textures.data[i];
 
@@ -598,13 +597,12 @@ public class FBXImporter
 					GLib.File source_image_file  = GLib.File.new_for_path(source_image_filename);
 					string source_image_path     = source_image_file.get_path();
 
-					string texture_resource_filename = project.resource_filename(source_image_path);
-					string texture_resource_path     = ResourceId.normalize(texture_resource_filename);
-					string texture_resource_name     = ResourceId.name(texture_resource_path);
-
+					FileStream fs;
+					// Extract embedded texture data or copy external
+					// texture files into textures_path.
 					if (texture.content.data.length > 0) {
 						// Extract embedded PNG data.
-						FileStream fs = FileStream.open(source_image_path, "wb");
+						fs = FileStream.open(source_image_path, "wb");
 						if (fs == null) {
 							loge("Failed to open texture '%s'".printf(source_image_path));
 							return ImportResult.ERROR;
@@ -616,18 +614,28 @@ public class FBXImporter
 							return ImportResult.ERROR;
 						}
 					} else {
-						// Copy external texture file.
+						// TODO: Copy external texture file.
+						fs = null;
 					}
 
-					// Create .texture resource.
-					Guid texture_id = Guid.new_guid();
-					// FIXME: detect texture type.
-					TextureResource texture_resource = TextureResource.color_map(db
-						, texture_id
-						, texture_resource_name + ".png"
-						);
-					texture_resource.save(project, texture_resource_name);
-					imported_textures.set(texture, texture_resource_name);
+					// Only create .texture resource if source image exists.
+					if (fs == null) {
+						logw("'%s' references non-existing texture '%s'".printf(filename_i, (string)texture.name.data));
+					} else {
+						string texture_resource_filename = project.resource_filename(source_image_path);
+						string texture_resource_path     = ResourceId.normalize(texture_resource_filename);
+						string texture_resource_name     = ResourceId.name(texture_resource_path);
+
+						// Create .texture resource.
+						Guid texture_id = Guid.new_guid();
+						// FIXME: detect texture type.
+						TextureResource texture_resource = TextureResource.color_map(db
+							, texture_id
+							, texture_resource_name + ".png"
+							);
+						texture_resource.save(project, texture_resource_name);
+						imported_textures.set(texture, texture_resource_name);
+					}
 				}
 			}