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

Code before:
			can_instantiate = true;
		}
	}

	if (can_instantiate) {
		Transform3D global_transform = Transform3D(Basis(), _get_instance_position(p_point));
		preview_node->set_global_transform(global_transform);
		return true;
	}

	if (editor->get_preview_material().is_valid()) {
		ObjectID new_object_id = _select_ray(p_point);

		// Get the corresponding GeometryInstance for new_object_id, and if it is not valid, reset the new_object_id
		if (new_object_id.is_valid()) {
			GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(ObjectDB::get_instance(new_object_id));
			if (!geometry_instance) {
				new_object_id = ObjectID();
			}
		}

		// Execute only if the new_object_id does not the preview_material_target
		if (editor->get_preview_material_target() != new_object_id) {
			// If we have an existing preview_material_target, reset its material
			if (editor->get_preview_material_target().is_valid()) {
				GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(ObjectDB::get_instance(editor->get_preview_material_target()));
				if (geometry_instance) {
					geometry_instance->set_material_override(editor->get_preview_reset_material());
				}
			}

			// If new_object_id is valid, save the original material as the preview_reset_material and assign the new preview_material
			if (new_object_id.is_valid()) {
				GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(ObjectDB::get_instance(new_object_id));
				if (geometry_instance) {
					editor->set_preview_reset_material(geometry_instance->get_material_override());
					geometry_instance->set_material_override(editor->get_preview_material());
				}
			}

			// Save the new preview_material_target
			editor->set_preview_material_target(new_object_id);
		}
		return true;
	}

	return false;
}

void Node3DEditorViewport::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
	if (!can_drop_data_fw(p_point, p_data, p_from)) {

Review comment (bug):
Same thing, this is in the wrong callback. It should only be set when the drop action is actually confirmed (`drop_data_fw` I believe).

Code after:
	}

	return true;
}

void Node3DEditorViewport::_perform_drop_data() {
	if (spatial_editor->get_preview_material_target().is_valid()) {
		GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(ObjectDB::get_instance(spatial_editor->get_preview_material_target()));
		MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(ObjectDB::get_instance(spatial_editor->get_preview_material_target()));
		if (mesh_instance && spatial_editor->get_preview_material_surface() != -1) {
			editor_data->get_undo_redo().create_action(vformat(TTR("Set Surface %d Override Material"), spatial_editor->get_preview_material_surface()));
			editor_data->get_undo_redo().add_do_method(geometry_instance, "set_surface_override_material", spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_material());
			editor_data->get_undo_redo().add_undo_method(geometry_instance, "set_surface_override_material", spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_reset_material());
			editor_data->get_undo_redo().commit_action();
		} else if (geometry_instance) {
			editor_data->get_undo_redo().create_action(TTR("Set Material Override"));
			editor_data->get_undo_redo().add_do_method(geometry_instance, "set_material_override", spatial_editor->get_preview_material());
			editor_data->get_undo_redo().add_undo_method(geometry_instance, "set_material_override", spatial_editor->get_preview_reset_material());
			editor_data->get_undo_redo().commit_action();
		}

		_remove_preview_material();
		return;
	}

	_remove_preview_node();

	Vector<String> error_files;

	editor_data->get_undo_redo().create_action(TTR("Create Node"));

	for (int i = 0; i < selected_files.size(); i++) {
		String path = selected_files[i];
		Ref<Resource> res = ResourceLoader::load(path);
		if (res.is_null()) {
			continue;
		}
		Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
		Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
		if (mesh != nullptr || scene != nullptr) {
			bool success = _create_instance(target_node, path, drop_pos);
			if (!success) {
				error_files.push_back(path);
			}
		}
	}

	editor_data->get_undo_redo().commit_action();

	if (error_files.size() > 0) {
		String files_str;