[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3995] contrib/py/scripts/addons/ uv_bake_texture_to_vcols.py: Addon: Bake UV tex to VCol layer improved, now handles multiple uv_texture images and allows to set a color for transparency .

Sebastian Nell codemanx at gmx.de
Thu Nov 22 10:49:45 CET 2012


Revision: 3995
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3995
Author:   codemanx
Date:     2012-11-22 09:49:41 +0000 (Thu, 22 Nov 2012)
Log Message:
-----------
Addon: Bake UV tex to VCol layer improved, now handles multiple uv_texture images and allows to set a color for transparency. I would call it feature complete.

Modified Paths:
--------------
    contrib/py/scripts/addons/uv_bake_texture_to_vcols.py

Modified: contrib/py/scripts/addons/uv_bake_texture_to_vcols.py
===================================================================
--- contrib/py/scripts/addons/uv_bake_texture_to_vcols.py	2012-11-21 19:28:06 UTC (rev 3994)
+++ contrib/py/scripts/addons/uv_bake_texture_to_vcols.py	2012-11-22 09:49:41 UTC (rev 3995)
@@ -16,23 +16,24 @@
 #
 # ##### END GPL LICENSE BLOCK #####
 
+# <pep8 compliant>
+
 """
 Bake UV-Texture to Vertex Colors Addon
 
 Contact:        p_boelens at msn.com
 Information:    http://projects.blender.org/tracker/index.php?func=detail&aid=28211
 
-Contributor(s): CoDEmanX.
+Contributor(s): Patrick Boelens, CoDEmanX.
     
 All rights reserved.
 """
 
 bl_info = {
     "name": "Bake UV-Texture to Vertex Colors",
-    "description": "Bakes the colors of the active UV Texture to Vertex Colors. "
-                   "Uses the active object and creates new VCol layer.",
+    "description": "Bakes the colors of the active UV Texture to a Vertex Color layer. ",
     "author": "Patrick Boelens, CoDEmanX",
-    "version": (0, 4),
+    "version": (0, 6),
     "blender": (2, 6, 3),
     "location": "3D View > Vertex Paint > Toolshelf > Bake",
     "warning": "Requires image texture, generated textures aren't supported.",
@@ -42,7 +43,7 @@
     "category": "UV"}
 
 import bpy
-from bpy.props import BoolProperty, EnumProperty
+from bpy.props import BoolProperty, EnumProperty, FloatVectorProperty
 from math import fabs
 from colorsys import rgb_to_hsv, hsv_to_rgb
 
@@ -51,11 +52,15 @@
     bl_label = "Bake UV-Texture to Vertex Colors"
     bl_description = "Bake active UV-Texture to new Vertex Color layer (requires image texture)"
     bl_options = {'REGISTER', 'UNDO'}
-       
+    
+    replace_active_layer = BoolProperty(name="Replace layer",
+                                        description="Overwrite active Vertex Color layer",
+                                        default=True)
+
     mappingModes = [("CLIP", "Clip", "Don't affect vertices who's UV-coordinates are out of bounds."),
-                 ("REPEAT", "Repeat", "Tile the image so that each vertex is accounted for."),
-                 ("EXTEND", "Extend", "Extends the edges of the image to the UV-coordinates.")
-                 ]
+                    ("REPEAT", "Repeat", "Tile the image so that each vertex is accounted for."),
+                    ("EXTEND", "Extend", "Extends the edges of the image to the UV-coordinates.")
+                   ]
     
     mappingMode = EnumProperty(items=mappingModes,
                                default="CLIP",
@@ -85,200 +90,228 @@
                                 name="Blend Type",
                                 description="The blending mode to use when baking")
     
-    
     mirror_x = BoolProperty(name="Mirror X", description="Mirror the image on the X-axis.")
     mirror_y = BoolProperty(name="Mirror Y", description="Mirror the image on the Y-axis.")
-    
+        
     @classmethod
     def poll(self, context):
         return (context.object and
                 context.object.type == 'MESH' and 
                 context.mode != 'EDIT_MESH' and
-                context.object.data.uv_textures.active.data[0].image)
+                context.object.data.uv_layers.active and
+                context.object.data.uv_textures.active)
 
     def execute(self, context):
         obdata = context.object.data
-        loop_count = len(obdata.loops)
-        uv_tex = obdata.uv_textures.active.data[0] # TODO: Add support for multiple images
-        image_size_x = uv_tex.image.size[0]
-        image_size_y = uv_tex.image.size[1]
+
+        if self.replace_active_layer and obdata.vertex_colors.active:
+            vertex_colors = obdata.vertex_colors.active
+        else:
+            vertex_colors = obdata.vertex_colors.new(name="Baked UV texture")
         
-        # Accessing this directly is far too slow. Copied to new array for massive performance-gain.
-        uv_pixels = uv_tex.image.pixels[:]
+            if not vertex_colors:
+                # Can't add more than 17 VCol layers
+                self.report({'ERROR'}, "Couldn't add another Vertex Color layer,\n"
+                                       "Please remove an existing layer or replace active.")
+                return {'CANCELLED'}
             
-        if uv_tex.image.name not in obdata.vertex_colors:
-            obdata.vertex_colors.new(name=uv_tex.image.name)
-            
-        vertex_colors = obdata.vertex_colors[uv_tex.image.name]
         obdata.vertex_colors.active = vertex_colors
         
-        for i in range(loop_count):
-            co = obdata.uv_layers.active.data[i].uv
-            x_co = round(co[0] * (image_size_x - 1))
-            y_co = round(co[1] * (image_size_y - 1))
+        uv_images = {}
+        for uv_tex in obdata.uv_textures.active.data:
+            if uv_tex.image and uv_tex.image.name not in uv_images and uv_tex.image.pixels:
+                
+                uv_images[uv_tex.image.name] = (uv_tex.image.size[0],
+                                                uv_tex.image.size[1],
+                                                uv_tex.image.pixels[:]
+                                                # Accessing pixels directly is far too slow.
+                                                # Copied to new array for massive performance-gain.
+                                               )
+        
+        for p in obdata.polygons:
+            img = obdata.uv_textures.active.data[p.index].image
+            if not img:
+                continue
             
-            if x_co < 0 or x_co >= image_size_x or y_co < 0 or y_co >= image_size_y:
-                if self.mappingMode == 'CLIP':
-                    continue
+            image_size_x, image_size_y, uv_pixels = uv_images[img.name]
+            
+            for loop in p.loop_indices:
+        
+                co = obdata.uv_layers.active.data[loop].uv
+                x_co = round(co[0] * (image_size_x - 1))
+                y_co = round(co[1] * (image_size_y - 1))
+                
+                if x_co < 0 or x_co >= image_size_x or y_co < 0 or y_co >= image_size_y:
+                    if self.mappingMode == 'CLIP':
+                        continue
 
-                elif self.mappingMode == 'REPEAT':
-                    x_co %= image_size_x
-                    y_co %= image_size_y
+                    elif self.mappingMode == 'REPEAT':
+                        x_co %= image_size_x
+                        y_co %= image_size_y
 
-                elif self.mappingMode == 'EXTEND':
-                    if x_co > image_size_x - 1:
-                        x_co = image_size_x - 1
-                    if x_co < 0:
-                        x_co = 0
-                    if y_co > image_size_y - 1:
-                        y_co = image_size_y - 1
-                    if y_co < 0:
-                        y_co = 0
-            
-            if self.mirror_x:
-                 x_co = image_size_x -1 - x_co
-                 
-            if self.mirror_y:
-                 y_co = image_size_y -1 - y_co
-                 
-            col_out = vertex_colors.data[i].color
+                    elif self.mappingMode == 'EXTEND':
+                        if x_co > image_size_x - 1:
+                            x_co = image_size_x - 1
+                        if x_co < 0:
+                            x_co = 0
+                        if y_co > image_size_y - 1:
+                            y_co = image_size_y - 1
+                        if y_co < 0:
+                            y_co = 0
                 
-            pixelNumber = (image_size_x * y_co) + x_co
-            r = uv_pixels[pixelNumber*4]
-            g = uv_pixels[pixelNumber*4 + 1]
-            b = uv_pixels[pixelNumber*4 + 2]
-            col_in = r, g, b
-            col_result = [r,g,b] #col_in = texture-color, col_out = existing/ 'base' color
-            
-            if self.blendingMode == 'MIX':
-                col_result = col_in
+                if self.mirror_x:
+                     x_co = image_size_x -1 - x_co
+                     
+                if self.mirror_y:
+                     y_co = image_size_y -1 - y_co
+                     
+                col_out = vertex_colors.data[loop].color
+                    
+                pixelNumber = (image_size_x * y_co) + x_co
+                r = uv_pixels[pixelNumber*4]
+                g = uv_pixels[pixelNumber*4 + 1]
+                b = uv_pixels[pixelNumber*4 + 2]
+                a = uv_pixels[pixelNumber*4 + 3]
                 
-            elif self.blendingMode == 'ADD':
-                col_result[0] = col_in[0] + col_out[0]
-                col_result[1] = col_in[1] + col_out[1]
-                col_result[2] = col_in[2] + col_out[2]
+                col_in = r, g, b # texture-color
+                col_result = [r,g,b] # existing / 'base' color
                 
-            elif self.blendingMode == 'SUBTRACT':
-                col_result[0] = col_in[0] - col_out[0]
-                col_result[1] = col_in[1] - col_out[1]
-                col_result[2] = col_in[2] - col_out[2]
+                if self.blendingMode == 'MIX':
+                    col_result = col_in
+                    
+                elif self.blendingMode == 'ADD':
+                    col_result[0] = col_in[0] + col_out[0]
+                    col_result[1] = col_in[1] + col_out[1]
+                    col_result[2] = col_in[2] + col_out[2]
+                    
+                elif self.blendingMode == 'SUBTRACT':
+                    col_result[0] = col_in[0] - col_out[0]
+                    col_result[1] = col_in[1] - col_out[1]
+                    col_result[2] = col_in[2] - col_out[2]
+                    
+                elif self.blendingMode == 'MULTIPLY':
+                    col_result[0] = col_in[0] * col_out[0]
+                    col_result[1] = col_in[1] * col_out[1]
+                    col_result[2] = col_in[2] * col_out[2]
+                    
+                elif self.blendingMode == 'SCREEN':
+                    col_result[0] = 1 - (1.0 - col_in[0]) * (1.0 - col_out[0])
+                    col_result[1] = 1 - (1.0 - col_in[1]) * (1.0 - col_out[1])
+                    col_result[2] = 1 - (1.0 - col_in[2]) * (1.0 - col_out[2])
+                    
+                elif self.blendingMode == 'OVERLAY':
+                    if col_out[0] < 0.5:

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list