Friday, October 19, 2018

rendering - Efficient method to export a lot of pre-rendered sprites from 3D models


I have a lot of 3D Characters and I wish to use them in a 2.5D game as Sprites; in doing this I have to render them from a fixed camera angle and since each should have its own atlas file containing animation frames, it's going to be a very time consuming job! I was thinking to write a python script (models are in Blender format) and let it render the Sprites. Since I don't know Python do you think that it is efficient of me to learn the python first and write the script to do the job for me? Does anyone have a better idea?


In brief: I am looking for an efficient method to export pre-rendered sprites from 3D models considering that they all have the same armature with the same animations.



Answer



Learning Python is very easy I found a code in the internet and customized it to render from 8 different view port while fixing the lamp and camera location, the code also scales the objects to fit in 1 meter cube so the camera and lamp can be fixed correctly.


I succeed to write the following code in one night! Thanks to Python! and Blender API Documentation. Hope this help some body. I am working on copying armatures from a .blend file into other characters meshes and rendering different POS from the 8 corresponded viewport to use them in my game as sprite sources.



 import bpy
cam = bpy.data.objects["Camera"]
cam.rotation_mode = 'XYZ'
cam.location = (1.5,0,3)
cam.rotation_euler = (0.5236,0,1.5707)
alamp = bpy.data.objects["Lamp"]
alamp.location = (1,0,1)
alamp.rotation = (0,0,0)
meshObj = bpy.data.objects["boss"]
meshObj.rotation_mode = 'XYZ'

meshObj.rotation_euler = (0,0,0)
d = meshObj.dimensions

# Finding maximum dim of the object
objectScale = 1
if d[1] >= d[2] and d[1] >= d[0]:
objectScale = 1 / d[1]
elif d[2] >= d[1] and d[2] >= d[0]:
objectScale = 1 / d[2]
elif d[0] >= d[1] and d[0] >= d[2]:

objectScale = 1 / d[0]

# Normalize object since some of them are bigger
print("Scaling ",objectScale)
meshObj.scale = (objectScale,objectScale,objectScale)

# Rotating X axis if object is X -Z Y Convert to X Y Z
rot = meshObj.rotation_euler
if d[1] > d[2]:
print("Rotating X by 90 Degree")

rot[0] = radians(90)
meshObj.rotation_euler = rot;
# Initiating the Rotation/Render Loop
rot[2] = 0 # Start from Z Rotation = 0
meshObj.rotation_euler = rot;
for x in range(1,8):
angle = (x-1)*0.7854
rot[2] = angle
meshObj.rotation_euler = rot;
bpy.context.scene.render.filepath = "/Users/iman/Documents/Render/Boss00%d.png" % (x)

bpy.ops.render.render(write_still=True, use_viewport=True, scene="Camera")

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...