1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Export wrappers and integration with external tools.
8 from bpy
.app
.translations
import (
10 pgettext_data
as data_
,
15 from bpy_extras
import node_shader_utils
18 mat_wrap
= node_shader_utils
.PrincipledBSDFWrapper(mat
)
19 base_color_tex
= mat_wrap
.base_color_texture
20 if base_color_tex
and base_color_tex
.image
:
21 return base_color_tex
.image
24 def image_copy_guess(filepath
, objects
):
25 # 'filepath' is the path we are writing to.
30 for slot
in obj
.material_slots
:
32 mats
.add(slot
.material
)
35 image
= image_get(mat
)
43 imagepath
= bpy
.path
.abspath(image
.filepath
, library
=image
.library
)
44 if os
.path
.exists(imagepath
):
45 filepath_noext
= os
.path
.splitext(filepath
)[0]
46 ext
= os
.path
.splitext(imagepath
)[1]
48 imagepath_dst
= filepath_noext
+ ext
49 print(f
"copying texture: {imagepath!r} -> {imagepath_dst!r}")
52 shutil
.copy(imagepath
, imagepath_dst
)
58 def write_mesh(context
, report_cb
):
62 layer
= context
.view_layer
63 unit
= scene
.unit_settings
64 print_3d
= scene
.print_3d
66 export_format
= print_3d
.export_format
67 global_scale
= unit
.scale_length
if (unit
.system
!= 'NONE' and print_3d
.use_apply_scale
) else 1.0
68 path_mode
= 'COPY' if print_3d
.use_export_texture
else 'AUTO'
69 export_path
= bpy
.path
.abspath(print_3d
.export_path
)
70 obj
= layer
.objects
.active
71 export_data_layers
= print_3d
.use_data_layers
73 # Create name 'export_path/blendname-objname'
74 # add the filename component
76 name
= os
.path
.basename(bpy
.data
.filepath
)
77 name
= os
.path
.splitext(name
)[0]
79 name
= data_("untitled")
83 name
+= "-" + re
.sub(r
'[\\/:*?"<>|]', "", obj
.name
)
85 # first ensure the path is created
87 # this can fail with strange errors,
88 # if the dir can't be made then we get an error later.
90 os
.makedirs(export_path
, exist_ok
=True)
95 filepath
= os
.path
.join(export_path
, name
)
97 # ensure addon is enabled
100 def addon_ensure(addon_id
):
101 # Enable the addon, dont change preferences.
102 _default_state
, loaded_state
= addon_utils
.check(addon_id
)
104 addon_utils
.enable(addon_id
, default_set
=False)
106 if export_format
== 'STL':
107 addon_ensure("io_mesh_stl")
108 filepath
= bpy
.path
.ensure_ext(filepath
, ".stl")
109 ret
= bpy
.ops
.export_mesh
.stl(
112 use_mesh_modifiers
=True,
114 global_scale
=global_scale
,
116 elif export_format
== 'PLY':
117 filepath
= bpy
.path
.ensure_ext(filepath
, ".ply")
118 ret
= bpy
.ops
.wm
.ply_export(
121 apply_modifiers
=True,
122 export_selected_objects
=True,
123 global_scale
=global_scale
,
124 export_normals
=export_data_layers
,
125 export_uv
=export_data_layers
,
126 export_colors
="SRGB" if export_data_layers
else "NONE",
128 elif export_format
== 'X3D':
129 addon_ensure("io_scene_x3d")
130 filepath
= bpy
.path
.ensure_ext(filepath
, ".x3d")
131 ret
= bpy
.ops
.export_scene
.x3d(
133 use_mesh_modifiers
=True,
135 global_scale
=global_scale
,
137 use_normals
=export_data_layers
,
139 elif export_format
== 'OBJ':
140 filepath
= bpy
.path
.ensure_ext(filepath
, ".obj")
141 ret
= bpy
.ops
.wm
.obj_export(
143 apply_modifiers
=True,
144 export_selected_objects
=True,
145 scaling_factor
=global_scale
,
147 export_normals
=export_data_layers
,
148 export_uv
=export_data_layers
,
149 export_materials
=export_data_layers
,
150 export_colors
=export_data_layers
,
155 # for formats that don't support images
156 if path_mode
== 'COPY' and export_format
in {'STL', 'PLY'}:
157 image_copy_guess(filepath
, context
.selected_objects
)
159 if 'FINISHED' in ret
:
160 if report_cb
is not None:
161 report_cb({'INFO'}, tip_("Exported: {!r}").format(filepath
))
165 if report_cb
is not None:
166 report_cb({'ERROR'}, "Export failed")