1
$\begingroup$

Sorry if the title is unclear, I couldn't think how else to word it.
What I'm looking to do is create a book with turning pages, where each page uses an image from a specified directory, so the ideal end output would create a plane for each item in the folder and assign the next image sequentially to that plane.
First question on here and not too familiar with Python, so any help/input would be much appreciated! Thanks

$\endgroup$
1
  • $\begingroup$ The texture coordinates don’t actually need to be changed at all, just the materials. In fact, depending on what you’re doing, it might be more efficient to just run the same two-page animation over and over via an NLA strip, but have the material sequence through all the images one by one for each page turn (and one ahead for the page behind it). $\endgroup$
    – TheLabCat
    Commented Dec 31, 2022 at 5:11

1 Answer 1

0
$\begingroup$

Looping across the list of filenames using this answer to create individual planes like so;

import bpy
import os

def create_page(filename, page_num):
    bpy.ops.mesh.primitive_plane_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
    plane_object = bpy.context.view_layer.objects.active
    plane_object.name = f"page-{page_num}"

    page_material = bpy.data.materials.new(name='page-material-{page_number}')
    page_material.use_nodes = True

    principled_bsdf_node = page_material.node_tree.nodes["Principled BSDF"]
    image_texture_node = page_material.node_tree.nodes.new('ShaderNodeTexImage')
    image_texture_node.image = bpy.data.images.load(filename)

    page_material.node_tree.links.new(principled_bsdf_node.inputs['Base Color'], image_texture_node.outputs['Color'])

    # Assign it to object
    if plane_object.data.materials:
        plane_object.data.materials[0] = page_material
    else:
        plane_object.data.materials.append(page_material)

    material_output_node = page_material.node_tree.nodes.get('Material Output')

# Replace <SPECIFIED DIRECTORY> with location of image files
image_file_directory = <SPECIFIED DIRECTORY>
for page_num, image_file_name in enumerate(os.listdir(image_file_directory)):
    # It may be necessary to parse the filenames if there are files in the specified
    # directory which are not images or should be otherwise excluded
    create_page(image_file_name, page_num)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .