[Bf-extensions-cvs] [25d49a34] blender2.8: Multi-Objects: UV_OT_export_layout (80%)

Dalai Felinto noreply at git.blender.org
Thu Sep 6 15:24:29 CEST 2018


Commit: 25d49a3436164c3cf95c47ab77ffa3e0bc80ce67
Author: Dalai Felinto
Date:   Thu Sep 6 09:40:20 2018 -0300
Branches: blender2.8
https://developer.blender.org/rBA25d49a3436164c3cf95c47ab77ffa3e0bc80ce67

Multi-Objects: UV_OT_export_layout (80%)

"""
Apart from the 'export as PNG' all seems to be working ok.

'Export to PNG' also seems to work but doesn't look acceptable because the mesh
wireframe modifier is used  to draw UV wireframes (I was going to see if a curve
object would be usable to draw wireframes). Something in setting the renderer up
could also be wrong or missing.
"""

D3507 by @Al

Note from reviewer: I can't get this addon to show in the menus. But the syntax
seems correct, and it's pep8 friendly, so no harm in committing it so I can
ask other devs to see why the addon is never registering here.

Also, some changes I made include making it pep8 friendly and replace:
['foo', 'bar'] with {'foo', 'bar'}.

===================================================================

M	io_mesh_uv_layout/__init__.py
M	io_mesh_uv_layout/export_uv_eps.py
M	io_mesh_uv_layout/export_uv_png.py
M	io_mesh_uv_layout/export_uv_svg.py

===================================================================

diff --git a/io_mesh_uv_layout/__init__.py b/io_mesh_uv_layout/__init__.py
index 30dff949..e33dcb41 100644
--- a/io_mesh_uv_layout/__init__.py
+++ b/io_mesh_uv_layout/__init__.py
@@ -22,7 +22,7 @@ bl_info = {
     "name": "UV Layout",
     "author": "Campbell Barton, Matt Ebb",
     "version": (1, 1, 1),
-    "blender": (2, 75, 0),
+    "blender": (2, 80, 0),
     "location": "Image-Window > UVs > Export UV Layout",
     "description": "Export the UV layout as a 2D graphic",
     "warning": "",
@@ -114,7 +114,7 @@ class ExportUVLayout(bpy.types.Operator):
     @classmethod
     def poll(cls, context):
         obj = context.active_object
-        return (obj and obj.type == 'MESH' and obj.data.uv_textures)
+        return (obj and obj.type == 'MESH' and obj.data.uv_layers)
 
     def _space_image(self, context):
         space_data = context.space_data
@@ -138,27 +138,82 @@ class ExportUVLayout(bpy.types.Operator):
 
         return image_width, image_height
 
-    def _face_uv_iter(self, context, mesh, tessellated):
+    # Trying to be consistent with ED_object_get_active_image
+    # from uvedit_ops.c so that what is exported are the uvs
+    # that are seen in the UV Editor
+    #
+    # returns Image or None
+    def _get_active_texture(self, mat):
+        if mat is None or not mat.use_nodes:
+            return None
+
+        node = self._get_active_texture_nodetree(mat.node_tree)
+
+        if node is not None and node.bl_rna.identifier in {'ShaderNodeTexImage', 'ShaderNodeTexEnvironment'}:
+            return node.image
+
+        return None
+
+    # returns image node or None
+    def _get_active_texture_nodetree(self, node_tree):
+        active_tex_node = None
+        active_group = None
+        has_group = False
+        inactive_node = None
+
+        for node in node_tree.nodes:
+            if node.show_texture:
+                active_tex_node = node
+                if node.select:
+                    return node
+            elif inactive_node is None and node.bl_rna.identifier in {'ShaderNodeTexImage', 'ShaderNodeTexEnvironment'}:
+                inactive_node = node
+            elif node.bl_rna.identifier == 'ShaderNodeGroup':
+                if node.select:
+                    active_group = node
+                else:
+                    has_group = True
+
+        # Not found a selected show_texture node
+        # Try to find a selected show_texture node in the selected group
+        if active_group is not None:
+            node = self._get_active_texture_nodetree(active_group.node_tree)
+            if node is not None:
+                return node
+
+        if active_tex_node is not None:
+            return active_tex_node
+
+        if has_group:
+            for node in node_tree.nodes:
+                if node.bl_rna.identifier == 'ShaderNodeGroup':
+                    n = self._get_active_texture_nodetree(node.node_tree)
+                    if n is not None and (n.show_texture or inactive_node is None):
+                        return n
+
+        return None
+
+    def _face_uv_iter(self, context, material_slots, mesh):
         uv_layer = mesh.uv_layers.active.data
         polys = mesh.polygons
 
         if not self.export_all:
-            uv_tex = mesh.uv_textures.active.data
-            local_image = Ellipsis
+            local_image = None
 
             if context.tool_settings.show_uv_local_view:
                 space_data = self._space_image(context)
                 if space_data:
                     local_image = space_data.image
+                    has_active_texture = [
+                        self._get_active_texture(slot.material)
+                        is local_image for slot in material_slots]
 
             for i, p in enumerate(polys):
                 # context checks
-                if polys[i].select and local_image in {Ellipsis,
-                                                       uv_tex[i].image}:
+                if (polys[i].select and (local_image is None or has_active_texture[polys[i].material_index])):
                     start = p.loop_start
                     end = start + p.loop_total
-                    uvs = tuple((uv.uv[0], uv.uv[1])
-                                for uv in uv_layer[start:end])
+                    uvs = tuple((uv.uv[0], uv.uv[1]) for uv in uv_layer[start:end])
 
                     # just write what we see.
                     yield (i, uvs)
@@ -171,7 +226,6 @@ class ExportUVLayout(bpy.types.Operator):
                 yield (i, uvs)
 
     def execute(self, context):
-
         obj = context.active_object
         is_editmode = (obj.mode == 'EDIT')
         if is_editmode:
@@ -186,24 +240,36 @@ class ExportUVLayout(bpy.types.Operator):
 
         if mode == 'EPS':
             from . import export_uv_eps
-            func = export_uv_eps.write
+            exportUV = export_uv_eps.Export_UV_EPS()
         elif mode == 'PNG':
             from . import export_uv_png
-            func = export_uv_png.write
+            exportUV = export_uv_png.Export_UV_PNG()
         elif mode == 'SVG':
             from . import export_uv_svg
-            func = export_uv_svg.write
+            exportUV = export_uv_svg.Export_UV_SVG()
 
-        if self.modified:
-            mesh = obj.to_mesh(context.scene, True, 'PREVIEW')
-        else:
-            mesh = obj.data
+        obList = [ob for ob in context.selected_objects if ob.type == 'MESH']
+
+        for obj in obList:
+            obj.data.tag = False
 
-        func(fw, mesh, self.size[0], self.size[1], self.opacity,
-             lambda: self._face_uv_iter(context, mesh, self.tessellated))
+        exportUV.begin(fw, self.size, self.opacity)
 
-        if self.modified:
-            bpy.data.meshes.remove(mesh)
+        for obj in obList:
+            if (obj.data.tag):
+                continue
+
+            obj.data.tag = True
+
+            if self.modified:
+                mesh = obj.to_mesh(context.scene, True, 'PREVIEW')
+            else:
+                mesh = obj.data
+
+            exportUV.build(mesh, lambda: self._face_uv_iter(
+                                        context, obj.material_slots, mesh))
+
+        exportUV.end()
 
         if is_editmode:
             bpy.ops.object.mode_set(mode='EDIT', toggle=False)
@@ -242,5 +308,6 @@ def unregister():
     bpy.utils.unregister_module(__name__)
     bpy.types.IMAGE_MT_uvs.remove(menu_func)
 
+
 if __name__ == "__main__":
     register()
diff --git a/io_mesh_uv_layout/export_uv_eps.py b/io_mesh_uv_layout/export_uv_eps.py
index a15dc266..d00e998a 100644
--- a/io_mesh_uv_layout/export_uv_eps.py
+++ b/io_mesh_uv_layout/export_uv_eps.py
@@ -21,66 +21,75 @@
 import bpy
 
 
-def write(fw, mesh, image_width, image_height, opacity, face_iter_func):
-    fw("%!PS-Adobe-3.0 EPSF-3.0\n")
-    fw("%%%%Creator: Blender %s\n" % bpy.app.version_string)
-    fw("%%Pages: 1\n")
-    fw("%%Orientation: Portrait\n")
-    fw("%%%%BoundingBox: 0 0 %d %d\n" % (image_width, image_height))
-    fw("%%%%HiResBoundingBox: 0.0 0.0 %.4f %.4f\n" %
-       (image_width, image_height))
-    fw("%%EndComments\n")
-    fw("%%Page: 1 1\n")
-    fw("0 0 translate\n")
-    fw("1.0 1.0 scale\n")
-    fw("0 0 0 setrgbcolor\n")
-    fw("[] 0 setdash\n")
-    fw("1 setlinewidth\n")
-    fw("1 setlinejoin\n")
-    fw("1 setlinecap\n")
+class Export_UV_EPS:
+    def begin(self, fw, image_size, opacity):
 
-    polys = mesh.polygons
+        self.fw = fw
+        self.image_width = image_size[0]
+        self.image_height = image_size[1]
+        self.opacity = opacity
 
-    if opacity > 0.0:
-        for i, mat in enumerate(mesh.materials if mesh.materials else [None]):
-            fw("/DRAW_%d {" % i)
-            fw("gsave\n")
-            if mat:
-                color = tuple((1.0 - ((1.0 - c) * opacity))
-                              for c in mat.diffuse_color)
-            else:
-                color = 1.0, 1.0, 1.0
-            fw("%.3g %.3g %.3g setrgbcolor\n" % color)
-            fw("fill\n")
-            fw("grestore\n")
-            fw("0 setgray\n")
-            fw("} def\n")
+        fw("%!PS-Adobe-3.0 EPSF-3.0\n")
+        fw("%%%%Creator: Blender %s\n" % bpy.app.version_string)
+        fw("%%Pages: 1\n")
+        fw("%%Orientation: Portrait\n")
+        fw("%%%%BoundingBox: 0 0 %d %d\n" % (self.image_width, self.image_height))
+        fw("%%%%HiResBoundingBox: 0.0 0.0 %.4f %.4f\n" %
+           (self.image_width, self.image_height))
+        fw("%%EndComments\n")
+        fw("%%Page: 1 1\n")
+        fw("0 0 translate\n")
+        fw("1.0 1.0 scale\n")
+        fw("0 0 0 setrgbcolor\n")
+        fw("[] 0 setdash\n")
+        fw("1 setlinewidth\n")
+        fw("1 setlinejoin\n")
+        fw("1 setlinecap\n")
 
-        # fill
+    def build(self, mesh, face_iter_func):
+        polys = mesh.polygons
+
+        if self.opacity > 0.0:
+            for i, mat in enumerate(mesh.materials if mesh.materials else [None]):
+                self.fw("/DRAW_%d {" % i)
+                self.fw("gsave\n")
+                if mat:
+                    color = tuple((1.0 - ((1.0 - c) * self.opacity))
+                                  for c in mat.diffuse_color)
+                else:
+                    color = 1.0, 1.0, 1.0
+                self.fw("%.3g %.3g %.3g setrgbcolor\n" % color)
+                self.fw("fill\n")
+                self.fw("grestore\n")
+                self.fw("0 setgray\n")
+                self.fw("} def\n")
+
+            # fill
+            for i, uvs in face_iter_func():
+                self.fw("newpath\n")
+                for j, uv in enumerate(uvs):
+                    uv_scale = (uv[0] * self.image_width, uv[1] * self.image_height)
+                    if j == 0:
+                        self.fw("%.5f %.5f moveto\n" % uv_scale)
+                    else:
+                        self.fw("%.5f %.5f lineto\n" % uv_scale)
+
+                self.fw("closepath\n")
+                self.fw("DRAW_%d\n" % polys[i].material_index)
+
+        # stroke only
         for i, uvs in face_iter_func():
-            fw("newpath\n")
+            self.fw("newpath\n")
             for j, uv in enumerate(uvs):
-                uv_sc

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list