Добавлен импорт иерархии объектов из FreeCAD в Blender
This commit is contained in:
parent
5208577d86
commit
b744fe30a0
6 changed files with 70 additions and 23 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -107,4 +107,7 @@ install_plugin_cad.sh
|
||||||
|
|
||||||
# emacs backup files
|
# emacs backup files
|
||||||
~*
|
~*
|
||||||
|
*~
|
||||||
*#
|
*#
|
||||||
|
.#*
|
||||||
|
\#*\#
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
# GNU General Public License for more details.
|
# GNU General Public License for more details.
|
||||||
|
__version__ = "0.2"
|
||||||
import time
|
import time
|
||||||
import FreeCAD
|
import FreeCAD
|
||||||
import logging
|
import logging
|
||||||
|
@ -22,6 +23,7 @@ import os
|
||||||
import bpy
|
import bpy
|
||||||
from bpy_extras.node_shader_utils import PrincipledBSDFWrapper
|
from bpy_extras.node_shader_utils import PrincipledBSDFWrapper
|
||||||
from import_fcstd.handler import FreeCAD_xml_handler
|
from import_fcstd.handler import FreeCAD_xml_handler
|
||||||
|
from import_fcstd.import_hierarchy import import_hierarchy
|
||||||
from import_fcstd.materials import set_fem_mat
|
from import_fcstd.materials import set_fem_mat
|
||||||
from import_fcstd.is_object_solid import is_object_solid
|
from import_fcstd.is_object_solid import is_object_solid
|
||||||
|
|
||||||
|
@ -236,11 +238,16 @@ def obj_importer(filename,
|
||||||
bobj.rotation_quaternion = (q)
|
bobj.rotation_quaternion = (q)
|
||||||
bobj.rotation_mode = m
|
bobj.rotation_mode = m
|
||||||
bobj.scale = (scale, scale, scale)
|
bobj.scale = (scale, scale, scale)
|
||||||
if obj.Name in guidata:
|
|
||||||
# !!!
|
if obj.Name in guidata:
|
||||||
# one material for the whole object
|
# !!!
|
||||||
for fem_mat in doc.Objects:
|
# one material for the whole object
|
||||||
set_fem_mat(obj, bobj, fem_mat)
|
for fem_mat in doc.Objects:
|
||||||
|
set_fem_mat(obj, bobj, fem_mat)
|
||||||
|
|
||||||
|
obj_parent = obj.getParentGeoFeatureGroup()
|
||||||
|
if obj_parent:
|
||||||
|
import_hierarchy(obj, bobj, scale)
|
||||||
|
|
||||||
fcstd_collection.objects.link(bobj)
|
fcstd_collection.objects.link(bobj)
|
||||||
if select:
|
if select:
|
||||||
|
|
50
cg/blender/import_fcstd/import_hierarchy.py
Normal file
50
cg/blender/import_fcstd/import_hierarchy.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
# -*- 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)
|
|
@ -24,10 +24,4 @@ def is_object_solid(obj):
|
||||||
if not hasattr(obj, 'Shape'):
|
if not hasattr(obj, 'Shape'):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not hasattr(obj.Shape, 'Solids'):
|
return obj.Shape.isClosed()
|
||||||
return False
|
|
||||||
|
|
||||||
if len(obj.Shape.Solids) == 0:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
|
@ -19,10 +19,13 @@ logging.basicConfig(level=logging.INFO)
|
||||||
def asset_setup(transforms=True, sharpness=True, shading=True):
|
def asset_setup(transforms=True, sharpness=True, shading=True):
|
||||||
""" asset setup pipeline """
|
""" asset setup pipeline """
|
||||||
for ob in bpy.context.scene.objects:
|
for ob in bpy.context.scene.objects:
|
||||||
#deselect all but just one object and make it active
|
if not ob.type == 'MESH':
|
||||||
|
continue
|
||||||
bpy.ops.object.select_all(action='DESELECT')
|
bpy.ops.object.select_all(action='DESELECT')
|
||||||
ob.select_set(state=True)
|
ob.select_set(state=True)
|
||||||
bpy.context.view_layer.objects.active = ob
|
bpy.context.view_layer.objects.active = ob
|
||||||
|
# apply scale
|
||||||
|
apply_transforms(ob, location=False, rotation=False, scale=True)
|
||||||
|
|
||||||
if transforms:
|
if transforms:
|
||||||
# remove doubles
|
# remove doubles
|
||||||
|
|
|
@ -37,16 +37,6 @@ def freecad_asset_pipeline(fcstd_path,
|
||||||
cleanup_orphan_data()
|
cleanup_orphan_data()
|
||||||
obj_importer(fcstd_path, tessellation)
|
obj_importer(fcstd_path, tessellation)
|
||||||
|
|
||||||
# apply scale to all objects
|
|
||||||
obs = bpy.context.selected_objects
|
|
||||||
for ob in obs:
|
|
||||||
bpy.ops.object.select_all(action='DESELECT')
|
|
||||||
ob.select_set(state=True)
|
|
||||||
bpy.context.view_layer.objects.active = ob
|
|
||||||
apply_transforms(ob, location=False, rotation=False, scale=True)
|
|
||||||
for ob in obs:
|
|
||||||
ob.select_set(state=True)
|
|
||||||
|
|
||||||
if json_path is not None:
|
if json_path is not None:
|
||||||
for point in os.listdir(json_path):
|
for point in os.listdir(json_path):
|
||||||
if point.endswith('.json'):
|
if point.endswith('.json'):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue