Skip to content

Instantly share code, notes, and snippets.

@HsiaTsing
Created October 30, 2014 03:09
Show Gist options
  • Save HsiaTsing/84ee8042ce595f03e6ae to your computer and use it in GitHub Desktop.
Save HsiaTsing/84ee8042ce595f03e6ae to your computer and use it in GitHub Desktop.
This code use the Blender Python API to batch render 3D model to image file. It is only tested on Blender 2.64.
import os
import bpy
# put the location to the folder where the objs are located here in this fashion
path_to_obj_dir = os.path.join('D:\\', 'Workspace\\Data\\scapecomp')
# get list of all files in directory
file_list = sorted(os.listdir(path_to_obj_dir))
# get a list of files ending in 'obj'
obj_list = [item for item in file_list if item[-3:] == 'obj']
# loop through the strings in obj_list and add the files to the scene
for item in obj_list:
# full path and file name
path_to_file = os.path.join(path_to_obj_dir, item)
# import ply into the scene
bpy.ops.import_scene.obj(filepath = path_to_file)
# change suffix to ".png"
portion = os.path.splitext(item)
newname = portion[0]+".png"
# apply current material to this object
bpy.ops.object.material_slot_copy()
# shade mode
bpy.ops.object.shade_flat()
#bpy.ops.object.shade_smooth()
# translate and rotate
#bpy.ops.transform.rotate(value=(30,), axis=(0.0, 1.0, 0.0), constraint_axis=(False, False, False))
#bpy.ops.transform.resize(value=(5, 5, 5), constraint_axis=(False, False, False))
# new full path and filename
path_to_file2 = os.path.join(path_to_obj_dir, newname)
# set output render filepath
bpy.data.scenes['Scene'].render.filepath = path_to_file2
bpy.ops.render.render( write_still=True )
#bpy.ops.export_scene.obj(filepath = path_to_file2)
# delete current object from the scene
bpy.ops.object.delete()
@GundeepSinghGulati
Copy link

how we can adjust the camera for every object file and every object file have a different size so how's is it possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment