Browse Source

Fix GLTF exporting invalid meshes and attempting to export gizmo meshes

Aaron Franke 1 year ago
parent
commit
2d38c980ee
2 changed files with 21 additions and 6 deletions
  1. 10 0
      core/error/error_macros.h
  2. 11 6
      modules/gltf/gltf_document.cpp

+ 10 - 0
core/error/error_macros.h

@@ -730,6 +730,16 @@ void _err_flush_stdout();
 	} else                                                                                        \
 	} else                                                                                        \
 		((void)0)
 		((void)0)
 
 
+/**
+ * Warns about `m_msg` only when verbose mode is enabled.
+ */
+#define WARN_VERBOSE(m_msg)               \
+	{                                     \
+		if (is_print_verbose_enabled()) { \
+			WARN_PRINT(m_msg);            \
+		}                                 \
+	}
+
 // Print deprecated warning message macros.
 // Print deprecated warning message macros.
 
 
 /**
 /**

+ 11 - 6
modules/gltf/gltf_document.cpp

@@ -5413,14 +5413,13 @@ BoneAttachment3D *GLTFDocument::_generate_bone_attachment(Ref<GLTFState> p_state
 
 
 GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref<GLTFState> p_state, MeshInstance3D *p_mesh_instance) {
 GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref<GLTFState> p_state, MeshInstance3D *p_mesh_instance) {
 	ERR_FAIL_NULL_V(p_mesh_instance, -1);
 	ERR_FAIL_NULL_V(p_mesh_instance, -1);
-	if (p_mesh_instance->get_mesh().is_null()) {
-		return -1;
-	}
+	ERR_FAIL_COND_V_MSG(p_mesh_instance->get_mesh().is_null(), -1, "glTF: Tried to export a MeshInstance3D node named " + p_mesh_instance->get_name() + ", but it has no mesh. This node will be exported without a mesh.");
+	Ref<Mesh> mesh_resource = p_mesh_instance->get_mesh();
+	ERR_FAIL_COND_V_MSG(mesh_resource->get_surface_count() == 0, -1, "glTF: Tried to export a MeshInstance3D node named " + p_mesh_instance->get_name() + ", but its mesh has no surfaces. This node will be exported without a mesh.");
 
 
-	Ref<Mesh> import_mesh = p_mesh_instance->get_mesh();
-	Ref<ImporterMesh> current_mesh = _mesh_to_importer_mesh(import_mesh);
+	Ref<ImporterMesh> current_mesh = _mesh_to_importer_mesh(mesh_resource);
 	Vector<float> blend_weights;
 	Vector<float> blend_weights;
-	int32_t blend_count = import_mesh->get_blend_shape_count();
+	int32_t blend_count = mesh_resource->get_blend_shape_count();
 	blend_weights.resize(blend_count);
 	blend_weights.resize(blend_count);
 	for (int32_t blend_i = 0; blend_i < blend_count; blend_i++) {
 	for (int32_t blend_i = 0; blend_i < blend_count; blend_i++) {
 		blend_weights.write[blend_i] = 0.0f;
 		blend_weights.write[blend_i] = 0.0f;
@@ -5531,6 +5530,12 @@ void GLTFDocument::_convert_scene_node(Ref<GLTFState> p_state, Node *p_current,
 	if (retflag) {
 	if (retflag) {
 		return;
 		return;
 	}
 	}
+#ifdef TOOLS_ENABLED
+	if (Engine::get_singleton()->is_editor_hint() && p_gltf_root != -1 && p_current->get_owner() == nullptr) {
+		WARN_VERBOSE("glTF export warning: Node '" + p_current->get_name() + "' has no owner. This is likely a temporary node generated by a @tool script. This would not be saved when saving the Godot scene, therefore it will not be exported to glTF.");
+		return;
+	}
+#endif // TOOLS_ENABLED
 	Ref<GLTFNode> gltf_node;
 	Ref<GLTFNode> gltf_node;
 	gltf_node.instantiate();
 	gltf_node.instantiate();
 	gltf_node->set_name(_gen_unique_name(p_state, p_current->get_name()));
 	gltf_node->set_name(_gen_unique_name(p_state, p_current->get_name()));