-
Notifications
You must be signed in to change notification settings - Fork 2
/
Blender_PNG_from_X3D.py
161 lines (136 loc) · 5.39 KB
/
Blender_PNG_from_X3D.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os, sys, getopt
# It takes an X3D file and exports a preview PNG for it, an STL with the same model, and a preview PNG for the STL in the "NIH blue" color
def usage():
print ("")
print ("Blender_PNG_from_X3D.py: a Python script to be executed in Blender")
print ("")
print ("""Usage: blender --background --factory-startup --python Blender_PNG_from_X3D.py -- <X3D_file>""")
print ("")
try:
import bpy
except ImportError:
usage()
sys.exit(2)
# 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"
# open the file
if len(args) == 1:
Src_file = args[0]
input_name, ext = os.path.splitext(Src_file)
PNG_file = input_name + ".png"
X3D_file = input_name + ".x3d"
else:
print("\nError: You must specify a single X3D 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 " + Src_file + "\n")
bpy.ops.import_scene.x3d(filepath = Src_file)
# DELETE THE OTHER IMPORTED OBJECTS
print("Deleting the imported lamps, curves, cameras\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='CURVE')
bpy.ops.object.delete(use_global=False)
bpy.ops.object.select_by_type(extend=False, type='CAMERA')
bpy.ops.object.delete(use_global=False)
# Check to see if each mesh has "Col" vertex group;
# if so, then turn on vertex color
print("Determining if per-vertex coloring is needed")
bpy.ops.object.select_by_type(extend=False, type='MESH')
for mesh_object in bpy.context.selected_objects:
bpy.context.scene.objects.active = mesh_object
if len(mesh_object.data.vertex_colors.values()) > 0:
print(str(mesh_object) + " has vertex colors")
if len(mesh_object.data.materials.values()) == 0:
print(str(mesh_object) + " has no material; adding material and turning on vertex coloring")
mesh_object.data.materials.append(bpy.data.materials.new(mesh_object.name))
bpy.data.materials[mesh_object.name].use_vertex_color_paint = True
# else:
# print(str(mesh_object) + " has material(s); turning off vertex coloring")
# for mesh_object_material in mesh_object.data.materials.values():
# bpy.data.materials[mesh_object_material.name].use_vertex_color_paint = False
# print(str(mesh_object) + " has material " + str(mesh_object_material) + " ; turning off vertex coloring")
else:
for mesh_object_material in mesh_object.data.materials.values():
bpy.data.materials[mesh_object_material.name].use_vertex_color_paint = False
# print(str(mesh_object) + " has material " + str(mesh_object_material) + " ; turning off vertex coloring")
# CAMERA
print("Setting camera\n")
cam = bpy.data.cameras.new("TheCamera")
cam.clip_end = 1000000
bpycam = bpy.data.objects.new("TheCamera", cam)
bpy.context.scene.camera = bpycam
bpy.ops.object.select_by_type(extend=False, type='MESH')
bpy.ops.view3d.camera_to_view_selected()
# SET SCENE
print("Setting the background color\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
bpy.data.worlds['World'].light_settings.use_ambient_occlusion = True
bpy.data.worlds['World'].light_settings.gather_method = 'APPROXIMATE'
# SAVE PNG
print("Rendering " + PNG_file +"\n")
bpy.data.scenes['Scene'].render.resolution_x = 500
bpy.data.scenes['Scene'].render.resolution_y = 500
bpy.data.scenes['Scene'].render.resolution_percentage = 100
bpy.data.scenes['Scene'].render.filepath = PNG_file
bpy.ops.render.render(write_still=True)
# EXPORT X3D
print("Exporting " + X3D_file +"\n")
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)
# ------------------------------------
# NOW WE NEED TO MAKE AN STL OUT OF IT
# ------------------------------------
# RECOLOR TO NIH BLUE
mat = bpy.data.materials.new("NIH_blue")
mat.diffuse_color = (0.20,0.38,0.60)
mat.diffuse_shader = 'LAMBERT'
mat.diffuse_intensity = 1.0
mat.specular_color = (1,1,1)
mat.specular_shader = 'COOKTORR'
mat.specular_intensity = 0.5
mat.alpha = 1
mat.ambient = 1
for o in bpy.context.scene.objects:
if o.type == 'MESH':
o.data.materials.clear()
o.data.materials.append(mat)
# EXPORT STL
print("Exporting STL file\n")
bpy.ops.export_mesh.stl(filepath=input_name + "-mono.stl")
# SAVE MONOCHROME PNG
print("Rendering monochrome PNG\n")
bpy.data.scenes['Scene'].render.filepath = input_name + "-mono.png"
bpy.ops.render.render(write_still=True)
# SAVE OUT X3D
print("Exporting cleaned monochrome X3D file")
bpy.ops.export_scene.x3d(filepath=input_name + "-mono.x3d")
# SAVE .BLEND
print("Saving monochrome .blend file")
bpy.ops.wm.save_as_mainfile(filepath = input_name + "-mono.blend", check_existing = False)
# QUIT
print("Quitting Blender")
bpy.ops.wm.quit_blender()
sys.exit(0)