Batch translate Solidworks Headlessly via Python

Hey,
I just worked up a proof of concept. When I run this with the -ExecutePythonScript flag, I get a single FBX exported for my assembly.

Hopefully this looks pretty straightforward. Let me know if you have questions.


import unreal

file_to_import = "D:\\assets\\CAD\\Mage-Bike\\Clutch assembly.SLDASM"
final_fbx_file = "D:\\my_filename.fbx"

# clear anything existing in the level.
all_actors = unreal.EditorLevelLibrary.get_all_level_actors()

for a in all_actors:
    unreal.EditorLevelLibrary.destroy_actor(a)

# Construct the Datasmith Scene from a file on disk.
ds_scene_in_memory = unreal.DatasmithCADSceneElement.construct_datasmith_scene_from_file(file_to_import, "/Game/MyCADScene")

print("constructed the scene")

if ds_scene_in_memory is None:
    print "Scene loading failed."
    quit()

# Set import options.
import_options = ds_scene_in_memory.get_import_options()
tessellation_options = import_options.tessellation_options
tessellation_options.chord_tolerance = 15
tessellation_options.max_edge_length = 40
tessellation_options.normal_tolerance = 45
base_options = import_options.base_options
base_options.scene_handling = unreal.DatasmithImportScene.CURRENT_LEVEL

# Finalize the process by creating assets and actors.
ds_scene_in_memory.import_scene()

# Clean up the Datasmith Scene.
ds_scene_in_memory.destroy_scene()

print("Import complete!")

# merge the actors into one object
all_actors = unreal.EditorLevelLibrary.get_all_level_actors()
merge_options = unreal.EditorScriptingMergeStaticMeshActorsOptions()
# look for the unreal.MeshMergingSettings class to see what options you can set in here
merge_options.base_package_name = "/Game/NEW_MESH"
new_mesh_actor = unreal.EditorLevelLibrary.merge_static_mesh_actors(all_actors, merge_options)

# load the merged asset
loaded_asset = unreal.EditorAssetLibrary.load_asset("/Game/NEW_MESH")

# set up the FBX export options
task = unreal.AssetExportTask()
task.object = loaded_asset      # the asset to export
task.filename = final_fbx_file        # the filename to export as
task.automated = True           # don't display the export options dialog
task.replace_identical = True   # always overwrite the output
task.options = unreal.FbxExportOption()

# export!
result = unreal.Exporter.run_asset_export_task(task)

print("Export complete!")
for error_msg in task.errors:
    unreal.log_error("{}".format(error_msg))


1 Like