Set Document Preview



  • On 24/01/2014 at 10:28, xxxxxxxx wrote:

    Is there a way to set the document preview or force it to create one when saving a file via the SaveProject() function?

    None of the files generated by my script have a preview of the scene when selected in the browser of Mac OS X.  Documents I manually save show a preview when selected in the file browser.



  • On 26/01/2014 at 04:02, xxxxxxxx wrote:

    Hi,

    check out the following piece of code. This might help you how to initialize and set the preview bitmap.

    import c4d
    import os
      
    def SetDocumentPreviewBitmap(doc, fn) :
        previewBmp = doc.GetDocPreviewBitmap()
        if not os.path.exists(fn) :
            raise StandardError, "Bitmap file does not exist"
        
        if not previewBmp.InitWith(fn) :
            raise StandardError, "Could not load file"
      
        return
      
    d = c4d.documents.BaseDocument()
    d.InsertObject(c4d.BaseObject(c4d.Ocube)) #there must be at least one object in the scene to generate a preview
      
    SetDocumentPreviewBitmap(d, "C:\\Users\\YourUserName\\Desktop\\abc.png")
      
    c4d.documents.SaveDocument(d, "C:\\Users\\YourUserName\\Desktop\\file.c4d", c4d.SAVEDOCUMENTFLAGS_0, c4d.FORMAT_C4DEXPORT)
    

    Cheers



  • On 28/01/2014 at 19:22, xxxxxxxx wrote:

    A belated thank you for the post. I was unable to get on the forum for some reason.  I've been busy with other stuff this week but hope to take a stab at it tomorrow.



  • On 04/02/2014 at 08:14, xxxxxxxx wrote:

    I was able to take what you provided and get it working with a file saved to the hard drive and read back in to create the document preview.  Thank you for the direction.

    I would prefer to skip the saving file part.  The documentation for BaseBitmap.InitWith shows that it can be passed a string or a MemoryFileStruct.  I found the example script for writing a bmp to a MFS so I incorporated that into the script but must be missing something.  Im not getting any error messages but I'm not getting a preview either.

    I tried sending the bmp to the picture viewer and have confirmed everything is working from the render standpoint.  Any insight is appreciated.

    import c4d
    import os
    from c4d import bitmaps, documents, storage
      
    def WriteBitmap(bmp, format=c4d.FILTER_JPG,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) :
                print "fail"
                return None
            hf.Close()
        return mfs
      
    d = c4d.documents.BaseDocument()
    d.InsertObject(c4d.BaseObject(c4d.Ocube)) 
    rd = d.GetActiveRenderData().GetData()
    xres = int(rd[c4d.RDATA_XRES])
    yres = int(rd[c4d.RDATA_YRES])
    bmp = bitmaps.BaseBitmap()
    bmp.Init(x=xres, y=yres, depth=24)
      
    rd[c4d.RDATA_RENDERENGINE] = c4d.RDATA_RENDERENGINE_PREVIEWHARDWARE 
    res = documents.RenderDocument(d, rd, bmp, c4d.RENDERFLAGS_EXTERNAL)
      
    mfs = WriteBitmap(bmp)
      
    if res==c4d.RENDERRESULT_OK:
        previewBmp = d.GetDocPreviewBitmap()
        if not previewBmp.InitWith(mfs) :
            raise StandardError, "Not working"
      
    c4d.documents.SaveDocument(d, "filepath/file.c4d", c4d.SAVEDOCUMENTFLAGS_0, c4d.FORMAT_C4DEXPORT)
    


  • On 04/02/2014 at 13:24, xxxxxxxx wrote:

    Hi,

    try use 'doc' for the current document and try again. Here it works fine then.

    Cheers,
    s_rath



  • On 05/02/2014 at 06:19, xxxxxxxx wrote:

    Strange.  I changed it to previewBmp = doc.GetDocPreviewBitmap() and it still doesn't do it.  I end up with the generic c4d preview icon.



  • On 05/02/2014 at 06:35, xxxxxxxx wrote:

    If I load an external file the result of bitmap.InitWith()  is (1, False)  when I run my MemoryFileStructure version the result is (-1, False).



  • On 18/02/2014 at 12:35, xxxxxxxx wrote:

    Is it possible there is a difference between Mac and Windows implementation and we're on different platforms getting different results?  I'm doing this in OSX.

    thanks again for your help.



  • On 20/02/2014 at 14:21, xxxxxxxx wrote:

    No, the code here should work on Win and OSX the smae way.

    Cheers, s_rath


Log in to reply