I have built a script that follows a set of instructions, getting lots of textures. The script is filling the sides of a cube with textures, with 2 methods, map4 cube faces and map1cubeface. It first looks for inside the directories of a folder, jpg images, and then allocate them to the faces, then unwrap, then render. Hope you understand.
Here you go:
import bpy
import os
from math import radians
bpy.ops.object.select_all(action='DESELECT')
# retrieve the active object
#bpy.ops.object.mode_set(mode = 'OBJECT')
def map_4_cube_faces():
#call context into the object to be worked with
bpy.context.scene.objects.active = bpy.data.objects["cube"]
#attribute 4 images to 4 faces that were tracked
for number in range(1, 5):
#enter the context of activated material
bpy.context.object.active_material_index = number
#attribute image to the material image_texture node
bpy.data.materials[number-1].node_tree.nodes[1].image = bpy.data.images[str(number) + ".jpg"]
#enter edit mode context
bpy.ops.object.editmode_toggle()
#select face that is assigned by the material
bpy.ops.object.material_slot_select()
#uv-unwrap image to the face
bpy.ops.uv.unwrap(method='ANGLE_BASED', fill_holes=True, correct_aspect=True, use_subsurf_data=False, margin=0.001)
#store last context to var last_area
last_area = bpy.context.area.type
#change context to UV Editing context
bpy.context.area.type = 'IMAGE_EDITOR'
bpy.ops.image.reload()
#here it should rotate the texture by 180º, trial 1 o/
bpy.ops.transform.rotate(value=(radians(180)), axis=(0, 0, -1), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)
#returns to the last context stored on the var last area
bpy.context.area.type = last_area
#reset uv
bpy.ops.uv.reset()
#go back into object mode
bpy.ops.object.editmode_toggle()
bpy.ops.object.select_all(action='DESELECT')
def map_1_cube_face():
#call context into the object to be worked with
bpy.context.scene.objects.active = bpy.data.objects["cube"]
for number in range(1, 5):
#enter the context of activated material
bpy.context.object.active_material_index=number
#attribute image to the material image_texture node
bpy.data.materials[number-1].node_tree.nodes[1].image = bpy.data.images["1.jpg"]
#enter edit mode context
bpy.ops.object.editmode_toggle()
#select face that is assigned by the material
bpy.ops.object.material_slot_select()
#uv-unwrap image to the face
bpy.ops.uv.unwrap(method='ANGLE_BASED', fill_holes=True, correct_aspect=True, use_subsurf_data=False, margin=0.001)
#store last context to var last_area
last_area = bpy.context.area.type
#change context to UV Editing context
bpy.context.area.type = 'IMAGE_EDITOR'
bpy.ops.image.reload()
#here it should rotate the texture by 180º, but it doesn't
bpy.ops.transform.rotate(value=(radians(180)), axis=(0, 0, -1), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)
#returns to the last context stored on the var last area
bpy.context.area.type = last_area
#reset uv
bpy.ops.uv.reset()
#go back into object mode
bpy.ops.object.editmode_toggle()
bpy.ops.object.select_all(action='DESELECT')
def set_top_color_render(type,file_path):
sceneKey = bpy.data.scenes.keys()[0]
for i in range (0,7):
if i == 0:
bpy.data.objects["top"].active_material = bpy.data.materials["yellow"]
if i == 1:
bpy.data.objects["top"].active_material = bpy.data.materials["blue"]
if i == 2:
bpy.data.objects["top"].active_material = bpy.data.materials["white"]
if i == 3:
bpy.data.objects["top"].active_material = bpy.data.materials["orange"]
if i == 4:
bpy.data.objects["top"].active_material = bpy.data.materials["black"]
if i == 5:
bpy.data.objects["top"].active_material = bpy.data.materials["purple"]
if i == 6:
bpy.data.objects["top"].active_material = bpy.data.materials["red"]
c=0
for obj in bpy.data.objects:
# Find cameras that match cameraNames
if ( obj.type =='CAMERA') and ( obj.name == 'cam1' or obj.name == 'cam2') and type=="0":
#print("Rendering scene["+sceneKey+"] with Camera["+obj.name+"]")
# Set Scenes camera and output filename
bpy.data.scenes[sceneKey].camera = obj
bpy.data.scenes[sceneKey].render.image_settings.file_format = 'JPEG'
bpy.data.scenes[sceneKey].render.filepath = file_path+'/renders/topcolor'+str(i)+'-'+'camera_' + str(c)
# Render Scene and store the scene
bpy.ops.render.render( write_still=True )
c = c + 1
if ( obj.type =='CAMERA') and ( obj.name == 'cam1') and type=="1":
#print("Rendering scene["+sceneKey+"] with Camera["+obj.name+"]")
# Set Scenes camera and output filename
bpy.data.scenes[sceneKey].camera = obj
bpy.data.scenes[sceneKey].render.image_settings.file_format = 'JPEG'
bpy.data.scenes[sceneKey].render.filepath = file_path+'/renders/topcolor'+str(i)
# Render Scene and store the scene
bpy.ops.render.render( write_still=True )
c = c + 1
path="fill here the full path of your project"
for root, dirs, files in os.walk(path):
if (len(dirs)!=0):
for directory in dirs:
if not "renders" in directory:
if "tp1" in directory:
file_path = os.path.join(root,directory)
for r,d,f in os.walk(file_path):
for im in f:
if not ".DS_Store" in im and not "top" in im:
bpy.ops.image.open(filepath=format(file_path)+"/"+im)
#map faces
map_4_cube_faces()
#set top color and render
set_top_color_render("0",file_path)
else:
file_path = os.path.join(root,directory)
for r,d,f in os.walk(file_path):
for im in f:
if not ".DS_Store" in im and not "top" in im:
bpy.ops.image.open(filepath=format(file_path)+"/"+im)
#map faces
map_1_cube_face()
#set top color and render
set_top_color_render("1", file_path)
for r,d,f in os.walk(file_path):
for im in f:
if not ".DS_Store" in im and not "top" in im:
try:
bpy.data.images[im].user_clear()
can_continue = True
except:
print ("tried user_clear() for "+im+" with no success")
can_continue = False
if can_continue == True:
try:
bpy.data.images.remove(bpy.data.images[im])
except:
print ("tried remove() for "+im+" with no success")