framework/cg/blender/import_fcstd/import_hierarchy.py

60 lines
2.1 KiB
Python

# -*- 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.
'''
DESCRIPTION.
Collecting all parents and reconstruct this hierarhy in bledner.
'''
__version__ = '0.2'
import logging
import bpy
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def placement(bobj, obj, scale):
''' blender object, freecad object, scale factor '''
bobj.location = obj.Placement.Base.multiply(scale)
m = bobj.rotation_mode
bobj.rotation_mode = 'QUATERNION'
if obj.Placement.Rotation.Angle:
# FreeCAD Quaternion is XYZW while Blender is WXYZ
q = (obj.Placement.Rotation.Q[3],)+obj.Placement.Rotation.Q[:3]
bobj.rotation_quaternion = (q)
bobj.rotation_mode = m
return bobj
def hierarchy(bobj, obj, scale):
''' blender object, freecad object, scale factor '''
obj_parent = obj.getParentGeoFeatureGroup()
obj_child_name = None
parents = []
while obj_parent:
if bpy.data.objects.get(obj_parent.Label):
empty = bpy.data.objects[obj_parent.Label]
else:
empty = bpy.data.objects.new(obj_parent.Label, None)
empty.empty_display_type = 'CUBE'
empty.empty_display_size = 0.01
placement(empty, obj_parent, scale)
parents.append(empty)
if not bobj.parent:
bobj.parent = empty
else:
bpy.data.objects[obj_child_name].parent = empty
obj_child_name = obj_parent.Label
obj_parent = obj_parent.getParentGeoFeatureGroup()
empty.select_set(False)
logger.debug('Add parent %s to object %s', empty.name, bobj.name)
return parents