Skip to content

Instantly share code, notes, and snippets.

@aobond2
Last active April 12, 2024 09:29
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 aobond2/7001f51c16f1f7f033c71dbb94597283 to your computer and use it in GitHub Desktop.
Save aobond2/7001f51c16f1f7f033c71dbb94597283 to your computer and use it in GitHub Desktop.
Iterate on objects, create camera+lights, then make nice thumbnail render
import bpy
from mathutils import Vector
import math
import os
folderPath = "C:/Users/BondhanKimbalazani/Documents/ModularCharacter/GLB_Test/BodyA"
#folderPath = 'C:/Users/BondhanKimbalazani/Documents/ModularCharacter/GLB_Test/BodyA/TorsoUpper'
thumbnailFolder = 'C:/Users/BondhanKimbalazani/Pictures/Thumbnails/'
def importGLBs():
for root, dirs, files in os.walk(folderPath):
for file in files:
if file.endswith('.glb'):
filePath = os.path.join(root, file)
bpy.ops.import_scene.gltf(filepath=filePath)
def deleteArmature():
for armature in bpy.data.armatures:
bpy.data.armatures.remove(armature, do_unlink=True)
def iterateOnCollection():
for obj in bpy.context.scene.collection.objects:
bpy.ops.object.select_all(action='DESELECT')
print (obj.name)
obj.select_set(True)
renderSelected()
def createCamera(objectInput):
bpy.ops.object.camera_add(location=(0, 0, 10))
new_camera = bpy.context.object
new_camera.name = "MyNewCamera"
bpy.context.scene.camera = new_camera
# Camera settings
new_camera.data.lens = 35
new_camera.rotation_euler = (math.radians(75), 0, math.radians(-20))
frameCamera(objectInput)
# Move the camera along its local Z axis by .1 units
translation_vector = Vector((0.0, 0.0, 0.1))
new_camera.location += new_camera.matrix_world.to_3x3() @ translation_vector
def frameCamera(objectInput):
# Select objects that will be rendered
objectInput.select_set(True)
bpy.ops.view3d.camera_to_view_selected()
def set2SidedMat(objectInput):
material = objectInput.active_material
if material:
material.use_backface_culling = False
def deleteCamera():
camera_object = bpy.data.objects.get('MyNewCamera')
if camera_object is not None:
bpy.data.objects.remove(camera_object, do_unlink=True)
def createLights():
print ("Create lights")
# Back light
backLightData = bpy.data.lights.new(name="Sun", type='SUN')
backLightObject = bpy.data.objects.new(name="Sun", object_data=backLightData)
backLightObject.rotation_euler = (math.radians(-96), math.radians(182.75), math.radians(-38))
backLightObject.location = (10, 10, 10)
backLightObject.data.use_contact_shadow = True
backLightObject.data.energy = 30
# Main light
mainLightData = bpy.data.lights.new(name="MainLight", type='POINT')
mainLightObject = bpy.data.objects.new(name="MainLight", object_data=mainLightData)
mainLightObject.location = (-0.55, -0.67, 1.74)
mainLightObject.data.use_contact_shadow = True
mainLightObject.data.energy = 20
# Fill light
fillLightData = bpy.data.lights.new(name="FillLight", type='POINT')
fillLightObject = bpy.data.objects.new(name="FillLight", object_data=fillLightData)
fillLightObject.location = (0.31, -0.58, 1.3)
fillLightObject.data.use_contact_shadow = True
fillLightObject.data.energy = 5
# Link light objects to scene
bpy.context.collection.objects.link(backLightObject)
bpy.context.collection.objects.link(mainLightObject)
bpy.context.collection.objects.link(fillLightObject)
def deleteLights():
bpy.ops.object.select_by_type(type='LIGHT')
bpy.ops.object.delete()
def renderSelected():
# Create lights
createLights()
selected_objects = (bpy.context.selected_objects)[0]
set2SidedMat(selected_objects)
# Create camera, run this after selecting an object so we can frame it
createCamera(selected_objects)
if selected_objects:
# Set up rendering parameters
bpy.context.scene.render.engine = 'BLENDER_EEVEE'
bpy.context.scene.render.filepath = thumbnailFolder + selected_objects.name + '.png'
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.context.scene.render.resolution_x = 512
bpy.context.scene.render.resolution_y = 512
# Make only the selected objects visible
for obj in bpy.data.objects:
if not obj.type == 'LIGHT':
obj.hide_render = True
selected_objects.hide_render = False
print (selected_objects.name)
# Render the scene
bpy.ops.render.render(write_still=True)
deleteCamera()
deleteLights()
importGLBs()
deleteArmature()
iterateOnCollection()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment