bim.assign_class to apply transform from parents and constraints

To avoid confusing situations when user would assign a class, move object around and save ifc file, then to realize that it was saved using just local transforms which can be completely different.
This commit is contained in:
Andrej730
2024-11-19 20:09:16 +05:00
parent 857dd3aa60
commit 3b6128b2df
2 changed files with 34 additions and 0 deletions
@@ -199,6 +199,16 @@ class AssignClass(bpy.types.Operator, tool.Ifc.Operator):
if obj.mode != "OBJECT":
self.report({"ERROR"}, "Object must be in OBJECT mode to assign class")
continue
# Clear any transform modifications.
if not tool.Blender.apply_transform_as_local(obj):
self.report(
{"ERROR"},
f"Object '{obj.name}' has parent/constraints with a shear transform that cannot be applied safely as a local transform. "
"Please apply parent/constraints manually and try again.",
)
continue
element = core.assign_class(
tool.Ifc,
tool.Collector,
+24
View File
@@ -1233,3 +1233,27 @@ class Blender(bonsai.core.tool.Blender):
system = bpy.context.preferences.system
system_scale = system.dpi * system.pixel_size
return (system_scale / default_scale) * size
@classmethod
def apply_transform_as_local(cls, obj: bpy.types.Object) -> bool:
"""Apply object transforms as local matrix, if possible.
Clear parent and constraints.
:return: `True` if transform was applied and `False`
if transform wasn't applied it's not possible due to a shear.
"""
if not obj.parent or not obj.constraints:
return True
matrix = obj.matrix_world.copy()
# Matrix has a shear, it cannot be represented as a local matrix
# based on rotation+translation+scale.
if not matrix.to_3x3().is_orthogonal_axis_vectors:
return False
obj.parent = None
obj.constraints.clear()
obj.matrix_world = matrix
return True