Repository: godotengine/godot
File: editor/plugins/mesh_instance_3d_editor_plugin.cpp (C++)

Code before:
			String path = mesh2->get_path();
			int srpos = path.find("::");
			if (srpos != -1) {
				String base = path.substr(0, srpos);
				if (ResourceLoader::get_resource_type(base) == "PackedScene") {
					if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
						err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it does not belong to the edited scene. Make it unique first."));
						err_dialog->popup_centered();
						return;
					}
				} else {
					if (FileAccess::exists(path + ".import")) {
						err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it belongs to another resource which was imported from another file type. Make it unique first."));
						err_dialog->popup_centered();
						return;
					}
				}
			} else {
				if (FileAccess::exists(path + ".import")) {
					err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it was imported from another file type. Make it unique first."));
					err_dialog->popup_centered();
					return;
				}
			}

			Ref<PrimitiveMesh> primitive_mesh = Object::cast_to<PrimitiveMesh>(*mesh2);
			if (primitive_mesh.is_valid()) {
				EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
				ur->create_action(TTR("Unwrap UV2"));
				ur->add_do_method(*primitive_mesh, "set_add_uv2", true);
				ur->add_undo_method(*primitive_mesh, "set_add_uv2", primitive_mesh->get_add_uv2());
				ur->commit_action();
			} else {
				Ref<ArrayMesh> array_mesh = Object::cast_to<ArrayMesh>(*mesh2);
				if (!array_mesh.is_valid()) {
					err_dialog->set_text(TTR("Contained Mesh is not of type ArrayMesh."));
					err_dialog->popup_centered();
					return;
				}

				// Preemptively evaluate common fail cases for lightmap unwrapping.
				{
					if (array_mesh->get_blend_shape_count() > 0) {
						err_dialog->set_text(TTR("Can't unwrap mesh with blend shapes."));
						err_dialog->popup_centered();
						return;
					}

					for (int i = 0; i < array_mesh->get_surface_count(); i++) {
						Mesh::PrimitiveType primitive = array_mesh->surface_get_primitive_type(i);


Review comment (suggestion):
I think this can just be this:
```suggestion
			Ref<PrimitiveMesh> primitive_mesh = mesh2;
```
The ref will only be valid if the cast is valid.

Code after:
			String path = mesh2->get_path();
			int srpos = path.find("::");
			if (srpos != -1) {
				String base = path.substr(0, srpos);
				if (ResourceLoader::get_resource_type(base) == "PackedScene") {
					if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
						err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it does not belong to the edited scene. Make it unique first."));
						err_dialog->popup_centered();
						return;
					}
				} else {
					if (FileAccess::exists(path + ".import")) {
						err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it belongs to another resource which was imported from another file type. Make it unique first."));
						err_dialog->popup_centered();
						return;
					}
				}
			} else {
				if (FileAccess::exists(path + ".import")) {
					err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it was imported from another file type. Make it unique first."));
					err_dialog->popup_centered();
					return;
				}
			}

			Ref<PrimitiveMesh> primitive_mesh = mesh2;
			if (primitive_mesh.is_valid()) {
				EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
				ur->create_action(TTR("Unwrap UV2"));
				ur->add_do_method(*primitive_mesh, "set_add_uv2", true);
				ur->add_undo_method(*primitive_mesh, "set_add_uv2", primitive_mesh->get_add_uv2());
				ur->commit_action();
			} else {
				Ref<ArrayMesh> array_mesh = mesh2;
				if (!array_mesh.is_valid()) {
					err_dialog->set_text(TTR("Contained Mesh is not of type ArrayMesh."));
					err_dialog->popup_centered();
					return;
				}

				// Preemptively evaluate common fail cases for lightmap unwrapping.
				{
					if (array_mesh->get_blend_shape_count() > 0) {
						err_dialog->set_text(TTR("Can't unwrap mesh with blend shapes."));
						err_dialog->popup_centered();
						return;
					}

					for (int i = 0; i < array_mesh->get_surface_count(); i++) {
						Mesh::PrimitiveType primitive = array_mesh->surface_get_primitive_type(i);