用于替换多个图像纹理节点中图像的 Blender 脚本

Blender script to replace images in multiple image texture nodes

提问人:Mauricio Jimenez 提问时间:11/3/2023 最后编辑:Mauricio Jimenez 更新时间:11/4/2023 访问量:30

问:

我是新来的,对不起,如果这没有正确发布,我有这个 Blender 3.6 脚本

import bpy
import os

# Folder containing the images to replace
image_folder = "D:/Pizarro 3d/PRUBAS AUTOMATIZACION BLENDER-20231102T215909Z-001/PRUBAS AUTOMATIZACION BLENDER/IMAGENES"
blend_file = "D:/Pizarro 3d/PRUBAS AUTOMATIZACION BLENDER-20231102T215909Z-001/PRUBAS AUTOMATIZACION BLENDER/E-1-CLASE-0 - copia.blend"
blend_save_folder = "D:/Pizarro 3d/PRUBAS AUTOMATIZACION BLENDER-20231102T215909Z-001/PRUBAS AUTOMATIZACION BLENDER/Blend files"
render_output_folder = "D:/Pizarro 3d/PRUBAS AUTOMATIZACION BLENDER-20231102T215909Z-001/PRUBAS AUTOMATIZACION BLENDER/RENDER"

# Name of the image data block to replace in the Blender file
image_to_replace = "Image"

# Function to replace image data block and assign to specific texture nodes
def process_image(image_path):
    image_path_full = os.path.join(image_folder, image_path)
    
    bpy.ops.wm.open_mainfile(filepath=blend_file)

    # Find and delete the original image data block
    for img in bpy.data.images:
        if img.name == image_to_replace:
            bpy.data.images.remove(img)

    # Load the new image as a new image data block
    new_image = bpy.data.images.load(image_path_full)
    new_image.name = image_to_replace  # Set the new image's name to the original one

    # Find materials and update specific texture nodes
    for material in bpy.data.materials:
        if material.use_nodes:
            for node in material.node_tree.nodes:
                if node.type == 'TEX_IMAGE' and 'Replace' in getattr(node, "label", ""):
                    if node.image and node.image.name == image_to_replace:
                        node.image = new_image

                        # Re-establish node connections to apply the new image
                        for link in material.node_tree.links:
                            if link.to_node == node and link.to_socket.identifier == 'Color':
                                material.node_tree.links.remove(link)
                                material.node_tree.links.new(node.outputs["Color"], link.to_socket)

    # Save the modified blend file to a different folder
    blend_file_name = os.path.splitext(os.path.basename(blend_file))[0]
    image_name = os.path.splitext(image_path)[0]
    modified_blend_path = os.path.join(blend_save_folder, f"{blend_file_name}_{image_name}.blend")
    bpy.ops.wm.save_mainfile(filepath=modified_blend_path)

    # Set render output path
    render_output_path = os.path.join(render_output_folder, f"{blend_file_name}_{image_name}.png")
    bpy.context.scene.render.image_settings.file_format = 'PNG'
    bpy.context.scene.render.filepath = render_output_path
    bpy.context.scene.render.image_settings.color_mode = 'RGB'

    # Render the scene (a single frame)
    bpy.context.scene.frame_set(1)  # Set frame to render (frame 1)
    bpy.ops.render.render(write_still=True)

# Get a list of images in the folder
image_files = os.listdir(image_folder)

# Process each image in the folder
for image_file in image_files:
    if image_file.endswith(".png") or image_file.endswith(".jpg"):
        process_image(image_file)

我正在尝试用节点标签“替换”替换图像节点中的图像 将文件夹中的图像放在那里,然后保存一个混合文件,然后将文件渲染到一个单独的文件夹中 该脚本正在替换和保存混合文件,但它没有将图像放在相应的图像纹理节点中, 它们显示从图像文件夹加载的空纹理应该一个接一个地转到这里

我正在上传其渲染方式以及我期望渲染方式的图像,这就是它的渲染方式, 这就是我所期望的

图像纹理节点用于 3 种不同的材质,唯一的区别是纹理的比例

我尝试解压缩文件并替换它,但我真的不知道问题是什么

python 图像 替换 纹理 搅拌器

评论


答: 暂无答案