How to use Python to generate a bitmap with layer?

  • On 14/05/2015 at 15:10, xxxxxxxx wrote:


    I try to write an plugin that split a image into a image with layer.
    Each color (material id's) get it own layer.
    The layer are convert to use for alphamaps in c4d material.

    I can initialize a bitmap from a file.
    But it is BaseBitmap.

    I try to use MultipassBitmap.
    Now I can create a new empty layer.

    But I cant set Pixel at the empty layer.

    How can I set pixel to a layer?

    import c4d   
    import os   
    from c4d import bitmaps, gui, storage   
    def main() :   
        doc = c4d.documents.GetActiveDocument()   
        op = doc.GetActiveObject()   
        path = storage.LoadDialog(type=c4d.FILESELECTTYPE_IMAGES, title="Please Choose the PolyPaint Image")   
        if not path: return   
        # Create and initialize selected image   
        orig = bitmaps.BaseBitmap()   
        if orig.InitWith(path)[0] != c4d.IMAGERESULT_OK:   
            gui.MessageDialog("Cannot load image \"" + path + "\".")   
        # Get selected image infos   
        width, height = orig.GetSize()   
        bits = orig.GetBt()   
        # Create the copy and initialize it   
        copy = bitmaps.BaseBitmap()   
        copy.Init(width, height, bits)   
        multibmp = bitmaps.MultipassBitmap.AllocWrapper(copy)   
        layer_a = multibmp.AddLayer(multibmp.GetLayers(c4d.MPB_GETLAYERS_0),1)   
        # ColorPalette Variable vorbereiten   
        ColPal = [0,0]   
        cc = 0 # Zähler auf 0   
        while cc < 512: # ColorPalette 512 Einträge mit 0 füllen   
            cc = cc + 1   
        wy = 0   
        while wy < height: # Bildzeilen durchlaufen   
            wx = 0      
            while wx < width: # Bildpunkte der Zeile durchlaufen   
                readcol = orig.GetPixel(wx,wy) # Pixel als Farb-Vector auslesen   
                farbanzahl = 6               # In wie viele Farben aufteilen?   
                farbdivid = 256 * 3 / farbanzahl # Bit + Anzahl Farbkanäle geteilt durch die Farbanzahl = Divisor   
                # Farbkanäle runter rechnen   
                # reduzierte Farben in neue Bitmap eintragen   
                ColPal[r*g*b] = [r,g,b] # ColorPalette (512 mögliche Farben) mit Wert füllen   
                wx = wx +1 # Zeile +1   
            wy = wy + 1 # Spalte +1   
        bitmaps.ShowBitmap(orig) # Show original   
        bitmaps.ShowBitmap(copy) # Show copied image   
        bitmaps.ShowBitmap(multibmp) # Show copied image   
        # Einträge auswerten und anzeigen   
        ccc = 0   
        for element in ColPal:   
           if element > [0,0,0]:   
                print element   
                ccc = ccc + 1   
        print ccc   
        print layer_a   
        print multibmp   
    if __name__=='__main__':   

    This is my code for loading an image.
    Reduce the colors and write to a new bitmap.
    And I create a multipass bitmap for test to create a layer.

    Maybe, later I want to use OpenCV or PIL library for better convert the image.


  • On 18/05/2015 at 02:51, xxxxxxxx wrote:


    it seems that a MultipassBitmap layer can only be accessed through GetPixelCnt() / SetPixelCnt(), you you have to setup a ByteSeq:

    mp = c4d.bitmaps.MultipassBitmap(200,200,c4d.COLORMODE_RGB)  
    newlayer = mp.AddLayer(None,c4d.COLORMODE_RGB)  
    newlayer.SetParameter(c4d.MPBTYPE_NAME,"the new layer")  
    sq =, 3)  
    for x in xrange(200) :  
         for y in xrange(200) :  
                  # set the pixel to 255/128/0  
                  sq[0] = chr(255)  
                  sq[1] = chr(128)  
                  sq[2] = chr(0)  
    saveImagePath =,"Save as TIFF")  

    best wishes,

  • On 29/05/2015 at 07:43, xxxxxxxx wrote:

    Hello Bernd,

    was your question answered?

    Best wishes,

  • On 04/06/2015 at 10:55, xxxxxxxx wrote:

    Hi Sebastian :-)

    thank you after I try to develope the app I make a pause.
    I have read you answer - very best thanks!!!

    I will try it later ;-)
    Today I try another start to develope python script.

    Maybe some days later I will answer the code can help, or not.
    Thank you again, I don't forgot to post it later ;-)

  • On 10/06/2015 at 16:52, xxxxxxxx wrote:

    Looks good, but there is a problem:
    if I save as tif it will be very good!!!
    I open in Photoshop and there are the layers i can create now :-)

    But when I open the mp (bitmap) in the picture viewer it only show one layer!

    mp = c4d.bitmaps.MultipassBitmap(200,200,c4d.COLORMODE_RGB|c4d.COLORMODE_ALPHA)   
    newlayer = mp.AddLayer(None,c4d.COLORMODE_RGB)   
    newlayer.SetParameter(c4d.MPBTYPE_NAME,"the new layer")   
    sq =, 3)   
    for x in xrange(200) :   
        for y in xrange(200) :   
        #set the pixel to 255/128/0   
        sq[0] = chr(255)   
        sq[1] = chr(128)   
        sq[2] = chr(0)   
    # write pixel            newlayer.SetPixelCnt(x,y,1,sq.GetOffset(0),3,c4d.COLORMODE_RGB,c4d.PIXELCNT_0)   
    # a second layer   
    newlayerA = mp.AddLayer(None,c4d.COLORMODE_ALPHA)   
    newlayerA.SetParameter(c4d.MPBTYPE_NAME,"the new layer2")   
    sqA =, 3)   
    for x in xrange(200) :   
        for y in xrange(200) :   
        #set the pixel to 255/128/0   
        sqA[0] = chr(255)   
        sqA[1] = chr(128)   
        sqA[2] = chr(255)   
    saveImagePath =,"Save as TIFF")   

  • On 10/06/2015 at 17:05, xxxxxxxx wrote:


    Picture Viewer in C4D:

  • On 11/06/2015 at 01:05, xxxxxxxx wrote:


    a layer only appears in the Picture Viewer if the MPBTYPE_SHOW parameter is set to true. See also this thread: "Bake texture and MultipassBitMap".

    Best wishes,

  • On 11/06/2015 at 03:29, xxxxxxxx wrote:

    Thank you very much

Log in to reply