Blender automation script, unattended batch rendering/rendering video

 Rendering video is a very time-consuming project. If multiple videos need to be rendered or each video needs to switch between different textures, colors, etc., the workload will be even more outrageous, so we have to use scripts to automate.

Blender's script is written in Python, which is much more convenient than PS's js. Then download a set of API corresponding to the version of Blender, and you can have the code prompt function.

Blender API download, select the corresponding version of Blender:

Releases · nutti/fake-bpy-module · GitHub

 For example, I downloaded Blender 3.4, unzip the folder fake_bpy_modules_3.4-20230117 after downloading, create a Python script in this folder, and use VS Code to open the entire folder of fake_bpy_modules_3.4-20230117. VS Code needs to install the Python plug-in to support code prompts.

First create a new model in Blender, the Blender interface is as shown in the figure:

Take dynamically modifying the texture of a model through a script as an example. You can access all the nodes of the Scene panel through the script Blender API bpy.context.scene. For example, if we want to modify the texture of the "monkey head" model, we first need to get the "monkey head" node, then get the shader named "material", and then get the texture node in the shader, and re-specify the texture file path:

1. Obtain the node named "monkey head" in the scene: tNode:bpy.types.Object = bpy.context.scene.objects["monkey head"]

2. Get the material ball of the node: mat:bpy.types.MaterialSlot = tNode.material_slots["material"]

3. Get the node named "Image Texture" in the shader: texNode:bpy.types.ShaderNodeTexImage = mat.material.node_tree.nodes["Image Texture"]

4. Modify the texture file: texNode.image.filepath = "newTexture.png"

5. After modifying the texture, call texNode.image.update() to refresh the rendering

 

Use the same model, different textures, each texture to render a picture for the model:

1. First put the multiple textures that need to be dynamically switched into the specified folder, so that the program can get all the textures under the folder;

2. Traverse all the textures, set the current texture on the model shader according to the above process, and then render the picture. In this way, renderings of different textures can be obtained;

3. Execute bpy.ops.render.render() to start rendering the image;

The method of rendering video is the same. In the following panel of Blender, you can select the File Format property to set whether to render video or picture.

 You can judge the File Format currently selected in Blender in the code, if it is a video, turn on animation rendering, otherwise render it as a picture.

code show as below:

import bpy
import os
import glob
import os.path

texFolder = "C:/Users/Administrator/Desktop/Models/icons"

outputPrefixName = "Image_"
outputVideoPrefixName = "Video_"
tNode:bpy.types.Object = bpy.context.scene.objects["猴头"]
if not tNode:
    print("找到目标节点")
else:
    mat:bpy.types.MaterialSlot = tNode.material_slots["材质"]
    texNode:bpy.types.ShaderNodeTexImage = mat.material.node_tree.nodes["图像纹理"]

    # colTex = mat.material.node_tree.nodes["Color"]
    try : 
        texNode.image.unpack()
    except Exception as e:
        print("")


    # 根据给定贴图目录批量渲染

    texFiles:list[str] = glob.glob(os.path.join(texFolder, "*.png"))

    for texPath in texFiles:
        fileName = os.path.basename(texPath)
        texNode.image.filepath = texPath
        texNode.image.update()

        fileNameNoExt = os.path.splitext(fileName)[0]
        if bpy.context.scene.render.image_settings.file_format.startswith("AVI") or bpy.context.scene.render.image_settings.file_format == "FFMPEG":
            # 渲染avi视频
            bpy.context.scene.render.filepath = "//OutputVideos/"+outputVideoPrefixName + fileNameNoExt
            bpy.context.scene.render.use_overwrite = False
            bpy.context.view_layer.update()
            bpy.ops.render.render(animation=True)
        else:
            # 渲染图片
            bpy.context.scene.render.filepath = "//OutputImages/"+outputPrefixName + fileNameNoExt
            # bpy.context.scene.render.image_settings.file_format = "PNG"
            bpy.context.scene.render.use_overwrite = False
            bpy.context.view_layer.update()
            bpy.ops.render.render(write_still=True)

Run the script:

1. Switch to the script bar in Blender;

2. Select to open the written script file;

3. Click the Run button to execute the script;

 After the execution is completed, the rendered image/video will be output in the directory where the model is located:

Guess you like

Origin blog.csdn.net/final5788/article/details/131835359