-
Notifications
You must be signed in to change notification settings - Fork 2
/
Blender_STL_cleanup.py
144 lines (121 loc) · 4.27 KB
/
Blender_STL_cleanup.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# For license and additional information about this code, please refer to https://github.com/niaid/3Dmodel_scripts/blob/master/README.md
# This script runs in Blender: https://www.blender.org/
import os, sys, getopt
def usage():
print ("")
print ("Blender_STL_cleanup.py: a Python script to be executed in Blender")
print ("")
print ("""Usage: blender --background --factory-startup --python Blender_STL_cleanup.py -- <WRL_file> <decimation_factor> <replace_input>""")
print ("")
try:
import bpy
except ImportError:
usage()
sys.exit(2)
#print(sys.argv)
# Parse the command line arguments
try:
opts, args = getopt.getopt(sys.argv[6:], "h", [ "help" ] )
except getopt.GetoptError as err:
print (str(err))
usage()
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
else:
assert False, "unhandled options"
#print(args)
# open the file
if len(args) == 1:
WRL_file = args[0]
DecimationFactor = 0.5
input_name, ext = os.path.splitext(WRL_file)
X3D_file = input_name + ".x3d"
STL_file = input_name + ".stl"
elif len(args) == 2:
WRL_file = args[0]
DecimationFactor = float(args[1])
input_name, ext = os.path.splitext(WRL_file)
X3D_file = input_name + ".x3d"
STL_file = input_name + ".stl"
elif len(args) == 3:
WRL_file = args[0]
DecimationFactor = float(args[1])
ReplaceInput = int(args[2])
input_name, ext = os.path.splitext(WRL_file)
X3D_file = input_name + ".x3d"
STL_file = input_name + ".stl"
else:
print("\nError: You must specify a single WRL file")
usage()
sys.exit(2)
os.chdir(".")
# CLEAN UP INITIAL VIEW
bpy.ops.object.select_by_type(extend=False, type='MESH')
bpy.ops.object.delete(use_global=False)
bpy.ops.object.select_by_type(extend=False, type='LAMP')
bpy.ops.object.delete(use_global=False)
# IMPORT THE MESH
print("Importing the VRML file\n")
bpy.ops.import_scene.x3d(filepath=WRL_file)
# CLEAN UP THE OTHER IMPORTED OBJECTS
print("Deleting the imported lamps and curves\n")
bpy.ops.object.select_by_type(extend=False, type='LAMP')
bpy.ops.object.delete(use_global=False)
bpy.ops.object.select_by_type(extend=False, type='CAMERA')
bpy.ops.object.delete(use_global=False)
bpy.ops.object.select_by_type(extend=False, type='CURVE')
bpy.ops.object.delete(use_global=False)
# JOIN THE SUB-MESHES
print("Joining the sub-meshes\n")
#bpy.ops.object.select_pattern(pattern="Shape*", case_sensitive=False, extend=True)
bpy.ops.object.select_by_type(type='MESH')
bpy.context.scene.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.join()
# REMOVE DOUBLED VERTICES AND ADJUST NORMALS
print("Removing doubled vertices")
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.remove_doubles()
#bpy.ops.mesh.normals_make_consistent()
bpy.ops.object.mode_set(mode='OBJECT')
# Decimate
#print("Decimating to ratio " + str(DecimationFactor) + "\n")
#bpy.ops.object.modifier_add(type="DECIMATE")
#bpy.context.object.modifiers['Decimate'].ratio = DecimationFactor
#bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Decimate")
# REMOVE DOUBLED VERTICES AGAIN
#print("Removing doubled vertices")
#bpy.ops.object.mode_set(mode='EDIT')
#bpy.ops.mesh.remove_doubles()
#bpy.ops.object.mode_set(mode='OBJECT')
# EXPORT STL
print("Exporting STL file\n")
bpy.ops.export_mesh.stl(filepath=STL_file)
# CENTER AND COLOR
print("Moving object to center\n")
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
bpy.ops.object.location_clear()
bpy.ops.object.visual_transform_apply()
print("Setting the background and camera\n")
bpy.data.worlds['World'].horizon_color[0]=1
bpy.data.worlds['World'].horizon_color[1]=1
bpy.data.worlds['World'].horizon_color[2]=1
sc = bpy.context.scene
if not sc.camera:
# Assumes there's a camera called "Camera" in data, but not an scene object around it
sc.camera = bpy.data.objects.new("Camera", bpy.data.cameras.get("Camera"))
bpy.ops.view3d.camera_to_view_selected()
bpy.data.cameras['Camera'].clip_end = 1000000
# SAVE OUT X3D
if ReplaceInput:
print("Exporting cleaned X3D file")
bpy.ops.export_scene.x3d(filepath=X3D_file)
# SAVE .BLEND
print("Saving .blend file")
bpy.ops.wm.save_as_mainfile(filepath=input_name + ".blend", check_existing = False)
# QUIT
print("Quitting Blender")
bpy.ops.wm.quit_blender()
#sys.exit(0)