framework/cg/blender/utils/apply_transforms.py

46 lines
1.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# original idea from https://github.com/machin3io/MACHIN3tools
# Copyright (C) 2023 Ilia Kurochkin <brothermechanic@gmail.com>
#
# This program 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.
__version__ = "0.1"
from mathutils import Matrix, Vector, Quaternion
def apply_transforms(obj):
""" bake local object transforms """
def get_loc_matrix(location):
return Matrix.Translation(location)
def get_rot_matrix(rotation):
return rotation.to_matrix().to_4x4()
def get_sca_matrix(scale):
scale_mx = Matrix()
for i in range(3):
scale_mx[i][i] = scale[i]
return scale_mx
mx = obj.matrix_world
loc, rot, sca = mx.decompose()
meshmx = get_rot_matrix(rot) @ get_sca_matrix(sca)
obj.data.transform(meshmx)
applymx = get_loc_matrix(loc) @ get_rot_matrix(Quaternion()) @ get_sca_matrix(Vector.Fill(3, 1))
obj.matrix_world = applymx
obj.rotation_mode = 'XYZ'