THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 29/08/2012 at 10:39, xxxxxxxx wrote:
Here is the code for a tag plugin that will draw text onto the screen like a HUD display using the DrawTexture() function.
I've converted this to python from my C++ plugin. But the SDK uses an unsigned short int (UWORD) in the alpha section that I do not know how to convert to python.
So I'm hoping someone here will know how to convert that part from C++ to python.
import c4d,os
from c4d import plugins, bitmaps, documents
PLUGIN_ID = 1000003 #TESTING ID# Only!!!
class DrawText(plugins.TagData) :
def Init(self, tag) :
return True
def Draw(self, tag, op, bd, bh) :
doc = documents.GetActiveDocument()
obj = tag.GetObject() #The object the python tag is on
if obj.GetType() == 5100:
points = obj.GetAllPoints()
globalpnts = obj.GetMg() * points[4]
##### There is an OpenGL bug that makes the text hide behind objects #####
##### This is a dummy 2D object with zero length which works around that bug #####
bd.SetPen(c4d.Vector(1.0, 1.0, 1.0))
bd.SetMatrix_Screen() #Use the screen's matrix to draw on
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
###############################################################
text = "My Text" #The text that will display on the screen
cm = c4d.bitmaps.GeClipMap()
cm.Init(0, 0, 32)
cm.BeginDraw() #Start drawing the object. This must be used before drawing the text
width = cm.TextWidth(text) #Gets the width of the text
height = cm.TextHeight() #Gets the height of the text
cm.EndDraw() #Tell C4D we're done drawing the text
cm.Destroy() #Release any memory used by the GeClipMap class
cm.Init(width, height, 32) #Dynamically sets the size of the virtual plane the text is on
cm.BeginDraw()
cm.SetColor(255, 255, 255, 255) #Sets the color of the text to white
cm.TextAt(0,0,text)
cm.EndDraw()
bd.SetMatrix_Screen() #We always need a matrix to draw things...In this case we'll use the screen's matrix
bd.SetLightList(c4d.BDRAW_SETLIGHTLIST_NOLIGHTS)#Use other options for different results if desired
xpos=255 #The X screen location of the left upper corner of the plane object the text bitmap is on
ypos=255 #The Y screen location of the left upper corner of the plane object the text bitmap is on
#Now we set the actual vector postions for the four point plane object that holds the text 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
]
#Now we set the color vector values for the plane object
cadr = [
(c4d.Vector(1,1,1)),
(c4d.Vector(1,1,1)),
(c4d.Vector(1,1,1)),
(c4d.Vector(1,1,1))
]
#Now we set up the normals directions for the four point plane object that holds the text bitmap
vnadr = [
(c4d.Vector(0,0,1)),
(c4d.Vector(0,0,1)),
(c4d.Vector(0,0,1)),
(c4d.Vector(0,0,1))
]
#Now we set up the UV's for the four point plane object that holds the text bitmap
uvadr = [
(c4d.Vector(0,0,0)),
(c4d.Vector(1,0,0)),
(c4d.Vector(1,1,0)),
(c4d.Vector(0,1,0))
]
cmbmp = bitmaps.BaseBitmap()
cmbmp = cm.GetBitmap()
bmp = bitmaps.BaseBitmap()
bmp = cm.GetBitmap()
alpha = bmp.GetInternalChannel() #Get at the RGBA channels of the bitmap copy
alpha = bmp.AddChannel(True, False)
################## This is the C++ looop that needs to be converted to python########
######## GetPixel() stores color data r,g,b in a UWORD variable that doesn't exist in python #######
#Apply the alpha bitmap to the solution so only the text is visible on the screen
#The alpha acts like a matte object. So if you find that your text is getting cut off..Your alpha might be too small
#You can increase the width and height of the alpha to fix that problem
# LONG x,y;
# for(y=0; y<height; y++)
# {
# for(x=0; x<width; x++)
# {
# UWORD r; //An unsigned short int variable...Can hold absolute values between 0->65,535
# bmp->GetPixel(x,y,&r,&r,&r); //Get each pixel's X.Y coords and assign them to pointers (r,g,b) (0 <= r/g/b <= 255)
# bmp->SetAlphaPixel(alpha, x, y, r); //r is the opacity
# }
# }
########################################################################################################
bd.DrawTexture(bmp,padr,cadr,vnadr,uvadr,4,c4d.DRAW_ALPHA_NORMAL,c4d.DRAW_TEXTUREFLAGS_0)
bd.SetDepth(True)
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 = "DrawText", g = DrawText, description = "drawtext", icon = bmp, info = c4d.TAG_EXPRESSION|c4d.TAG_VISIBLE)
-ScottA