Skip to content

Instantly share code, notes, and snippets.

@gifguide2code
Created July 1, 2018 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gifguide2code/4c307edaed30ed646e0e129aa1334398 to your computer and use it in GitHub Desktop.
Save gifguide2code/4c307edaed30ed646e0e129aa1334398 to your computer and use it in GitHub Desktop.
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