Update for changes in Blender's API
[blender-addons.git] / io_mesh_uv_layout / __init__.py
blob30dff949f103a17466ecc85b847ededb3d53525d
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8-80 compliant>
21 bl_info = {
22 "name": "UV Layout",
23 "author": "Campbell Barton, Matt Ebb",
24 "version": (1, 1, 1),
25 "blender": (2, 75, 0),
26 "location": "Image-Window > UVs > Export UV Layout",
27 "description": "Export the UV layout as a 2D graphic",
28 "warning": "",
29 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Import-Export/UV_Layout",
30 "support": 'OFFICIAL',
31 "category": "Import-Export",
35 # @todo write the wiki page
37 if "bpy" in locals():
38 import importlib
39 if "export_uv_eps" in locals():
40 importlib.reload(export_uv_eps)
41 if "export_uv_png" in locals():
42 importlib.reload(export_uv_png)
43 if "export_uv_svg" in locals():
44 importlib.reload(export_uv_svg)
46 import bpy
48 from bpy.props import (
49 StringProperty,
50 BoolProperty,
51 EnumProperty,
52 IntVectorProperty,
53 FloatProperty,
57 class ExportUVLayout(bpy.types.Operator):
58 """Export UV layout to file"""
60 bl_idname = "uv.export_layout"
61 bl_label = "Export UV Layout"
62 bl_options = {'REGISTER', 'UNDO'}
64 filepath = StringProperty(
65 subtype='FILE_PATH',
67 check_existing = BoolProperty(
68 name="Check Existing",
69 description="Check and warn on overwriting existing files",
70 default=True,
71 options={'HIDDEN'},
73 export_all = BoolProperty(
74 name="All UVs",
75 description="Export all UVs in this mesh (not just visible ones)",
76 default=False,
78 modified = BoolProperty(
79 name="Modified",
80 description="Exports UVs from the modified mesh",
81 default=False,
83 mode = EnumProperty(
84 items=(('SVG', "Scalable Vector Graphic (.svg)",
85 "Export the UV layout to a vector SVG file"),
86 ('EPS', "Encapsulate PostScript (.eps)",
87 "Export the UV layout to a vector EPS file"),
88 ('PNG', "PNG Image (.png)",
89 "Export the UV layout to a bitmap image"),
91 name="Format",
92 description="File format to export the UV layout to",
93 default='PNG',
95 size = IntVectorProperty(
96 size=2,
97 default=(1024, 1024),
98 min=8, max=32768,
99 description="Dimensions of the exported file",
101 opacity = FloatProperty(
102 name="Fill Opacity",
103 min=0.0, max=1.0,
104 default=0.25,
105 description="Set amount of opacity for exported UV layout"
107 tessellated = BoolProperty(
108 name="Tessellated UVs",
109 description="Export tessellated UVs instead of polygons ones",
110 default=False,
111 options={'HIDDEN'}, # As not working currently :/
114 @classmethod
115 def poll(cls, context):
116 obj = context.active_object
117 return (obj and obj.type == 'MESH' and obj.data.uv_textures)
119 def _space_image(self, context):
120 space_data = context.space_data
121 if isinstance(space_data, bpy.types.SpaceImageEditor):
122 return space_data
123 else:
124 return None
126 def _image_size(self, context, default_width=1024, default_height=1024):
127 # fallback if not in image context.
128 image_width, image_height = default_width, default_height
130 space_data = self._space_image(context)
131 if space_data:
132 image = space_data.image
133 if image:
134 width, height = tuple(context.space_data.image.size)
135 # in case no data is found.
136 if width and height:
137 image_width, image_height = width, height
139 return image_width, image_height
141 def _face_uv_iter(self, context, mesh, tessellated):
142 uv_layer = mesh.uv_layers.active.data
143 polys = mesh.polygons
145 if not self.export_all:
146 uv_tex = mesh.uv_textures.active.data
147 local_image = Ellipsis
149 if context.tool_settings.show_uv_local_view:
150 space_data = self._space_image(context)
151 if space_data:
152 local_image = space_data.image
154 for i, p in enumerate(polys):
155 # context checks
156 if polys[i].select and local_image in {Ellipsis,
157 uv_tex[i].image}:
158 start = p.loop_start
159 end = start + p.loop_total
160 uvs = tuple((uv.uv[0], uv.uv[1])
161 for uv in uv_layer[start:end])
163 # just write what we see.
164 yield (i, uvs)
165 else:
166 # all, simple
167 for i, p in enumerate(polys):
168 start = p.loop_start
169 end = start + p.loop_total
170 uvs = tuple((uv.uv[0], uv.uv[1]) for uv in uv_layer[start:end])
171 yield (i, uvs)
173 def execute(self, context):
175 obj = context.active_object
176 is_editmode = (obj.mode == 'EDIT')
177 if is_editmode:
178 bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
180 mode = self.mode
182 filepath = self.filepath
183 filepath = bpy.path.ensure_ext(filepath, "." + mode.lower())
184 file = open(filepath, "w")
185 fw = file.write
187 if mode == 'EPS':
188 from . import export_uv_eps
189 func = export_uv_eps.write
190 elif mode == 'PNG':
191 from . import export_uv_png
192 func = export_uv_png.write
193 elif mode == 'SVG':
194 from . import export_uv_svg
195 func = export_uv_svg.write
197 if self.modified:
198 mesh = obj.to_mesh(context.scene, True, 'PREVIEW')
199 else:
200 mesh = obj.data
202 func(fw, mesh, self.size[0], self.size[1], self.opacity,
203 lambda: self._face_uv_iter(context, mesh, self.tessellated))
205 if self.modified:
206 bpy.data.meshes.remove(mesh)
208 if is_editmode:
209 bpy.ops.object.mode_set(mode='EDIT', toggle=False)
211 file.close()
213 return {'FINISHED'}
215 def check(self, context):
216 filepath = bpy.path.ensure_ext(self.filepath, "." + self.mode.lower())
217 if filepath != self.filepath:
218 self.filepath = filepath
219 return True
220 else:
221 return False
223 def invoke(self, context, event):
224 import os
225 self.size = self._image_size(context)
226 self.filepath = os.path.splitext(bpy.data.filepath)[0]
227 wm = context.window_manager
228 wm.fileselect_add(self)
229 return {'RUNNING_MODAL'}
232 def menu_func(self, context):
233 self.layout.operator(ExportUVLayout.bl_idname)
236 def register():
237 bpy.utils.register_module(__name__)
238 bpy.types.IMAGE_MT_uvs.append(menu_func)
241 def unregister():
242 bpy.utils.unregister_module(__name__)
243 bpy.types.IMAGE_MT_uvs.remove(menu_func)
245 if __name__ == "__main__":
246 register()