Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    How to set the active documents preview image

    Cinema 4D SDK
    windows python
    2
    2
    369
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      MMayrh
      last edited by

      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

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @MMayrh
        last edited by ferdinand

        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 of BaseBitmap.

        54c636b2-05c8-4e3a-af41-9edf7e80bd24-image.png

        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.

        3e4681a7-7db6-45b7-a1da-5279cc028cf2-image.png

        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:

        459e7ff6-a8fe-4abf-ae35-adc803c17170-image.png

        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 the BitmapButtonStruct 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 implement MSG_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 the SetParameter 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

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • First post
          Last post