60 lines
2.4 KiB
Python
60 lines
2.4 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"""
|
|
|
|
if not bpy.data.collections.get('Import Hierarchy'):
|
|
hierarchy_collection = bpy.data.collections.new("Import Hierarchy")
|
|
bpy.context.scene.collection.children.link(hierarchy_collection)
|
|
bpy.context.view_layer.active_layer_collection = \
|
|
bpy.context.view_layer.layer_collection.children['Import Hierarchy']
|
|
else:
|
|
hierarchy_collection = bpy.data.collections['Import Hierarchy']
|
|
|
|
obj_parent = fc_obj.getParentGeoFeatureGroup()
|
|
obj_child_name = None
|
|
while obj_parent:
|
|
if hierarchy_collection.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 not b_obj.parent:
|
|
b_obj.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, b_obj.name)
|
|
|