-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.py
60 lines (52 loc) · 2.09 KB
/
import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import bpy
import sys
import os
import math
try:
# Get the input STL file path and output Blender file path from command line arguments
input_stl_path = sys.argv[-2]
output_blend_path = sys.argv[-1]
# Check if the output Blender file already exists
if os.path.exists(output_blend_path):
# Load the existing Blender file
bpy.ops.wm.open_mainfile(filepath=output_blend_path)
print("Add mesh to EXISTING file")
else:
# Clear the default cube (if it exists)
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
print("DELETE objects for new file")
# Import the STL file
bpy.ops.wm.stl_import(filepath=input_stl_path)
# Select the imported object and center it
imported_obj = bpy.context.selected_objects[0]
bpy.ops.object.select_all(action='DESELECT')
imported_obj.select_set(True)
bpy.context.view_layer.objects.active = imported_obj
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
# Set the remesh voxel size to 1mm
imported_obj.data.remesh_voxel_size = 0.001
# Create a camera if it doesn't exist
if not bpy.context.scene.camera:
bpy.ops.object.camera_add()
camera = bpy.context.active_object
bpy.context.scene.camera = camera
else:
camera = bpy.context.scene.camera
# Position the camera to view the imported object
bound_box = imported_obj.bound_box
center = imported_obj.location
size = max((max(v[i] for v in bound_box) - min(v[i] for v in bound_box)) for i in range(3))
camera.location = (center.x, center.y - size * 2, center.z + size)
camera.rotation_euler = (math.radians(60), 0, 0)
# Save the blend file
bpy.ops.wm.save_as_mainfile(filepath=output_blend_path)
#
print(f"STL imported from: {input_stl_path}")
print(f"Blender file saved to: {output_blend_path}")
except Exception as e:
print(f"An error occurred: {str(e)}")
# Optionally, you can print more detailed error information:
import traceback
print("Detailed error information:")
print(traceback.format_exc())