Skip to content

Instantly share code, notes, and snippets.

@lukebitts
Created October 9, 2014 16:54
Show Gist options
  • Save lukebitts/8acf8f482a0982b8f1f2 to your computer and use it in GitHub Desktop.
Save lukebitts/8acf8f482a0982b8f1f2 to your computer and use it in GitHub Desktop.
Blender scene to text file exporter
import bpy
import bpy_extras
from math import degrees
from mathutils import Vector
from mathutils import Quaternion
import os
PATH = bpy.path.abspath('//')+"scene_test2/"
print("\nSTARTING SCRIPT...")
files = os.listdir(PATH)
for file in files:
if file.split('.')[-1] == "png" or file.split('.')[-1] == "obj" or file.split('.')[-1] == "l3d" or file.split('.')[-1] == "lif" or file.split('.')[-1] == "lsc":
os.remove(PATH+file)
def parse_customprops(object):
ret = ""
for prop in object.keys():
if prop == "solid":
ret += "solid:" + object["solid"]
return ret
def parse_transform(object):
bpy.context.scene.update()
mm = bpy_extras.io_utils.axis_conversion(from_forward='Y', from_up='Z', to_forward='-Z', to_up='Y')
om = object.matrix_world.to_3x3()
t = mm * om
v = t.to_euler('XYZ')
print('pos:(%s, %s, %s)' % (obj.matrix_world.translation.x, obj.matrix_world.translation.z, -obj.matrix_world.translation.y))
print('rot:(%s, %s, %s)' % (degrees(v.x) + 90.0, degrees(v.y), degrees(v.z)))
ret = "position:"+str(object.location.x)+","+str(object.location.z)+","+str(-object.location.y)
ret += "|"
ret += "rotation:"
ret += str(degrees(v.x) + 90.0)+","
ret += str(degrees(v.y))+","
ret += str(degrees(v.z))+","
ret += str(object.rotation_quaternion.w)
ret += "|"
ret += "scale:"+str(object.scale.x)+","+str(object.scale.y)+","+str(object.scale.z)
return ret
def parse_materials(object):
ret = "materials:"+str(len(object.material_slots)) + "|"
for mat in object.material_slots:
for tex in mat.material.texture_slots:
if tex != None:
name = os.path.basename(tex.texture.image.filepath).split('.')[0]+".png"
#resave the image as a png file
scene=bpy.context.scene
scene.render.image_settings.file_format='PNG'
if not os.path.isfile(PATH+name):
tex.texture.image.save_render(PATH+name)
ret += "texture:"+name+";"
return ret[:-1]
def parse_mesh(object):
#export the obj file with the mesh
#todo: Put the export part in another function
scene = bpy.context.scene
name = object.name.split('.')[0]+".obj"
bpy.ops.object.select_all(action='DESELECT')
scene.objects.active = object
object.select = True
old_location = Vector([object.location.x,object.location.y,object.location.z])
old_rotation = Vector([object.rotation_euler.x,object.rotation_euler.y,object.rotation_euler.z])
old_scale = Vector([object.scale.x,object.scale.y,object.scale.z])
object.location = Vector([0,0,0])
object.rotation_euler = Vector([0,0,0])
object.scale = Vector([1,1,1])
if not os.path.isfile(PATH+name):
bpy.ops.export_scene.obj(filepath=PATH+name, group_by_material=True, use_selection=True, use_uvs=True, use_normals=True, use_materials=False)
object.location = old_location
object.rotation_euler = old_rotation
object.scale = old_scale
object.select = False
ret = "mesh"
ret += "|"
ret += name
ret += "|"
ret += parse_transform(object)
props = parse_customprops(object)
materials = parse_materials(object)
if props != "":
ret += "|" + props
ret += "|"
if materials[-1] == "|":
ret += materials[:-1]
else:
ret += materials
return ret
def parse_lamp(object):
ret = "lamp"
ret += "|"
ret += parse_transform(object)
props = parse_customprops(object)
if props != "":
ret += "|" + props
return ret
scene_file = ""
for obj in bpy.context.scene.objects:
if(obj.type == "MESH"):
scene_file += parse_mesh(obj) + "\n"
elif(obj.type == "LAMP"):
scene_file += parse_lamp(obj) + "\n"
with open(PATH+"scene.lsc", "w") as text_file:
text_file.write(scene_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment