framework/cg/blender/import_fcstd/import_hierarchy.py

50 lines
2 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.
import logging
import bpy
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def import_hierarchy(fc_obj, b_obj, scale):
"""FreeCAD object, Blender object, scene scale"""
obj_parent = fc_obj.getParentGeoFeatureGroup()
obj_child_name = None
while obj_parent:
if bpy.context.scene.objects.get(obj_parent.Label):
empty = bpy.data.objects[obj_parent.Label]
else:
bpy.ops.object.empty_add(
type='CUBE', radius=0.01, align='WORLD',
location=(0, 0, 0), rotation=(0, 0, 0))
empty = bpy.data.objects['Empty']
empty.name = obj_parent.Label
placement = obj_parent.Placement
empty.location = placement.Base.multiply(scale)
rm = empty.rotation_mode
if placement.Rotation.Angle:
empty.rotation_mode = 'QUATERNION'
q = (placement.Rotation.Q[3],)+placement.Rotation.Q[:3]
empty.rotation_quaternion = (q)
empty.rotation_mode = rm
if b_obj.parent:
bpy.data.objects[obj_child_name].parent = empty
else:
b_obj.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, b_obj.name)