Update 2.49 script to work in 2.6x

Hey there,

I am trying to update this 2.49 script to work in 2.6x. If anyone could help me out or get me started that would be great.



- #!BPY
- # Install the script in the 'Object' menu
- """
- Name: 'Vertex Color Alpha-izer'
- Blender: 249
- Group: 'Object'
- Tooltip: 'Sets the vertex color alpha based on the color of the second vertex color layer'
- """
- """
- When painting on the second vertex color layer, anything black will be fully opaque and
- the closer you get to white the closer you get to fully transparent
- """
- import os
- import sys
- import struct
- import math
- import string
- import Blender
- import [platform](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=platform)
- import shutil
- import syslog
- import imp
- import BlenderScripts.UnityLib
- UL = BlenderScripts.UnityLib
- UL = reload( BlenderScripts.UnityLib )
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender                    import [Material](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Material)
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender                    import [Texture](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Texture)
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender                    import [Window](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Window)
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender                    import Object
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender                    import NMesh
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender                    import Mathutils
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender.Scene              import [Render](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Render)
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender.[Draw](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Draw)               import *
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender.BGL                import *
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender.[Window](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Window)             import *
- [from](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=from) Blender.Mathutils          import *
- # GUI variables
- EVENT_EXIT          = 0
- EVENT_SET_ALPHA     = 1
- EVENT_TOGGLE_FACES  = 2
- SEL_FACES_ONLY      = 1
- #=========================================
- # RENDER THE GUI
- #=========================================
- def drawGui():
-     glClearColor( 0.74, 0.74, 0.74, 0.0 )
-     glClear( GL_COLOR_BUFFER_BIT )
-     UL.drawRect( 5, 92, 450,  20, 0.6, 0.6, 0.6, 1.0 )
-     UL.drawRect( 5,  6, 450,  86, 0.8, 0.8, 0.8, 1.0 )
-     glColor3f( 1.0, 1.0, 1.0 )
-     glRasterPos2i( 15, 98 )
-     Text( "Vertex Color Alpha-izer" )
-     glPolygonMode( GL_FRONT_AND_BACK, GL_LINE )
-     UL.drawRect( 5,  7, 450, 106, 0.5, 0.5, 0.5, 1.0 )
-     UL.drawRect( 5,  7, 450,  86, 0.5, 0.5, 0.5, 1.0 )
-     glPolygonMode( GL_FRONT_AND_BACK, GL_FILL )
-     glColor3f( 0.0, 0.0, 0.0)
-     glRasterPos2i( 15, 72 )
-     Text( "Sets the vertex colors alpha value based on the color of the second layer" )
-     glRasterPos2i( 15, 52 )
-     Text( "If 'Selected Faces Only' is unchecked, all faces vertex colors will change" )
-     [Toggle](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Toggle)( "Selected Faces Only", EVENT_TOGGLE_FACES, 20, 16, 155, 20, SEL_FACES_ONLY, "Only change the alpha of selected faces" )
-     [Button](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Button)( "Modify Alpha", EVENT_SET_ALPHA, 255, 16, 105, 20 )
-     [Button](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Button)( "Exit", EVENT_EXIT, 370, 16, 75, 20 )
- #====================================
- # GUI EVENTS CALLBACKS
- #====================================
- def eventGui( event ):
-     global SEL_FACES_ONLY
-     if( event == EVENT_EXIT ):
-         [Exit](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Exit)()
-     if( event == EVENT_SET_ALPHA ):
-         setVertexAlpha()
-     if( event == EVENT_TOGGLE_FACES ):
-         SEL_FACES_ONLY = not SEL_FACES_ONLY
- #====================================
- # SETS THE VERTEX ALPHA COLOR
- #====================================
- def setVertexAlpha():
-     global SEL_FACES_ONLY
-     # Grab the selected objects
-     scn = Blender.Scene.GetCurrent()
-     UL.log( "---------- Starting vertex alpha-izer -------------" )
-     # Loop through our selected objects
-     for obj in scn.[objects](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=objects):
-         # Make sure the object is on the first layer, it is a Mesh and it is selected
-         if( obj.getType() == "Mesh" and obj.layers[0] == 1 and obj.isSelected() ):
-             # Grab the mesh data and vertex color layers
-             mesh = obj.getData( False, True )
-             vertexColorLayerNames = mesh.getColorLayerNames()
-             if len( vertexColorLayerNames ) != 2:
-                 continue;
-             # Rename our colorLayers for clarity
-             mesh.renameColorLayer( vertexColorLayerNames[0], 'RGBA' )
-             mesh.renameColorLayer( vertexColorLayerNames[1], 'Alpha' )
-             # Set the 'Alpha' layer as active and get all the colors from it
-             mesh.activeColorLayer = 'Alpha'
-             alphaColorList = []
-             # Grab all the selected faces
-             allMeshFaces = UL.getMeshObjectFaces( obj, SEL_FACES_ONLY )
-             # Save all the selected faces colors
-             for face in allMeshFaces:
-                 alphaColorList.append( face.col )
-             # Set the 'RGBA' layer as active and use the 'Alpha' values to set the alpha of the 'RGBA' colors
-             mesh.activeColorLayer = 'RGBA'
-             for face in allMeshFaces:
-                 alphaColorsForFace = alphaColorList.pop( 0 )
-                 # Loop through the colors for this face setting the alpha based on the color from the 'Alpha' layer
-                 for rgbaColor, alphaColor in zip( face.col, alphaColorsForFace ):
-                     rgbaColor.a = ( alphaColor.r + alphaColor.g + alphaColor.b ) / 3
-                     UL.log( "new alpha: %s" % rgbaColor.a )
-             mesh.update()
-             [Exit](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Exit)()
- #====================================
- # MAIN
- #====================================
- Register( drawGui, None, eventGui )


Thanks for looking at this.

Andrew

you need to rewrite it from scratch, but i’m actually not sure how it works and if it’s still possible in 2.5x+

was there alpha support in 2.4x? Or is this some kind of trick, two regular color layers?

Your guess is 100% correct. It overcomes the Blender limitation by combining two color layers.

This original code was developed in conjunction with the .fbx export to bring RGBA vertex values into Unity.

Does Blender color layer have space for 4 channels and the 4th is just not editable in vertex paint, or is RGB with no Alpha hard coded into the color by definition?

Ideally the 4 channels would be visible in the editor - but the two color layer trick was at least a good hack. It was RGB of first layer combined with Black/White values of the second layer to simulate Alpha.

edit* btw your texture to vertex color script is very much appreciated.

There’s no 4th component, IIRC a hack is used internally for drawing which uses the alpha component, so it’s not easily added.

Drawing support not either, but ideasman liked the idea to show multiple vcol layers at once (would require blending modes and alpha support).

You can use a second layer of course and treat it as alpha. You can even use colors and turn it into B/W (using either 1/3 of every component, or using a luminance formula for better results).

https://code.google.com/p/blender-cod/source/browse/blender_26/export_xmodel.py#568

Good news!

Found a dev on IRC who is going to look at just modifying the export_fbx.py code to accept values from 2 color layers.

I’m pretty excited.