"""
Blender Import/Export Handlers
This module contains handlers for importing and exporting assets in Blender.
Supports various file formats including OBJ, FBX, STL, PLY, DAE, and glTF.
"""
import bpy
import os
def import_reference(file_paths, collection_name):
"""Import reference meshes into a collection"""
try:
# Create or get collection
if collection_name not in bpy.data.collections:
collection = bpy.data.collections.new(collection_name)
bpy.context.scene.collection.children.link(collection)
collection_created = True
else:
collection = bpy.data.collections[collection_name]
collection_created = False
imported_objects = []
for file_path in file_paths:
ext = os.path.splitext(file_path)[1].lower()
# Import based on file extension
if ext == '.obj':
bpy.ops.import_scene.obj(filepath=file_path)
elif ext == '.fbx':
bpy.ops.import_scene.fbx(filepath=file_path)
elif ext == '.stl':
bpy.ops.import_mesh.stl(filepath=file_path)
elif ext == '.ply':
bpy.ops.import_mesh.ply(filepath=file_path)
elif ext == '.dae':
bpy.ops.wm.collada_import(filepath=file_path)
else:
continue
# Move imported objects to collection
for obj in bpy.context.selected_objects:
if obj.name not in imported_objects:
imported_objects.append(obj.name)
# Unlink from current collections
for coll in obj.users_collection:
coll.objects.unlink(obj)
# Link to target collection
collection.objects.link(obj)
return {
"success": True,
"imported_objects": imported_objects,
"collection_created": collection_created
}
except Exception as e:
return {"error": f"Failed to import reference: {str(e)}"}
def export_asset(object_names, file_path, file_format, apply_modifiers, scale, forward_axis, up_axis):
"""Export objects to various formats"""
try:
# Select objects to export
bpy.ops.object.select_all(action='DESELECT')
export_objects = []
for obj_name in object_names:
obj = bpy.data.objects.get(obj_name)
if obj:
obj.select_set(True)
export_objects.append(obj)
if not export_objects:
return {"error": "No valid objects found to export"}
# Convert axis strings to Blender format
axis_map = {
'X': 'X', '-X': 'NEGATIVE_X',
'Y': 'Y', '-Y': 'NEGATIVE_Y',
'Z': 'Z', '-Z': 'NEGATIVE_Z'
}
forward = axis_map.get(forward_axis, 'Y')
up = axis_map.get(up_axis, 'Z')
# Export based on format
if file_format == 'GLTF':
bpy.ops.export_scene.gltf(
filepath=file_path,
use_selection=True,
export_apply=apply_modifiers,
export_format='GLB' if file_path.endswith('.glb') else 'GLTF_SEPARATE'
)
elif file_format == 'FBX':
bpy.ops.export_scene.fbx(
filepath=file_path,
use_selection=True,
apply_scale_options='FBX_SCALE_ALL',
global_scale=scale,
axis_forward=forward,
axis_up=up,
use_mesh_modifiers=apply_modifiers
)
elif file_format == 'OBJ':
bpy.ops.export_scene.obj(
filepath=file_path,
use_selection=True,
global_scale=scale,
axis_forward=forward,
axis_up=up,
use_mesh_modifiers=apply_modifiers
)
elif file_format == 'STL':
bpy.ops.export_mesh.stl(
filepath=file_path,
use_selection=True,
global_scale=scale,
use_mesh_modifiers=apply_modifiers
)
else:
return {"error": f"Unsupported export format: {file_format}"}
# Get file size
file_size_mb = os.path.getsize(file_path) / (1024 * 1024)
return {
"success": True,
"file_path": file_path,
"file_size_mb": file_size_mb
}
except Exception as e:
return {"error": f"Failed to export asset: {str(e)}"}