Blender 2.5 Exporter

The place for artists, modellers, level designers et al to discuss their approaches for creating content for OGRE.
Tony Richards
Halfling
Posts: 86
Joined: Mon Nov 10, 2008 9:32 pm
Location: New York, NY, USA
x 1

Blender 2.5 Exporter

Post by Tony Richards »

Hopefully lf3thn4d does not take offense, but I've started a complete rewrite of the exporters, targeting Blender 2.5x.

The main reason for the rewrite is due to the new Blender Python API. The main reason for pushing forward with it now as opposed to waiting for lf3thn4d is because I need it now. I'll roll with the punches as the Blender API changes.

Just a few minutes ago I made my first Mercurial commit to the BlendOGRE project on Google Code.

This first version is not production quality and I've only tested it against the default "cube" mesh. It exports static geometry but not uv maps.

Using Google Code and Mercurial, my hopes is that it can become a community project and others with more experience in the subject (lf3thn4d!) can help when they have time.

Feedback and suggestions are warmly welcome.
Image IndieZen.org - Open source game development tools, game engine, and MMO frameworks
lucas92
Gnoblar
Posts: 11
Joined: Sat Aug 14, 2010 7:47 pm

Re: Blender 2.5 Exporter

Post by lucas92 »

I'm looking forward it. :)
User avatar
lf3thn4d
Orc
Posts: 478
Joined: Mon Apr 10, 2006 9:12 pm
x 12

Re: Blender 2.5 Exporter

Post by lf3thn4d »

Awesome. I'm not offended in any means. I think this is a good move in all nature of a community driven project. We could bring it in as an official exporter if it's good or use some of your code when working on the official version. :) Either way, good effort. Will have a look into it when I have the time. My only reason for not starting right now is due to blender's iffy python script API status.
drr00t
Gnoblar
Posts: 6
Joined: Thu Apr 24, 2008 6:06 pm

Re: Blender 2.5 Exporter

Post by drr00t »

Hi,

Would be nice have an exporter for Blender 2.5, but i think that start from begin is lot and hard work. What you think use or contribute to add support for Ogre on this exporter: http://code.google.com/p/tubras/ now irrb works very nice for Irrlicht.

Just my opnion.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: Blender 2.5 Exporter

Post by jacmoe »

I would very much prefer an in-house Blender exporter for Ogre.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
spacegaier
OGRE Team Member
OGRE Team Member
Posts: 4304
Joined: Mon Feb 04, 2008 2:02 pm
Location: Germany
x 136

Re: Blender 2.5 Exporter

Post by spacegaier »

jacmoe wrote:I would very much prefer an in-house Blender exporter for Ogre.
+1

Blender is one (if not the) most used tools in conjunction with Ogre. We need to have an own exporter, despite all the work that is connected with that.
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
ecoumans
Kobold
Posts: 39
Joined: Mon Sep 12, 2005 10:07 am
Location: San Francisco
x 4

Re: Blender 2.5 Exporter

Post by ecoumans »

If you need any data from Blender 2.5, just take the .blend file and extract the data using the small and open source bParse C++ library (under the permissive Zlib license)

You can see its use as part of OgreKit, extracting scenes, lights, cameras, triangle meshes, UV coordinates, textures, skinned animation, physics information, logic bricks and so on.

If you haven't tried it yet, see http://code.google.com/p/gamekit/ or this Forum topic http://www.ogre3d.org/forums/viewtopic.php?f=11&t=54431, there are a few precompiled Windows demos, and complete source code that compiles for Windows, Linux, Mac OSX and iPhone.
Cheers,
Erwin
Tony Richards
Halfling
Posts: 86
Joined: Mon Nov 10, 2008 9:32 pm
Location: New York, NY, USA
x 1

Re: Blender 2.5 Exporter

Post by Tony Richards »

@Erwin - I had definitely considered using gamekit code. I played around with it for a bit, but our coding styles are radically different making it difficult for me to follow the code.

Another two factors came to play as well... I recently switched jobs and the new job is going to eventually require me to be a heavy hitter in Python, so I need the practice with Python. The other factor was that I needed to make some custom scripts for Blender to automate some things anyway, so I'm quickly learning a lot about the new bpy API.

Side note, I'm actually quite enjoying the new API.... anyway, I'm digging back into the code in a few minutes. I'll push some more changes sometime this weekend.
Image IndieZen.org - Open source game development tools, game engine, and MMO frameworks
Tony Richards
Halfling
Posts: 86
Joined: Mon Nov 10, 2008 9:32 pm
Location: New York, NY, USA
x 1

Re: Blender 2.5 Exporter

Post by Tony Richards »

Wow, that was a little easier than I expected.

texcoord elements are being exported now.

I've pushed the latest code to the Google Code hg repo.

For the most part, simple static meshes should export. If the mesh has multiple UV maps, all of them are exported.

As with last time, I'm only doing a cursory test, so quite likely there are bugs. Feel free to check it out since it might actually be useful.

Note: If you open / save a .blend with a newer version of Blender, make sure you keep a backup copy just in case you want to use it with an older version... the file format is not backwards compatible.
Image IndieZen.org - Open source game development tools, game engine, and MMO frameworks
User avatar
PolyVox
OGRE Contributor
OGRE Contributor
Posts: 1316
Joined: Tue Nov 21, 2006 11:28 am
Location: Groningen, The Netherlands
x 18

Re: Blender 2.5 Exporter

Post by PolyVox »

Great, this looks useful! But how do I use it? I got the source from Mercurial and placed the 'ogreio' folder in 'Blender/2.53/Python/lib'. But this was only a guess. I started Blender and didn't see anything Ogre related in the export menu.

Sorry, I'm not that experienced with Blender...
Tony Richards
Halfling
Posts: 86
Joined: Mon Nov 10, 2008 9:32 pm
Location: New York, NY, USA
x 1

Re: Blender 2.5 Exporter

Post by Tony Richards »

I'm not sure about Windows... on Linux I put it under blender/2.53/scripts/io/ogreio.
Image IndieZen.org - Open source game development tools, game engine, and MMO frameworks
User avatar
PolyVox
OGRE Contributor
OGRE Contributor
Posts: 1316
Joined: Tue Nov 21, 2006 11:28 am
Location: Groningen, The Netherlands
x 18

Re: Blender 2.5 Exporter

Post by PolyVox »

Ah, ok, I think on Windows it doesn't go within 'Program Files' but within the users 'AppData' folder (on Win 7). So I've placed it in 'C:\Users\David\AppData\Roaming\Blender Foundation\Blender\2.53\scripts\io\ogreio', and when I run Blender it generates two additional files compiled Python files with a .pyc extention. So I think Blender is finding it but it's not showing up under File->Export. Is that where it is supposed to be, or is there some other way of acessing the functionality?
Tony Richards
Halfling
Posts: 86
Joined: Mon Nov 10, 2008 9:32 pm
Location: New York, NY, USA
x 1

Re: Blender 2.5 Exporter

Post by Tony Richards »

With the files in the correct location, you should get a new File -> Export -> OGRE XML Mesh (.mesh.xml) menu item.

Your setup sounds correct unless you have:

C:\Users\David\AppData\Roaming\Blender Foundation\Blender\2.53\scripts\io\ogreio\ogreio.

I'll setup a Windows VM with Blender and see what I can see.
Image IndieZen.org - Open source game development tools, game engine, and MMO frameworks
User avatar
PolyVox
OGRE Contributor
OGRE Contributor
Posts: 1316
Joined: Tue Nov 21, 2006 11:28 am
Location: Groningen, The Netherlands
x 18

Re: Blender 2.5 Exporter

Post by PolyVox »

Ok, actually I now notice there is some output in the Blender console wondow when I start it up (before I even do anything):

Image

Maybe that provides an insight into what's going wrong?
Tony Richards
Halfling
Posts: 86
Joined: Mon Nov 10, 2008 9:32 pm
Location: New York, NY, USA
x 1

Re: Blender 2.5 Exporter

Post by Tony Richards »

Ok, I figured it out...

bpyml isn't in the distribution but it is in Blender SVN.

I'm not using the official 2.53 release during development... in order to keep up with the changes as they happen, I'm using root trunk SVN of Blender.

If you export https://svn.blender.org/svnroot/bf-blen ... s/bpyml.py to Blender\2.53\scripts\modules that would fix it, except there's one other thing that broke between 2.53 and current trunk.

If you'll pull from hg again, I pushed a fix that should do the trick after you've gotten bpyml.py.

This type of problem is probably the reason lf3thn4d hasn't wanted to tackle this yet. :mrgreen:
Image IndieZen.org - Open source game development tools, game engine, and MMO frameworks
Tony Richards
Halfling
Posts: 86
Joined: Mon Nov 10, 2008 9:32 pm
Location: New York, NY, USA
x 1

Re: Blender 2.5 Exporter

Post by Tony Richards »

More changes pushed:

Added exception handling that outputs exceptions to the log file. This will help me debug if someone else is having a problem.
Fixed an exception that occurs if the mesh does not have a material assigned.
Added better version checking so that it works in 2.53.0 as well as 2.53.1.

Now, for a bit of programmer-art, here's a castle tower I've been working on in Blender:

Image

And here it is, exported and converted to .mesh and imported into Ogitor:

Image
Image IndieZen.org - Open source game development tools, game engine, and MMO frameworks
User avatar
PolyVox
OGRE Contributor
OGRE Contributor
Posts: 1316
Joined: Tue Nov 21, 2006 11:28 am
Location: Groningen, The Netherlands
x 18

Re: Blender 2.5 Exporter

Post by PolyVox »

Getting close! I now get the following output in the log:

Code: Select all

Exporting to C:\Users\David\Documents\startup.mesh.xml
Traceback (most recent call last):
  File "C:\Users\David\AppData\Roaming\Blender Foundation\Blender\2.53\scripts\io\ogreio\export_mesh.py", line 65, in execute
    self.writeMesh(filepath)
  File "C:\Users\David\AppData\Roaming\Blender Foundation\Blender\2.53\scripts\io\ogreio\export_mesh.py", line 242, in writeMesh
    faces_node, vertex_face_index = self.generateFacesNode(submesh)
  File "C:\Users\David\AppData\Roaming\Blender Foundation\Blender\2.53\scripts\io\ogreio\export_mesh.py", line 145, in generateFacesNode
    if len(face.vertices) == 3: #tri
AttributeError: 'MeshFace' object has no attribute 'vertices'
This is with the default startup scene (the cube) that Blender creates.
zaph34r
Gnoblar
Posts: 24
Joined: Mon Feb 22, 2010 4:56 pm
Location: Germany

Re: Blender 2.5 Exporter

Post by zaph34r »

I wrote a mesh exporter myself yesterday for 2.5.3 (as i didn't see that one was already available in this thread, should look a bit more next time :lol: of course i find it right after i finished a few hours of coding and debugging ..)

I'll post the code, if someone wants to use it, or just take it apart and use it for the one in this thread, or whatever.

Things to note:
- This version only works by pasting it in the editor in blender, and use Run Script, i just couldn't get it to be recognized as an Addon, even if i directly copypasted the relevant parts from the API reference, or other (working) addons :?
- Exports as .mesh.xml (have to convert for yourself to .mesh, haven't integrated that yet because i use a batch script to do that anyways)
- material names are exported as-is from blender (so if a face has material "foo" assigned, it will look for a material named "foo" in ogre, had some weird stuff with /SOLID/TEX/Untitled being appended with the 2.49 exporter, didn't play nice with my dynamically generated materials. probably something with the textures, im no blender expert, and i abhor the 2.49 interface so i corrected that with my batch converter script :? )

Code: Select all

# Preliminary Blender2.5.3 Ogre .mesh.xml export script v0.1 by zaph34r
# licensed under the YCDWYWWI license (You Can Do Whatever You Want With It)

# usage: paste to blender text editor, select some object, Run Script

# exports mesh data of currently selected object to OUTPUT_PATH/objectname.mesh.xml
# exports assigned materials as named in blender to separate submeshes
# exports first uv set
# should correct scale issues if scale is not applied to object in blender, but better use Apply Visual Transform in blender to make sure
# maps coordinates to ogre coordinate system (blender > ogre: x > x, y > -z, z > y)

import bpy

SHADING = "flat" # "smooth" uses vertex normals instead of face normals, still duplicates verts for corners though
OUTPUT_PATH = "yourpath"

class OgreVector(object):
    def __init__(self, bVector):
        self.x = bVector.x
        self.y = bVector.z
        self.z = -bVector.y

    def scale(self,scale):
        self.x *= scale.x
        self.y *= scale.z
        self.z *= scale.y

    def __eq__(self,other):
        return self.x == other.x and self.y == other.y and self.z == other.z

    def __neq__(self,other):
        return not self.__eq__(other)
    def __repr__(self):
        return "Vector({0.x},{0.y},{0.z}".format(self)

class OgreFace(object):
    def __init__(self, verts,normal,uvFace):
        self.verts = [v for v in verts]
        self.normal = normal
        self.uvs = uvFace
        if not type(normal) == OgreVector:
            self.normal = OgreVector(normal)
    def triangulate(self):
        v1,v2,v3,v4 = self.verts
        uv1,uv2,uv3,uv4 = self.uvs
        return [OgreFace([v1,v2,v3],self.normal,[uv1,uv2,uv3]),OgreFace([v1,v3,v4],self.normal,[uv1,uv3,uv4])]
    def __repr__(self):
        return repr(self.verts)

class OgreVertex(object):
    def __init__(self,bVertex, uvs, scale):
        self.pos = OgreVector(bVertex.co)
        self.normal = OgreVector(bVertex.normal)
        self.pos.scale(scale)
        self.u = uvs[0]
        self.v = uvs[1]

    def __eq__(self, other):
        if type(other) == OgreVertex:
            return self.pos == other.pos and self.normal == other.normal and self.u == other.u and self.v == other.v
        else:
            raise TypeError

    def __neq__(self,other):
        return not self.__eq__(other)

    def __repr__(self):
        return "Vertex > "+self.pos.__repr__()

def exportMeshData():
    filepath = OUTPUT_PATH
    xml = MeshXMLWriter()

    obj = bpy.context.active_object
    vertices = obj.data.verts
    faces = obj.data.faces
    uvs = obj.data.active_uv_texture.data

    subMeshData = {}
    for face,uvFace in zip(faces,uvs):
        mat = obj.material_slots[face.material_index].name
        uvFace = [uvFace.uv1,uvFace.uv2,uvFace.uv3,uvFace.uv4]
        if mat not in subMeshData:
            subMeshData[mat] = []
        if len(face.verts) == 3:
            subMeshData[mat].append(OgreFace(face.verts,face.normal,uvFace))
        elif len(face.verts) == 4:
            subMeshData[mat].extend(OgreFace(face.verts,face.normal,uvFace).triangulate())

    submeshes = {}
    for material,submesh in subMeshData.items():
        vertexIndex = 0
        submeshes[material] = {"vertices":[],"faces":submesh}
        for face in submesh:
            newFaceVerts = []
            for index in face.verts:
                vertex = OgreVertex(vertices[index],face.uvs[face.verts.index(index)],obj.scale)
                if SHADING != "smooth":
                    vertex.normal = OgreVector(face.normal)
                if vertex in submeshes[material]["vertices"]:
                    newFaceVerts.append(submeshes[material]["vertices"].index(vertex))
                else:
                    submeshes[material]["vertices"].append(vertex)
                    newFaceVerts.append(vertexIndex)
                    vertexIndex += 1
            face.verts = newFaceVerts

    xml.buffer("<mesh>")
    xml.buffer("<submeshes>",1)
    for material,submesh in submeshes.items():
        xml.buffer('<submesh material="{0}" usesharedvertices="false">'.format(material), 2)
        xml.buffer('<faces count="{0}">'.format(len(submesh["faces"])),3)
        for face in submesh["faces"]:
            xml.buffer('<face v1="{0.verts[0]}" v2="{0.verts[1]}" v3="{0.verts[2]}"/>'.format(face),4)
        xml.buffer('</faces>',3)
        xml.buffer('<geometry vertexcount="{0}">'.format(len(submesh["vertices"])),3)
        xml.buffer('<vertexbuffer positions="true" normals="true" texture_coords="1">',4)
        for vertex in submesh["vertices"]:
            xml.buffer('<vertex>',5)
            xml.buffer('<position x="{0.pos.x:.6f}" y="{0.pos.y:.6f}" z="{0.pos.z:.6f}"/>'.format(vertex),6)
            xml.buffer('<normal x="{0.normal.x:.6f}" y="{0.normal.y:.6f}" z="{0.normal.z:.6f}"/>'.format(vertex),6)
            xml.buffer('<texcoord u="{0.u}" v="{0.v}"/>'.format(vertex),6)
            xml.buffer('</vertex>',5)
        xml.buffer("</vertexbuffer>",4)
        xml.buffer('</geometry>',3)
        xml.buffer('</submesh>',2)
    xml.buffer("</submeshes>",1)
    xml.buffer("</mesh>")
    filepath.rstrip(".mesh.xml")
    filepath = filepath+obj.name
    filepath += ".mesh.xml"
    xml.write(filepath)

class MeshXMLWriter(object):
    def __init__(self):
        self.textbuffer = []
        self.indendations = []
    def buffer(self, text, indendationLevel = 0):
        self.textbuffer.append(str(text))
        self.indendations.append(indendationLevel)
    def write(self, filename):
        f = open(filename,"w")
        for i in range(len(self.textbuffer)):
            f.write(self.indendations[i]*"\t"+self.textbuffer[i]+"\n")
        f.close()

exportMeshData()
I just want to add that in no way i want to usurp the thread, or say my exporter is teh shiznit, haven't looked at the one from here yet (due to being fatigued by making my own atm).
Just thought if i made it, i might as well share it, for the chance that someone has some use for it

PS: here, have some of what i did with it too:
Image
yay for random dungeons :)
User avatar
PolyVox
OGRE Contributor
OGRE Contributor
Posts: 1316
Joined: Tue Nov 21, 2006 11:28 am
Location: Groningen, The Netherlands
x 18

Re: Blender 2.5 Exporter

Post by PolyVox »

zaph34r wrote:I wrote a mesh exporter myself yesterday for 2.5.3 (as i didn't see that one was already available in this thread, should look a bit more next time :lol: of course i find it right after i finished a few hours of coding and debugging ..)
Hi, thanks for your efforts but actually I didn't get this one to work either...

Image

Any ideas?
User avatar
Kentamanos
Minaton
Posts: 980
Joined: Sat Aug 07, 2004 12:08 am
Location: Dallas, TX

Re: Blender 2.5 Exporter

Post by Kentamanos »

I just copied his code from the above code block into notepad and for some reason, all of it is indented (which indicates scope change in Python). I recommend pasting it into something like Notepad++, selecting all of it and unindenting it.
zaph34r
Gnoblar
Posts: 24
Joined: Mon Feb 22, 2010 4:56 pm
Location: Germany

Re: Blender 2.5 Exporter

Post by zaph34r »

i had that problem sometimes too with codeblocks in phpBB based forums.
the above mentioned "unindent all" often fucks things up even more, especially from some python boards (handy isn't it?), because after unindenting everything the stuff that should be indented is indented too little (python needs 4 spaces per indendation level, some code tags in some boards often indent everything just one to three spaces when you copypaste)

EDIT: just checked, in this case it should work, at least for me when i click SELECT ALL at the code block it indents everything 4 spaces, so if you unindent 4 spaces it should work just fine

just a second, ill upload it in a txt file

http://www.behindthemirrors.com/files/g ... export.txt
Tony Richards
Halfling
Posts: 86
Joined: Mon Nov 10, 2008 9:32 pm
Location: New York, NY, USA
x 1

Re: Blender 2.5 Exporter

Post by Tony Richards »

Blender 2.54 Beta was released Sept 11. Since this is finally "Beta" and the Python API is starting to settle down, I'm going to target that as my officially supported release.

I've not tested it on Windows yet. I'll do that now and once I'm sure it's working with 2.54 I'll push the updates and make a post here.
Image IndieZen.org - Open source game development tools, game engine, and MMO frameworks
SRombauts
Kobold
Posts: 39
Joined: Mon Feb 28, 2011 6:58 pm
Location: Paris, France
x 2

Re: Blender 2.5 Exporter

Post by SRombauts »

Hi,

I am a newcomer to OGRE : I've only played a while with some (great) tutorials from the wiki.

So now I would like to learn to use Blender with OGRE :
but it seams that learning Blender 2.49 is not a good idea anymore, as Blender 2.5 is now officially recommended for beginners (at least in Blender Video Tutorials).

So my question is :
Is the this new script blendogre (https://code.google.com/p/blendogre/) is stable enough ?

Are you guys planing to maintain it ? And what for animations in mesh ? Any timeline ?

Regards,
SRombauts

ps: I think I will try it anyway, and give you some feedback... but I do not have much time before a while !
http://www.srombauts.fr, comments from an Android developer
SRombauts
Kobold
Posts: 39
Joined: Mon Feb 28, 2011 6:58 pm
Location: Paris, France
x 2

Re: Blender 2.5 Exporter

Post by SRombauts »

Hi,

In fact there is another quite good Blender 2.5 Plugin exporter for Ogre in development mentioned in this topic :
http://www.ogre3d.org/forums/viewtopic.php?f=4&t=61485

I will follow it closely and try to help !

Regards,
SRombauts
http://www.srombauts.fr, comments from an Android developer
User avatar
LiMuBei
Goblin
Posts: 297
Joined: Mon Jun 09, 2008 3:56 pm
Location: Karlsruhe, Germany
x 10

Re: Blender 2.5 Exporter

Post by LiMuBei »

Check out the one SRombauts pointed out. Works for animations!