On 04/12/2016 at 14:41, xxxxxxxx wrote:
Sry for the late reply I just got free time to go on this project and test what you said. And all is working. Moreover since it's store the basebitmap it's really cool ! 
Here is my ulgy test code if someone is interested !
Do not use it in production the db management is really done badly ! Never do something like that withtout a try/except/finally cause if any of the sql command fail your db will never be closed.
import sqlite3
import c4d
from c4d import bitmaps, storage
def WriteBitmap(bmp, format=c4d.FILTER_B3D,settings=c4d.BaseContainer()) :
mfs = storage.MemoryFileStruct()
mfs.SetMemoryWriteMode()
hf = storage.HyperFile()
if hf.Open(0, mfs, c4d.FILEOPEN_WRITE, c4d.FILEDIALOG_NONE) :
if not hf.WriteImage(bmp,format,settings) :
return None
hf.Close()
byteseq, size = mfs.GetData()
return byteseq, size
def ReadBitmap(byteseq) :
bmp = bitmaps.BaseBitmap()
hf = storage.HyperFile()
mfs = storage.MemoryFileStruct()
bmp = None
mfs.SetMemoryReadMode(byteseq, len(byteseq))
if hf.Open(0, mfs, c4d.FILEOPEN_READ, c4d.FILEDIALOG_NONE) :
bmp = hf.ReadImage()
hf.Close()
return bmp
def connect_db() :
db = sqlite3.connect('C:\\test.db')
db.text_factory = bytes
return db
def create_table(db) :
cursor = db.cursor()
cursor.execute('''
CREATE TABLE data(test BLOB)
''')
db.commit()
def add_data(db,data) :
cursor = db.cursor()
cursor.execute('''INSERT INTO data(test)
VALUES(:data)''',
{'data':data})
db.commit()
def get_data(db) :
cursor = db.cursor()
cursor.execute('''SELECT * FROM data''')
all_rows = cursor.fetchall()
db.close()
return all_rows
def main() :
path = "C:\\MyPicture.jpg"
if not path: return
# Create and initialize selected image
img = bitmaps.BaseBitmap()
if img.InitWith(path)[0] != c4d.IMAGERESULT_OK:
gui.MessageDialog("Cannot load image \"" + path + "\".")
return
byteseq, size = WriteBitmap(img) # Save image to hyper file in byte sequence
db = connect_db()
create_table(db)
add_data(db,bytes(byteseq))
test = get_data(db)[0][0]
bmp = ReadBitmap(test) # Read image from the byte sequence
bitmaps.ShowBitmap(bmp)
if __name__=='__main__':
main()
The trick was to use db.text_factory = bytes that force sqlite to return bytes instead of str(which fail decoding bytes.)
Btw could you confirm me if I do delete(memoryFileStruct) that will free the memory used by the memoryFileStruct I mean not only the object itself but also the memory where this file is pointing?
As said before since my plugin might be doing this function a lot of time I don't want to kill the memory by stacking something in it.
Anyway thanks for you reply ! 