framework/cg/pipeline/freecad_to_asset.py

105 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
"""
DESCRIPTION.
Convert and setup FreeCAD solid objects to 3d assets mesh files.
Support Blender compiled as a Python Module only!
"""
__version__ = "0.3"
import logging
import os
import sys
sys.path.append('../blender/')
from import_fcstd.import_cad_objects import obj_importer
from import_fcstd.import_coordinate_point import empty_importer
from utils.apply_transforms import apply_transforms
from utils.remove_collections import remove_collections
from utils.cleanup_orphan_data import cleanup_orphan_data
from utils.sdf_mesh_selector import sdf_mesh_selector
from remesh import asset_setup
from export.dae import export_dae
from export.collision import export_col_stl
import bpy
import mathutils
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def freecad_asset_pipeline(fcstd_path,
linear_deflection,
angular_deflection,
mesh_export_path=None,
json_path=None,
blend_path=None,
sdf_path=None):
""" Setup FreeCAD scene to CG asset """
# prepare blend file
remove_collections()
cleanup_orphan_data()
# import objects
obj_importer(fcstd_path, linear_deflection, angular_deflection)
# import lcs
if json_path is None:
json_path = os.path.dirname(fcstd_path)
for f in os.listdir(os.path.dirname(fcstd_path)):
if f.endswith('.json'):
json_file = os.path.join(json_path, f)
empty_importer(json_file)
# sdf setup WIP
if sdf_path is not None:
sdf_mesh_selector(sdf_path)
# retopo
asset_setup()
# save blender scene
if blend_path is not None:
if not os.path.isdir(os.path.dirname(blend_path)):
os.makedirs(os.path.dirname(blend_path))
bpy.ops.wm.save_as_mainfile(filepath=blend_path)
# export all objects
if mesh_export_path is not None:
obs = bpy.context.selected_objects
for ob in obs:
ob.matrix_world = mathutils.Matrix()
for ob in obs:
ob.select_set(state=True)
export_dae(mesh_export_path)
export_col_stl(mesh_export_path)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description='Convert and setup FreeCAD solid objects to 3d assets mesh files.')
parser.add_argument(
'--fcstd_path', type=str, help='Path to source FreeCAD scene', required=True)
parser.add_argument(
'--linear_deflection', type=float, help='Max linear distance error', default=0.1, required=False)
parser.add_argument(
'--angular_deflection', type=float, help='Max angular distance error', default=20.0, required=False)
parser.add_argument(
'--mesh_export_path', type=str, help='Path for export meshes', required=False)
parser.add_argument(
'--json_path', type=str, help='Path to DIR with coordinate points jsons', required=False)
parser.add_argument(
'--blend_path', type=str, help='Path for export blend assembly file', required=False)
parser.add_argument(
'--sdf_path', type=str, help='Path to source SDF assembly file', required=False)
args = parser.parse_args()
freecad_asset_pipeline(args.fcstd_path,
args.linear_deflection,
args.angular_deflection,
args.mesh_export_path,
args.json_path,
args.blend_path,
args.sdf_path)
logger.info("Assets setup finished without errors")