I live in my own little world. But its ok, they know me there.

Array to bitmap

I wanted to learn how to take an array of colour values in cinema4d and write them to bitmap.

I started with a shader of a fish animated over a pixel map. Each pixel had it’s own colour value.

I wrote out the array of colours for each frame for the animated fish texture to CSV. With the following script:


import c4d
from c4d.modules import mograph as mo
from PIL import Image
import csv

def main():

coldata1 = "……(add path here)/colordatargb.csv"

md = mo.GeGetMoData(op)
if md is None: return False

cnt = md.GetCount()
marr = md.GetArray(c4d.MODATA_MATRIX)
carr = md.GetArray(c4d.MODATA_COLOR)

storedpix = []
for c in carr:

print storedpix
fall = md.GetFalloffs()
with open(coldata1, ‘wb’) as f:
col_str = str(storedpix)
#write string of col vals to csv
f.write(‘,’) #separate values with comma in csv file

md.SetArray(c4d.MODATA_MATRIX, marr, True)

return True


I then took the CSV file and wrote out an image file for each frame which saved like this:


from PIL import Image
import csv
import ast
import os

#READ CSV FROM CINEMA and sort data out
colourC4D = []
with open(‘D:/C4D FILE TEST/171224_bitmaptest/fishcsv01.csv’, ‘rb’) as csvfile:
readme = csv.reader(csvfile)
for row in readme:
ColourfromC4D = ‘, ‘.join(row)
ColourfromC4D = ColourfromC4D[:-2] # delete the last comma and the bracket ###deletion dependent on CSV -3 or -2
ColourfromC4D = ColourfromC4D[0:] #delete the first bracket and go from the first character ##deletion dependent on CSV 0 or 1
ColourfromC4D = ast.literal_eval(ColourfromC4D) #convert to tuple
#make empty list to store colours
for c in ColourfromC4D: #iterate over colours


# break list into n number of tuples

def group(lst, n):
for i in range(0, len(lst), n):
val = lst[i:i+n]
if len(val) == n:
yield tuple(val)

newdata2 = list(group(colourC4D,3))

#split the list into 100 values because there are 100 pixels and 3 values(rgb)
sizepixCountMultRGB= 686
newdata3 = [newdata2[i:i+sizepixCountMultRGB] for i in range(0, len(newdata2), sizepixCountMultRGB)]

# based on cinema 4d data
pixelspace = 14
pixel_width = 49 # pixels in width
pixel_height = 14 # pixels in height
pixelCount = 686# total pixel count

data = newdata3

for d in data:
frames = 0
d[:] = [d[i:i + pixelspace] for i in range(0, pixelCount, pixelspace)]

img =‘RGB’, (pixel_width, pixel_height)) # mode type and bit depth of pixel image, size pixel height and width

pixels = img.load()

for i in range(img.size[0]):

for j in range(img.size[1]):

pixels[i, j] = d[i][j] #Show the image in picture viewer once created‘D:/C4D FILE TEST/171224_bitmaptest/bitmapseq/fullcolseq0.bmp’ )

while os.path.exists("D:/C4D FILE TEST/171224_bitmaptest/bitmapseq/fullcolseq%s.bmp" % frames):
frames += 1

open("D:/C4D FILE TEST/171224_bitmaptest/bitmapseq/fullcolseq%s.bmp" % frames, "w")‘D:/C4D FILE TEST/171224_bitmaptest/bitmapseq//fullcolseq%s.bmp’ % frames )


I ended up with files like this in my directory:

I then had a perfect pint sized animation that could be applied as a texture in a game engine i guess(need to try this…)

Next Post

Previous Post

© 2023 anam-ation

Theme by Anders Norén