How to set the active documents preview image
-
Hey,
I am trying to code a python script that automatically renders the active document and sets the preview for the document to that rendered image.
The set parameter that I use wants a BaseList2D object instead of a bitmap and I have no idea how I could convert that info. (Or maybe im dead lost already ) Maybe you can help?
Thanks in advance
import c4d from c4d import documents import os def main(): doc = documents.GetActiveDocument() rd = doc.GetActiveRenderData() # Set resolution rd[c4d.RDATA_XRES] = 640 rd[c4d.RDATA_YRES] = 480 # Render document bmp = c4d.bitmaps.BaseBitmap() bmp.Init(640, 480) if c4d.documents.RenderDocument(doc, rd.GetDataInstance(), bmp, c4d.RENDERFLAGS_EXTERNAL) != c4d.RENDERRESULT_OK: raise Exception("Rendering failed") # Save bitmap to disk for manual setting of preview later temp_filepath = os.path.join(os.path.expanduser("~"), "temp_preview.png") if bmp.Save(temp_filepath, c4d.FILTER_PNG, c4d.BaseContainer()) != c4d.IMAGERESULT_OK: raise Exception("Failed to save bitmap") # Display the bitmap for verification c4d.bitmaps.ShowBitmap(bmp) # Set the rendered Bitmap bmp as the preview of the document doc.SetParameter(c4d.DOCUMENT_PREVIEW_IMAGE, bmp, c4d.DESCFLAGS_SET_0) #<--- cant figure out how to get this working if __name__ == '__main__': main()
Version: Cinema4D 2025.0.2
OS: Windows 11
Python -
Hey @MMayrh,
Thank you for reaching out to us. This is not how this parameter works. The parameter DOCUMENT_PREVIEW_IMAGE is of data type
BitmapButtonStruct
, not ofBaseBitmap
.The documentation is a bit misleading here. This is not the document preview bitmap, but sort of the preview bitmap delegate. I.e., an entity that is used to retrieve a preview bitmap (for a document in this case). A
BitmapButtonStruct
wraps a node, an ID, and a dirty flag.This parameter does not make too much sense in the Python API, here is what the C++ API does when the parameter is access for a document:
I.e., it returns itself (the doc), the ID of the parameter (
DOCUMENT_PREVIEW_IMAGE
) and the dirty state of its internal current preview bitmap as theBitmapButtonStruct
bbs. What you could technically try, is implement a node, e.g., an object, and then set that object as the preview provider for a document. Your node would for that have to implementMSG_DESCRIPTION_GETBITMAP
, because that is what effectively will be called. But that is all very theoretical,DOCUMENT_PREVIEW_IMAGE
is largely unused in our code base, and I do not see any implemnation for theSetParameter
part. So, the document will likely just ignore you trying to overwrite its preview provider. There could be some base implemenation kicking in, but I doubt it.But what you definitely cannot do, is just set there a bitmap to overwrite the preview image of the document (assuming that was what you wanted to do). That would also not make too much sense since a document is constantly recalculating its preview image. So, on the next update that image would be gone (if it would work like that).
Cheers,
Ferdinand