Solved How to render a animation preview ?

Hello !

Question :

I wonder how can I do the same thing as the Make Preview do and auto save and show in Picture View ?

Details :

I think it is some steps :

  • Get the frame range and save path in Save in ActiveRenderData
  • Get the Viewport Renderer VideoPost settings
  • Render document as mp4 in half of the resolution
  • Save MP4 file to disk and load back to Picture Viewer

The output maybe like a 1s mp4 file named 'cube.mp4' and saved to disk .

for step 1 and 2 I have some experences , but for 3 and 4 , I have no idea how to do it right .
And I have read some post and python documents like RenderDocument() , but I don't know how it works with mp4 and how to set it . could you please provide a bare-bone code for this ?

Many thanks:blush:

Hi,

You can retrieve the render settings, update them and, use the command RenderDocument. In c++, the command ShowBitmap allow to display a file in the Picture viewver while in python you can only display bitmaps. The bitmap passed to the RenderDocument function is used as a dummy.

from typing import Optional
import c4d

doc: c4d.documents.BaseDocument  # The active document
op: Optional[c4d.BaseObject]  # The active object, None if unselected

def main() -> None:
    rd = doc.GetActiveRenderData()
    data = rd.GetData()
    # This bmp will server as a dummy bmp, the project being save below
    bmp = c4d.bitmaps.MultipassBitmap(int(rd[c4d.RDATA_XRES]), int(rd[c4d.RDATA_YRES]), c4d.COLORMODE_RGB)
    file = "D:\\temp\\toto_preview"
    
    # Define the render setting so the file will be saved on the harddrive.
    data.SetBool(c4d.RDATA_GLOBALSAVE, True);
    data.SetBool(c4d.RDATA_SAVEIMAGE, True);
    data.SetBool(c4d.RDATA_ALPHACHANNEL, False);
    data.SetInt32(c4d.RDATA_FORMATDEPTH, 0);
    # define the file Format to MP4
    data.SetInt32(c4d.RDATA_FORMAT, 1125); 
    data.SetFilename(c4d.RDATA_PATH, file);
    
    
    if bmp is None:
        raise RuntimeError("Failed to create the bitmap.")
    if c4d.documents.RenderDocument(doc, data, bmp, c4d.RENDERFLAGS_EXTERNAL | c4d.RENDERFLAGS_PREVIEWSETTINGS) != c4d.RENDERRESULT_OK:
        raise RuntimeError("Failed to render the temporary document.")

if __name__ == '__main__':
    main()

Another way would be to use MovieSaver. The problem is that the container defined by the Choose function is only readable in c++.

This will probably block you to define the codec parameters.

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

@manuel Thanks for that reply .

It's a cleary example that I can move on , a derivative of the qustion is if I cann't use ShowBitmap to show media file in Picture viewer , maybe I can get a range like 5f - 25f and render it and show it in sequence in Picture viewer in a folder ? Is it a stupid way or a no-exectable way :anguished:

Like this
for fame in [ range of render ( 5f to 25f ) ] :
1. render it
2. show it

@manuel
And a move on question with "preview" is can I set a parameter with plugin Make Preview , How can I set some settings for plugin like this and call it with this settings ?

fcc54871-338e-4981-8e25-dd0423cd3405-image.png

Hi @Dunhou, Manuel being in vaccation I will take over your topic.

@dunhou said in How to render a animation preview ?:

maybe I can get a range like 5f - 25f and render it and show it in sequence in Picture viewer in a folder ?

Sadly it is not possible to create folder in the Picture Viewer via the API. As Manuel said the only way to have image sequence displayed is to have a movie to be loaded in the Picture Viewer. Which is not possible in Python cinse the ShowBitmap function accepting a Filename is not available in Python. But this is something in our radar and I will look to support ShowBitmap with a filename in Python as soon as possible.

For the second part of your question about how to iterate a range of frames look at basedocument_animate_r13.py example, you will need to edit line 45 with a call to c4d.documents.RenderDocument. But in any case you will not be able to see them animated in the Picture Viewer.

@dunhou said in How to render a animation preview ?:

And a move on question with "preview" is can I set a parameter with plugin Make Preview , How can I set some settings for plugin like this and call it with this settings ?

When you call RenderDocument, the 2nd argument is the BaseContainer representing the c4d.documents.RenderData. This argument let you change the render setting as demonstrated in the render_current_frame_r12.py example. The class description of c4d.documents.RenderData enunerates the possible IDs and expected values to be defining in the render setting BaseContainer. So in your specif case this give us:

import c4d

def main() -> None:
    # Retrieves a copy of the current documents render settings
    data = doc.GetActiveRenderData().GetClone().GetData()
    if data is None:
        raise RuntimeError("Failed to retrieve the clone of the active Render Settings.")
        
    # Define the render setting so the file will be saved on the harddrive.
    data.SetInt32(c4d.RDATA_FRAMESEQUENCE, c4d.RDATA_FRAMESEQUENCE_PREVIEWRANGE)
    # To define with manually specified frame rande this can be defined like so:
    # data.SetInt32(c4d.RDATA_FRAMESEQUENCE, c4d.RDATA_FRAMESEQUENCE_MANUAL)
    # data.SetInt32(c4d.RDATA_FRAMEFROM, c4d.BaseTime(5, doc.GetFps()))
    # data.SetInt32(c4d.RDATA_FRAMETO, c4d.BaseTime(25, doc.GetFps()))    
    
    data.SetBool(c4d.RDATA_GLOBALSAVE, True);
    data.SetBool(c4d.RDATA_SAVEIMAGE, True);
    data.SetBool(c4d.RDATA_ALPHACHANNEL, False);
    data.SetInt32(c4d.RDATA_FORMATDEPTH, c4d.RDATA_FORMATDEPTH_8);
    
    # define the file Format to MP4
    data.SetInt32(c4d.RDATA_FORMAT, 1125); 
    file = r"/Users/m_adam/Desktop/temp_render"
    data.SetFilename(c4d.RDATA_PATH, file);
    
    # define the render resolution
    data.SetInt32(c4d.RDATA_XRES, 1280); 
    data.SetInt32(c4d.RDATA_YRES, 720); 
    
    # This bmp will server as a dummy bmp, the project being save below
    bmp = c4d.bitmaps.MultipassBitmap(int(data[c4d.RDATA_XRES]), int(data[c4d.RDATA_YRES]), c4d.COLORMODE_RGB)
    
    if bmp is None:
        raise RuntimeError("Failed to create the bitmap.")
    if c4d.documents.RenderDocument(doc, data, bmp, c4d.RENDERFLAGS_EXTERNAL | c4d.RENDERFLAGS_PREVIEWSETTINGS) != c4d.RENDERRESULT_OK:
        raise RuntimeError("Failed to render the temporary document.")

if __name__ == '__main__':
    main()

Cheers,
Maxime.

@m_adam
Thanks for that explain .

For the 1st issue , that is ok , I could open mp4 file in the disk instead of show in Picture View tight now .

And the 2rd one , I am so stupid that didn't realize that make preview plugin is a warp for viewport render . :anguished: Just wanna figure out how to set the dialog parameters .

But bt the way , animate example is a new one to me , thanks for piont out .

Cheers !

Hi @Dunhou you can't control the Make Preview plugin, you have to rewrite the function yourself with all the tips previously shared in this topic.

Cheers,
Maxime.