Forum: Poser 13


Subject: Poser 13: Python script to batch render MP4s?

tnpir4002 opened this issue on Sep 30, 2024 ยท 10 posts


tnpir4002 posted Sun, 27 October 2024 at 11:33 AM

I didn't crack the code for MP4s, but thanks to ChatGPT I did get this to work exactly as I need it to with Poser 12 for PNG sequences. Turns out this is a better solution for what I'm doing anyway, since the Preview engine can't render proper alpha channels in Silhouette mode. AfterEffects is able to import the PNGs as a PNG sequence and treat them exactly as they would an MP4, which for my purposes works precisely the way I need.

Code:

import os

import poser

import time


# Directory containing the .pz3 files

selected_directory = r"C:\Path\to\Your\Files"


# Find and process all .pz3 files in the directory

pz3_files = [f for f in os.listdir(selected_directory) if f.lower().endswith('.pz3')]


if not pz3_files:

    print("No PZ3 files found in the directory.")

else:

    for pz3_file in pz3_files:

        pz3_path = os.path.join(selected_directory, pz3_file)

        try:

            # Open the PZ3 file

            print(f"Attempting to open {pz3_path}")

            poser.OpenDocument(pz3_path)

            time.sleep(2)  # Wait to ensure document loads fully


            # Access the current scene

            scene = poser.Scene()

            if scene is None:

                print(f"Warning: No scene found after opening {pz3_path}")

                continue


            print(f"Successfully accessed {pz3_path}")


            # Set render engine to Preview using SetCurrentRenderEngine

            scene.SetCurrentRenderEngine(poser.kRenderEngineCodePREVIEW)

            print("Render engine set to Preview.")


            # Set display style to Texture Shaded

            scene.SetDisplayStyle(10)  # 10 is Texture Shaded

            print(f"Display style set to Texture Shaded for {pz3_file}.")


            # Determine output path and base filename

            base_name, _ = os.path.splitext(pz3_file)

            output_folder = os.path.dirname(pz3_path)


            # Render each frame in the animation sequence to PNG

            num_frames = scene.NumFrames()

            for frame in range(num_frames):

                scene.SetFrame(frame)  # Set the current frame

                output_filename = f"{base_name}_{frame+1:04d}.png"  # e.g., filename_0001.png

                output_path = os.path.join(output_folder, output_filename)


                # Render the frame to the specified output path

                scene.Render()

                scene.SaveImage("png", output_path)

                print(f"Rendered frame {frame+1} to {output_path}")


            # Close the document without saving

            poser.CloseDocument()

            print(f"Successfully closed {pz3_file}\n")


        except AttributeError as attr_err:

            print(f"Attribute error while processing {pz3_file}: {attr_err}")

        except Exception as e:

            print(f"Unexpected error while processing {pz3_file}: {e}")