#  ***** BEGIN GPL LICENSE BLOCK *****
#
#   Copyright (C) 2024 Ilia Kurochkin <brothermechanic@yandex.com>
#
#   Created by Ilia Kurochkin (brothermechanic)
#   contact:    brothermechanic@yandex.com
#
#   This is free software;
#   you can redistribute it and/or modify
#   it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version 3
#   of the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, see <https://www.gnu.org/licenses/>.
#
#  ***** END GPL LICENSE BLOCK *****
#
# coding: utf-8
'''
DESCRIPTION.
Convert all deformers and modifiers of object to it's mesh.
'''

__version__ = '0.1'

import bpy


def mesh_to_mesh(obj):
    ''' Convert all deformers and modifiers of object to it's mesh. '''
    if obj and obj.type == 'MESH':
        deg = bpy.context.evaluated_depsgraph_get()
        eval_mesh = obj.evaluated_get(deg).data.copy()

        orig_name = obj.name
        obj.name = '{}_temp'.format(orig_name)
        converted_obj = bpy.data.objects.new(orig_name, eval_mesh)
        converted_obj.matrix_world = obj.matrix_world

        bpy.context.view_layer.update()
        converted_obj.matrix_world = obj.matrix_world.copy()

        obj.users_collection[0].objects.link(converted_obj)

        bpy.data.objects.remove(obj, do_unlink=True)

    return converted_obj
