Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This Blender function will go through a folder, pull all the jpg's inside, and map them to planes in Blender along the Z axis.
import bpy
import os
def imageLoad(imgpath):
z=0
files = os.listdir(imgpath)
#Get the names of the files in the provided folder
for x in range(len(files)):
imagename = files[x][0:20]
if files[x].endswith('.jpg'):
bpy.ops.mesh.primitive_plane_add(location=(0,0,z))
bpy.ops.transform.resize(value=(1,1,1))
#Resize to fit the scene
bpy.data.objects['Plane'].name = imagename
#Rename the plane to the 1st parameter
mat_name = imagename
mat = bpy.data.materials.new(mat_name)
#Create a material with that name
bpy.data.materials[mat_name].use_nodes = True
bpy.data.materials[mat_name].node_tree.nodes.new(type='ShaderNodeBsdfDiffuse')
inp = bpy.data.materials[mat_name].node_tree.nodes['Material Output'].inputs['Surface']
outp = bpy.data.materials[mat_name].node_tree.nodes['Diffuse BSDF'].outputs['BSDF']
bpy.data.materials[mat_name].node_tree.links.new(inp,outp)
#Connect the diffuse node to the surface of your plane
new_img = bpy.data.images.load(filepath = imgpath + files[x])
#Load the image from the file
bpy.data.materials[mat_name].node_tree.nodes.new(type='ShaderNodeTexImage')
inp = bpy.data.materials[mat_name].node_tree.nodes['Diffuse BSDF'].inputs['Color']
outp = bpy.data.materials[mat_name].node_tree.nodes['Image Texture'].outputs['Color']
bpy.data.materials[mat_name].node_tree.links.new(inp,outp)
#Connect that image texture node to the diffuse material
bpy.data.materials[mat_name].node_tree.nodes["Image Texture"].image = new_img
bpy.data.objects[imagename].active_material = bpy.data.materials[mat_name]
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects[imagename].select = True
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action= 'DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.uv.smart_project()
#Use UV Smart Unwrap
z-=5
imgpathfull = "C:\\Users\\Desktop\\"
imageLoad(imgpathfull)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.