2
$\begingroup$

I am working on my own game engine for Android with OpenGL and C++, now if I don't want to create an editor to save time and effort, I use blender to create all models so can I use blender as levels editor where I can append models and put them all, make instances, adjust lights then export them as level file like xml or json where every item has its name and matrix? for example:

<scene id="level1">
    <mesh name="tree" matrix=" 1 0 2.03 ....."/>
    <mesh name="rock" matrix=" 5.231 0 2.03 ....."/>
    <mesh name="grass" count="50" matrix=" 1 0 2.03 .. all instances matrices... 10.22"/>
    <light position="1,50,3" color="..." intensity="2.0"/>
</scene>

I am not a python programmer so I can't do it by myself so if there is an addon or a script which can do something like that ?

$\endgroup$
1
$\begingroup$

After some researches and reading on Blender API I created that script and it worked perfect, so I created my own exporter :), so happy

import bpy


class ExportSceneToBoddooo(bpy.types.Operator):
"""Test exporter which just writes hello world"""
bl_idname = "export.scene_to_boddooo"
bl_label = "Export to Boddooo"

filepath = bpy.props.StringProperty(subtype="FILE_PATH")

@classmethod
def poll(cls, context):
    return context.object is not None

def execute(self, context):
    file = open(self.filepath, 'w')

    file.write("<Level> \n")
    file.write("    <Meshes> \n")        
    for ob in bpy.data.objects:
        locX=str(ob.location.x)
        locY=str(ob.location.y)
        locZ=str(ob.location.z)
        rotX=str(ob.rotation_quaternion.x)
        rotY=str(ob.rotation_quaternion.y)
        rotZ=str(ob.rotation_quaternion.z)            
        rotW=str(ob.rotation_quaternion.z)                        
        sclX=str(ob.scale.x)
        sclY=str(ob.scale.y)
        sclZ=str(ob.scale.z)                                  
        if ob.type=="MESH":
            file.write("        <" +ob.type + " name='" + ob.name + "' location='"+locX +" " + locY+" " + locZ+"' rotation='"+rotX +" " + rotY+" " + rotZ+" " + rotW+"' scale='"+sclX +" " + sclY+" " + sclZ+"'/>\n")
    file.write("    </Meshes> \n")   
    file.write("    <Lamps> \n")                     
    for ob in bpy.data.lamps:
        clrX=str(ob.color.r)
        clrY=str(ob.color.g)
        clrZ=str(ob.color.b)
        energy=str(ob.energy)
        file.write("        <" +ob.type + " name='" + ob.name + "' location='"+locX +" " + locY+" " + locZ+"' rotation='"+rotX +" " + rotY+" " + rotZ+" " + rotW+"' color='"+clrX +" " + clrY+" " + clrZ+"' intensity='"+energy+"' />\n")
    file.write("    </Lamps> \n")
    file.write("</Level>")         
    return {'FINISHED'}

def invoke(self, context, event):
    context.window_manager.fileselect_add(self)
    return {'RUNNING_MODAL'}


# Only needed if you want to add into a dynamic menu
def menu_func(self, context):
    self.layout.operator_context = 'INVOKE_DEFAULT'
    self.layout.operator(ExportSceneToBoddooo.bl_idname, text="Boddooo Exporter")

# Register and add to the file selector
bpy.utils.register_class(ExportSceneToBoddooo)
bpy.types.INFO_MT_file_export.append(menu_func)


# test call
bpy.ops.export.some_data('INVOKE_DEFAULT')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.