2023-02-01 13:43:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# 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.
|
2023-06-24 13:54:49 +00:00
|
|
|
__version__ = '0.3'
|
2023-02-01 13:43:23 +00:00
|
|
|
|
|
|
|
from mathutils import Matrix, Vector, Quaternion
|
2023-06-24 13:54:49 +00:00
|
|
|
import bpy
|
2023-02-01 13:43:23 +00:00
|
|
|
|
|
|
|
|
2023-03-06 12:50:35 +00:00
|
|
|
def apply_transforms(obj, location=False, rotation=False, scale=False):
|
2023-06-24 13:54:49 +00:00
|
|
|
''' bake local object transforms '''
|
|
|
|
# original idea from https://github.com/machin3io/MACHIN3tools
|
|
|
|
# update database
|
|
|
|
bpy.context.view_layer.update()
|
|
|
|
|
2023-02-01 13:43:23 +00:00
|
|
|
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):
|
2023-09-03 21:39:50 +00:00
|
|
|
scene_scale_martix = Matrix()
|
2023-02-01 13:43:23 +00:00
|
|
|
for i in range(3):
|
2023-09-03 21:39:50 +00:00
|
|
|
scene_scale_martix[i][i] = scale[i]
|
|
|
|
return scene_scale_martix
|
2023-03-06 12:50:35 +00:00
|
|
|
|
|
|
|
if location and rotation and scale:
|
|
|
|
loc, rot, sca = obj.matrix_world.decompose()
|
2023-09-03 21:39:50 +00:00
|
|
|
mesh_martix = get_loc_matrix(loc) @ get_rot_matrix(
|
|
|
|
rot) @ get_sca_matrix(sca)
|
2023-03-06 12:50:35 +00:00
|
|
|
obj.data.transform(mesh_martix)
|
|
|
|
apply_matrix = get_loc_matrix(Vector.Fill(3, 0)) @ get_rot_matrix(Quaternion()) @ get_sca_matrix(Vector.Fill(3, 1))
|
|
|
|
obj.matrix_world = apply_matrix
|
|
|
|
else:
|
|
|
|
if location:
|
2023-09-03 21:39:50 +00:00
|
|
|
raise Exception(
|
|
|
|
'Location only applies with all transformations (rotate and scale) together!'
|
|
|
|
)
|
2023-03-06 12:50:35 +00:00
|
|
|
if rotation:
|
|
|
|
loc, rot, sca = obj.matrix_world.decompose()
|
|
|
|
mesh_martix = get_rot_matrix(rot)
|
|
|
|
obj.data.transform(mesh_martix)
|
|
|
|
apply_matrix = get_loc_matrix(loc) @ get_rot_matrix(Quaternion()) @ get_sca_matrix(sca)
|
|
|
|
obj.matrix_world = apply_matrix
|
|
|
|
|
|
|
|
if scale:
|
|
|
|
loc, rot, sca = obj.matrix_world.decompose()
|
|
|
|
mesh_martix = get_sca_matrix(sca)
|
|
|
|
obj.data.transform(mesh_martix)
|
|
|
|
apply_matrix = get_loc_matrix(loc) @ get_rot_matrix(rot) @ get_sca_matrix(Vector.Fill(3, 1))
|
|
|
|
obj.matrix_world = apply_matrix
|
2023-02-01 13:43:23 +00:00
|
|
|
|
|
|
|
obj.rotation_mode = 'XYZ'
|
2023-06-24 13:54:49 +00:00
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
def round_transforms(obj):
|
|
|
|
''' Geting location of object and round it. '''
|
|
|
|
for idx, axis in enumerate(obj.location[:]):
|
|
|
|
obj.location[idx] = round(axis, 5)
|
|
|
|
return obj.location
|