1 # SPDX-License-Identifier: GPL-2.0-or-later
5 "author": "Campbell Barton, Matt Ebb",
8 "location": "Image-Window > UVs > Export UV Layout",
9 "description": "Export the UV layout as a 2D graphic",
11 "doc_url": "{BLENDER_MANUAL_URL}/addons/import_export/mesh_uv_layout.html",
12 "support": 'OFFICIAL',
13 "category": "Import-Export",
17 # @todo write the wiki page
21 if "export_uv_eps" in locals():
22 importlib
.reload(export_uv_eps
)
23 if "export_uv_png" in locals():
24 importlib
.reload(export_uv_png
)
25 if "export_uv_svg" in locals():
26 importlib
.reload(export_uv_svg
)
31 from bpy
.props
import (
40 class ExportUVLayout(bpy
.types
.Operator
):
41 """Export UV layout to file"""
43 bl_idname
= "uv.export_layout"
44 bl_label
= "Export UV Layout"
45 bl_options
= {'REGISTER', 'UNDO'}
47 filepath
: StringProperty(
50 export_all
: BoolProperty(
52 description
="Export all UVs in this mesh (not just visible ones)",
55 modified
: BoolProperty(
57 description
="Exports UVs from the modified mesh",
62 ('SVG', "Scalable Vector Graphic (.svg)",
63 "Export the UV layout to a vector SVG file"),
64 ('EPS', "Encapsulate PostScript (.eps)",
65 "Export the UV layout to a vector EPS file"),
66 ('PNG', "PNG Image (.png)",
67 "Export the UV layout to a bitmap image"),
70 description
="File format to export the UV layout to",
73 size
: IntVectorProperty(
77 description
="Dimensions of the exported file",
79 opacity
: FloatProperty(
83 description
="Set amount of opacity for exported UV layout",
85 # For the file-selector.
86 check_existing
: BoolProperty(
92 def poll(cls
, context
):
93 obj
= context
.active_object
94 return obj
is not None and obj
.type == 'MESH' and obj
.data
.uv_layers
96 def invoke(self
, context
, event
):
97 self
.size
= self
.get_image_size(context
)
98 self
.filepath
= self
.get_default_file_name(context
) + "." + self
.mode
.lower()
99 context
.window_manager
.fileselect_add(self
)
100 return {'RUNNING_MODAL'}
102 def get_default_file_name(self
, context
):
104 objects
= list(self
.iter_objects_to_export(context
))
105 name
= " ".join(sorted([obj
.name
for obj
in objects
[:AMOUNT
]]))
106 if len(objects
) > AMOUNT
:
110 def check(self
, context
):
111 if any(self
.filepath
.endswith(ext
) for ext
in (".png", ".eps", ".svg")):
112 self
.filepath
= self
.filepath
[:-4]
114 ext
= "." + self
.mode
.lower()
115 self
.filepath
= bpy
.path
.ensure_ext(self
.filepath
, ext
)
118 def execute(self
, context
):
119 obj
= context
.active_object
120 is_editmode
= (obj
.mode
== 'EDIT')
122 bpy
.ops
.object.mode_set(mode
='OBJECT', toggle
=False)
124 filepath
= self
.filepath
125 filepath
= bpy
.path
.ensure_ext(filepath
, "." + self
.mode
.lower())
127 meshes
= list(self
.iter_meshes_to_export(context
))
128 polygon_data
= list(self
.iter_polygon_data_to_draw(context
, meshes
))
129 different_colors
= set(color
for _
, color
in polygon_data
)
131 depsgraph
= context
.evaluated_depsgraph_get()
132 for obj
in self
.iter_objects_to_export(context
):
133 obj_eval
= obj
.evaluated_get(depsgraph
)
134 obj_eval
.to_mesh_clear()
136 export
= self
.get_exporter()
137 export(filepath
, polygon_data
, different_colors
, self
.size
[0], self
.size
[1], self
.opacity
)
140 bpy
.ops
.object.mode_set(mode
='EDIT', toggle
=False)
144 def iter_meshes_to_export(self
, context
):
145 depsgraph
= context
.evaluated_depsgraph_get()
146 for obj
in self
.iter_objects_to_export(context
):
148 yield obj
.evaluated_get(depsgraph
).to_mesh()
153 def iter_objects_to_export(context
):
154 for obj
in {*context
.selected_objects
, context
.active_object
}:
155 if obj
.type != 'MESH':
158 if mesh
.uv_layers
.active
is None:
163 def currently_image_image_editor(context
):
164 return isinstance(context
.space_data
, bpy
.types
.SpaceImageEditor
)
166 def get_currently_opened_image(self
, context
):
167 if not self
.currently_image_image_editor(context
):
169 return context
.space_data
.image
171 def get_image_size(self
, context
):
172 # fallback if not in image context
173 image_width
= self
.size
[0]
174 image_height
= self
.size
[1]
176 # get size of "active" image if some exist
177 image
= self
.get_currently_opened_image(context
)
178 if image
is not None:
179 width
, height
= image
.size
182 image_height
= height
184 return image_width
, image_height
186 def iter_polygon_data_to_draw(self
, context
, meshes
):
188 uv_layer
= mesh
.uv_layers
.active
.data
189 for polygon
in mesh
.polygons
:
190 if self
.export_all
or polygon
.select
:
191 start
= polygon
.loop_start
192 end
= start
+ polygon
.loop_total
193 uvs
= tuple(tuple(uv
.uv
) for uv
in uv_layer
[start
:end
])
194 yield (uvs
, self
.get_polygon_color(mesh
, polygon
))
197 def get_polygon_color(mesh
, polygon
, default
=(0.8, 0.8, 0.8)):
198 if polygon
.material_index
< len(mesh
.materials
):
199 material
= mesh
.materials
[polygon
.material_index
]
200 if material
is not None:
201 return tuple(material
.diffuse_color
)[:3]
204 def get_exporter(self
):
205 if self
.mode
== 'PNG':
206 from . import export_uv_png
207 return export_uv_png
.export
208 elif self
.mode
== 'EPS':
209 from . import export_uv_eps
210 return export_uv_eps
.export
211 elif self
.mode
== 'SVG':
212 from . import export_uv_svg
213 return export_uv_svg
.export
218 def menu_func(self
, context
):
219 self
.layout
.operator(ExportUVLayout
.bl_idname
)
223 bpy
.utils
.register_class(ExportUVLayout
)
224 bpy
.types
.IMAGE_MT_uvs
.append(menu_func
)
228 bpy
.utils
.unregister_class(ExportUVLayout
)
229 bpy
.types
.IMAGE_MT_uvs
.remove(menu_func
)
232 if __name__
== "__main__":