On 06/08/2014 at 17:13, xxxxxxxx wrote:
OK. I spent some time trying out the various bitmap and clipmap functions.
And it looks like the transparency can be set using a new clipmap that get's the data from your source image.
There's a strange bug in it though. For some reason once it hits 50% the image goes full transparent.:angry:
I'm not sure it's this is bug. Or I'm trying to make it do something it wasn't designed to do.
Here is the entire code for a tag plugin. Minus the other files (.res, .str, .etc)
#This is an example of changing an image's pixel and alpha values
#We can use a new bitmap instance to change the color & alpha values separately
#Or...We can use a new clipmap to set the alpha value for the entire image
#In either case. We must create a new bitmap or clipmap because we can't change the source image without physically saving it
#Use the proper DrawTexture() function at the end of the Draw() method. Depending on which method you're using
import c4d,os
from c4d import plugins, bitmaps, documents
PLUGIN_ID = 1000003 #TESTING ID# Only!!!
#This variable sets the amount of transparency
#NOTE: for some reason if I go below 51 the image goes full transparent...BUG??
transAmount = 51
class DrawImage(plugins.TagData) :
bm1 = bitmaps.BaseBitmap() #The source image
newbm = bitmaps.BaseBitmap() #A new empty bitmap image we will construct from scratch using the source image data
newcm = c4d.bitmaps.GeClipMap() #A new empty clipmap image we will construct from scratch using the source image data
def Init(self, tag) :
dir, file = os.path.split(__file__)
imgPath = os.path.join(dir, "res", "image1.psd") #The path to the res folder and the image to use
self.bm1.InitWith(imgPath)
#Init the new bitmap using the source image dimensions
width = self.bm1.GetBh()
height = self.bm1.GetBw()
self.newbm = c4d.bitmaps.BaseBitmap()
self.newbm.Init(width,height,32)
#Init the new clipMap using the source image dimensions
self.newcm.Init(width,height,32)
return True
def Draw(self, tag, op, bd, bh) :
doc = documents.GetActiveDocument()
bd.SetMatrix_Screen()
##### This is a little trick to fix the Z depth bug #####
bd.DrawLine2D(c4d.Vector(0, 0, 0), c4d.Vector(0, 0, 0)) #Draw a line with a zero length<---This is our dummy object
bd.SetDepth(True) #This fixes drawing problems when using 2D functions
#################################
#Get the source image's dimensions
width = self.bm1.GetBh()
height = self.bm1.GetBw()
#Get the alpha channel for both the original image
bm1AlphaChannel = self.bm1.GetInternalChannel()
#Create an alpha channel for new bitmap image (it doesn't have one when we create one from scratch)
newbmAlphaChannel = self.newbm.GetInternalChannel() #Get at the RGBA channels of the bitmap copy
newbmAlphaChannel = self.newbm.AddChannel(True, False) #Add a channel and assign it to a variable so we can use it later on
#Get the color & alpha values from the bm1 bitmap
#And copy the values to the newbm bitmap
for y in xrange(height) :
for x in xrange(width) :
#Copy the colors and alpha values to the new bitmap image
r,g,b = self.bm1.GetPixel(x, y)
alpha = self.bm1.GetAlphaPixel(bm1AlphaChannel, x, y)
self.newbm.SetPixel(x,y, r,g,b)
self.newbm.SetAlphaPixel(newbmAlphaChannel, x, y, alpha)
#Change the colors and alpha values to the new bitmap image
self.newbm.SetPixel(x,y, r+0,g+0,b+0)
self.newbm.SetAlphaPixel(newbmAlphaChannel, x, y, transAmount)
#Use the new clipMap SetPixelRGBA() function to set the transparency for the image
self.newcm.BeginDraw()
self.newcm.SetPixelRGBA(x,y, r,g,b, transAmount)
self.newcm.EndDraw()
#Use these to move the image around in scene editor window
xpos = 20 #The X screen location of the left upper corner of the image
ypos = 50 #The Y screen location of the left upper corner of the image
#Set the actual vector positions for the four point plane object that holds the bitmap
padr = [
(c4d.Vector(xpos,ypos,0)), #upper left corner
(c4d.Vector(xpos+width,ypos,0)), #upper right corner
(c4d.Vector(xpos+width,ypos+height,0)), #lower right corner
(c4d.Vector(xpos,ypos+height,0)) #lower left corner
]
cadr = [(c4d.Vector(1,1,1)),(c4d.Vector(1,1,1)),(c4d.Vector(1,1,1)),(c4d.Vector(1,1,1))] #Array with color vectors
vnadr = [(c4d.Vector(0,0,1)),(c4d.Vector(0,0,1)),(c4d.Vector(0,0,1)),(c4d.Vector(0,0,1))] #Array with normals of vertices
uvadr = [(c4d.Vector(0,0,0)),(c4d.Vector(1,0,0)),(c4d.Vector(1,1,0)),(c4d.Vector(0,1,0))] #Array with texture UVs
#Use this version to draw the new image with the changed color & alpha values individually
#bd.DrawTexture(self.newbm,padr,cadr,vnadr,uvadr,4,c4d.DRAW_ALPHA_FROM_IMAGE,c4d.DRAW_TEXTUREFLAGS_0)
#Use this version to set the transparency value for the entire image
nb = self.newcm.GetBitmap()
bd.DrawTexture(nb,padr,cadr,vnadr,uvadr,4,c4d.DRAW_ALPHA_FROM_IMAGE,c4d.DRAW_TEXTUREFLAGS_0)
return True
if __name__ == "__main__":
path, file = os.path.split(__file__)
bmp = bitmaps.BaseBitmap()
bmp.InitWith(os.path.join(path, "res", "icon.tif"))
plugins.RegisterTagPlugin(id = PLUGIN_ID, str = "drawimg", g = DrawImage, description = "drawimg", icon = bmp, info = c4d.TAG_EXPRESSION|c4d.TAG_VISIBLE)
-ScottA