[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3725] contrib/py/scripts/addons/ bake_uv_texture_to_vertex_colors.py: Addon: Bakes the colors of the active UV Texture to Vertex Colors.

Sebastian Nell codemanx at gmx.de
Mon Sep 10 22:39:05 CEST 2012


Revision: 3725
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3725
Author:   codemanx
Date:     2012-09-10 20:39:05 +0000 (Mon, 10 Sep 2012)
Log Message:
-----------
Addon: Bakes the colors of the active UV Texture to Vertex Colors. Uses the active object and creates new VCol layer. Supports various blending and mapping modes.

Initial commit of Baking script to SVN. Updated for Bmesh, original script by Patrick Boelens. Tested on a few simple models, no bugs so far.

Added Paths:
-----------
    contrib/py/scripts/addons/bake_uv_texture_to_vertex_colors.py

Added: contrib/py/scripts/addons/bake_uv_texture_to_vertex_colors.py
===================================================================
--- contrib/py/scripts/addons/bake_uv_texture_to_vertex_colors.py	                        (rev 0)
+++ contrib/py/scripts/addons/bake_uv_texture_to_vertex_colors.py	2012-09-10 20:39:05 UTC (rev 3725)
@@ -0,0 +1,299 @@
+ #  ***** BEGIN GPL LICENSE BLOCK *****
+ #
+ #  This program is free software: you can redistribute it and/or modify
+ #  it under the terms of the GNU General Public License as published by
+ #  the Free Software Foundation, either version 3 of the License, or
+ #  (at your option) any later version.
+ #
+ #  This program is distributed in the hope that it will be useful,
+ #  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ #  GNU General Public License for more details.
+ #
+ #  You should have received a copy of the GNU General Public License
+ #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ #
+ #  The Original Code is Copyright (C) <date>-<date> by <name of copyright holder>    ###
+ #  All rights reserved.
+ #
+ #  Contact:      p_boelens at msn.com
+ #  Information:  http://projects.blender.org/tracker/index.php?func=detail&aid=28211
+ #
+ #  Contributor(s): CoDEmanX.
+ #
+ #  ***** END GPL LICENSE BLOCK *****
+
+bl_info = {
+    "name": "Bake UV-Texture to Vertex Colors",
+    "description": "Bakes the colors of the active UV Texture to Vertex Colors on a new layer.",
+    "author": "Patrick Boelens, CoDEmanX",
+    "version": (0, 4),
+    "blender": (2, 6, 3),
+    "location": "3D View > Vertex Paint > Toolshelf > Bake",
+    "warning": "Sometimes fires errors when it shouldn't. A retry usually works for now.",
+    "wiki_url": "http://wiki.blender.org/index.php?title=User_talk:Senshi&oldid=143669",
+    "tracker_url": "http://projects.blender.org/tracker/index.php?func=detail&aid=28211",
+    "category": "Baking"}
+
+import bpy
+from math import fabs
+from colorsys import rgb_to_hsv, hsv_to_rgb
+
+class UVtoVC(bpy.types.Operator):
+    bl_idname = "object.uv_to_vcol" # uv/texture op?
+    bl_label = "Bake UV Texture to Vertex Colors"
+    bl_description = "Bake UV Texture to Vertex Colors (requires image texture)"
+    bl_options = {'REGISTER', 'UNDO'}
+       
+    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.")
+                 ]
+    
+    mappingMode = bpy.props.EnumProperty(items=mappingModes, default="CLIP", name="Mapping", description="The mode to use for baking vertices who's UV-coordinates are out of bounds.")
+    
+    blendingModes = [("MIX", "Mix", ""),
+                     ("ADD", "Add", ""),
+                     ("SUBTRACT", "Subtract", ""),
+                     ("MULTIPLY", "Multiply", ""),
+                     ("SCREEN", "Screen", ""),
+                     ("OVERLAY", "Overlay", ""),
+                     ("DIFFERENCE", "Difference", ""),
+                     ("DIVIDE", "Divide", ""),
+                     ("DARKEN", "Darken", ""),
+                     ("LIGHTEN", "Lighten", ""),
+                     ("HUE", "Hue", ""),
+                     ("SATURATION", "Saturation", ""),
+                     ("VALUE", "Value", ""),
+                     ("COLOR", "Color", ""),
+                     ("SOFT_LIGHT", "Soft Light", ""),
+                     ("LINEAR_LIGHT", "Linear Light", "")
+                    ]
+      
+    blendingMode = bpy.props.EnumProperty(items=blendingModes, default="MULTIPLY", name="Blend Type", description="The blending mode to use when baking")
+    
+    
+    mirror_x = bpy.props.BoolProperty(name="Mirror X", description="Mirror the image on the X-axis.")
+    mirror_y = bpy.props.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)
+
+    def execute(self, context):
+        obdata = context.object.data
+        loop_count = len(obdata.loops)
+        uv_tex = obdata.uv_textures.active.data[0]
+        image_size_x = uv_tex.image.size[0]
+        image_size_y = uv_tex.image.size[1]
+        
+        # Accessing this directly is far too slow. Copied to new array for massive performance-gain.
+        uv_pixels = uv_tex.image.pixels[:]
+            
+        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))
+            
+            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':
+                    while x_co > image_size_x - 1:
+                        x_co -= image_size_x
+                    while x_co < 0:
+                        x_co += image_size_x
+                    while y_co > image_size_y - 1:
+                        y_co -= image_size_y
+                    while y_co < 0:
+                        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
+                
+            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
+                
+            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:
+                    col_result[0] = col_out[0] * (2.0 * col_in[0])
+                else:
+                    col_result[0] = 1.0 - (2.0 * (1.0 - col_in[0])) * (1.0 - col_out[0])
+                if col_out[1] < 0.5:
+                    col_result[1] = col_out[1] * (2.0 * col_in[1])
+                else:
+                    col_result[1] = 1.0 - (2.0 * (1.0 - col_in[1])) * (1.0 - col_out[1])
+                if col_out[2] < 0.5:
+                    col_result[2] = col_out[2] * (2.0 * col_in[2])
+                else:
+                    col_result[2] = 1.0 - (2.0 * (1.0 - col_in[2])) * (1.0 - col_out[2])
+                
+            elif self.blendingMode == 'DIFFERENCE':
+                col_result[0] = fabs(col_in[0] - col_out[0])
+                col_result[1] = fabs(col_in[1] - col_out[1])
+                col_result[2] = fabs(col_in[2] - col_out[2])
+                
+            elif self.blendingMode == 'DIVIDE':
+                if(col_in[0] != 0.0):
+                    col_result[0] = col_out[0]/ col_in[0]
+                if(col_in[1] != 0.0):
+                    col_result[0] = col_out[1]/ col_in[1]
+                if(col_in[2] != 0.0):
+                    col_result[2] = col_out[2]/ col_in[2]
+                
+            elif self.blendingMode == 'DARKEN':
+                if col_in[0] < col_out[0]:
+                    col_result[0] = col_in[0]
+                else:
+                    col_result[0] = col_out[0]
+                if col_in[1] < col_out[1]:
+                    col_result[1] = col_in[1]
+                else:
+                    col_result[1] = col_out[1]
+                if col_in[2] < col_out[2]:
+                    col_result[2] = col_in[2]
+                else:
+                    col_result[2] = col_out[2]
+                
+                
+            elif self.blendingMode == 'LIGHTEN':
+                if col_in[0] > col_out[0]:
+                    col_result[0] = col_in[0]
+                else:
+                    col_result[0] = col_out[0]
+                if col_in[1] > col_out[1]:
+                    col_result[1] = col_in[1]
+                else:
+                    col_result[1] = col_out[1]
+                if col_in[2] > col_out[2]:
+                    col_result[2] = col_in[2]
+                else:
+                    col_result[2] = col_out[2]
+                
+            elif self.blendingMode == 'HUE':

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list