dotScene autoCoder

sharky_151

24-01-2006 00:28:27

Hello all, If you are using Blender 3d to create models and pyOgre to create your app, you might be interested in this file. This file reads in a dotScene XML that been exported from blender and spits out a pyOgre source file based on the SampleFramework thats included with pyOgre.




This is version 1 and has some limitations, see the bottom of the post.
However, it is pretty functional, so if this file is useful to anyone, reply to this post and i'll make version 2 available here when it is done.

# This code is in the Public Domain

def num_words(line):
word = 0
for char in range(0, len(line)):
if (line[char] == "\""):
word += 1
word += 1

return word

def get_word(word_num, string):
word = 0
pos = -1

for char in range(0, len(string)):
if (string[char] == "\""):
pos += 1
tempString = string[pos:char]
if (word_num == word):
return tempString

pos = char
word += 1

# This picks up the last word
if (word_num == word):
tempString = string[pos + 1:len(string) - 1]
return tempString

return

def strip_XML(filename, list):
try:
file_in = open(filename)
except:
return

# Skips the first two lines.
line = file_in.readline()
line = file_in.readline()

addWord = False
a_list = list
line = file_in.readline()

# Reads the file in one line at a time strips the XML stuff and
# adds the words to a list one word at a time.
while(line != ""):
for i in range(0, num_words(line)):
a_word = get_word(i, line)
if (addWord == True):
a_list.append(a_word)
addWord = False
else:
addWord = True

# Next Line
addWord = False
line = file_in.readline()

file_in.close()

return a_list

def rename(name):
for char in range(0, len(name)):
if (name[char] == "."):
tempString = name[:char] + "_" + name[char+1:]
return tempString

return name

c1 = ""
print " **********************************"
print " * Blender 3D *"
print " * 'dotScene autoCoder' *"
print " * *"
print " * *"
print " * written by Dave Harbold *"
print " * --- *"
print " * a.k.a sharky_151 *"
print " * *"
print " **********************************"
print
print
print "default = Scene.xml"
print
print "Type in the input filename or"
in_filename = raw_input("press ENTER for default: ")

if (in_filename == ""):
in_filename = "Scene.xml"

print "__________________________________________"
print
print "default = Scene.py"
print
print "Type in the output filename or"
out_filename = raw_input("press ENTER for default: ")

if (out_filename == ""):
out_filename = "Scene.py"

print
print "Working please wait..."
print

list = []
strip_XML(in_filename, list)

fout = open(out_filename,"w")

fout.write("# This code is in the Public Domain\n")
fout.write("from pyogre import ogre\n")
fout.write("import SampleFramework\n")
fout.write("\n")
fout.write("class dotSceneApplication(SampleFramework.Application):\n")
fout.write("\n")
fout.write(" def _createScene(self):\n")
fout.write(" # Scene Manager\n")
fout.write(" sceneManager = self.sceneManager\n")

for i in range(0,len(list)):
pos = (len(list) - (i+1))
case = list[pos]

if case == 'point':
newName = rename(list[pos-1])
fout.write(" \n")
fout.write(" # " + newName + "\n")
fout.write(" " + newName + " = self.sceneManager.createLight(\"" + newName + "_pnt\")\n")
fout.write(" " + newName + "_node = sceneManager.rootSceneNode.createChildSceneNode(\"" + newName + "_Node\")\n")
fout.write(" " + newName + "_node.attachObject(" + newName + ")\n")
fout.write(" " + newName + "_node.position = (" + list[pos-11] + ", " + list[pos-10] + ", " + list[pos-9] + ")\n")
fout.write(" " + newName + ".type = ogre.Light.LT_POINT\n")
fout.write(" " + newName + ".diffuseColour = (" + list[pos+1] + ", " + list[pos+2] + ", " + list[pos+3] + ")\n")
fout.write(" " + newName + ".specularColour = (" + list[pos+4] + ", " + list[pos+5] + ", " + list[pos+6] + ")\n")
fout.write(" " + newName + ".setAttenuation = (" + list[pos+7] + ", " + list[pos+8] + ", " + list[pos+9] + ", " + list[pos+10] + ")\n")

if case == 'spot':
if list[pos+1] == "true":
list[pos+1] = "True"
else:
list[pos+1] = "False"

newName = rename(list[pos-1])
fout.write(" \n")
fout.write(" # " + newName + "\n")
fout.write(" " + newName + " = self.sceneManager.createLight(\"" + newName + "_spt\")\n")
fout.write(" " + newName + "_node = sceneManager.rootSceneNode.createChildSceneNode(\"" + newName + "_Node\")\n")
fout.write(" " + newName + "_node.attachObject(" + newName + ")\n")
fout.write(" " + newName + "_node.position = (" + list[pos-11] + ", " + list[pos-10] + ", " + list[pos-9] + ")\n")
fout.write(" " + newName + "_node.rotate(ogre.Quaternion(" + list[pos-5] + ", " + list[pos-8] + ", " + list[pos-7] + ", " + list[pos-6] + "))\n")
fout.write(" " + newName + "_node.pitch(ogre.Degree(180))\n")
fout.write(" " + newName + ".type = ogre.Light.LT_SPOTLIGHT\n")
fout.write(" " + newName + ".castShadows = " + list[pos+1] + "\n")
fout.write(" " + newName + ".diffuseColour = (" + list[pos+5] + ", " + list[pos+6] + ", " + list[pos+7] + ")\n")
fout.write(" " + newName + ".specularColour = (" + list[pos+8] + ", " + list[pos+9] + ", " + list[pos+10] + ")\n")
fout.write(" " + newName + ".setSpotlightRange(ogre.Degree(" + list[pos+11] + "), ogre.Degree(" + list[pos+12] + "), " + list[pos+13] + ")\n")
fout.write(" " + newName + ".setAttenuation = (" + list[pos+14] + ", " + list[pos+15] + ", " + list[pos+16] + ", " + list[pos+17] + ")\n")

if case == 'directional':
newName = rename(list[pos-1])
fout.write(" \n")
fout.write(" # " + newName + "\n")
fout.write(" " + newName + " = self.sceneManager.createLight(\"" + newName + "_dir\")\n")
fout.write(" " + newName + "_node = sceneManager.rootSceneNode.createChildSceneNode(\"" + newName + "_Node\")\n")
fout.write(" " + newName + "_node.attachObject(" + newName + ")\n")
fout.write(" " + newName + "_node.position = (" + list[pos-11] + ", " + list[pos-10] + ", " + list[pos-9] + ")\n")
fout.write(" " + newName + "_node.rotate(ogre.Quaternion(" + list[pos-5] + ", " + list[pos-8] + ", " + list[pos-7] + ", " + list[pos-6] + "))\n")
fout.write(" " + newName + "_node.pitch(ogre.Degree(180))\n")
fout.write(" " + newName + ".type = ogre.Light.LT_DIRECTIONAL\n")
fout.write(" " + newName + ".diffuseColour = (" + list[pos+4] + ", " + list[pos+5] + ", " + list[pos+7] + ")\n")
fout.write(" " + newName + ".specularColour = (" + list[pos+7] + ", " + list[pos+8] + ", " + list[pos+9] + ")\n")

if case == 'material':
hasFog = False
if list[pos+4] == "linear":
fout.write(" sceneManager.setFog(ogre.FOG_LINEAR, ogre.ColourValue(" + list[pos+5] + ", "+ list[pos+6] + ", " + list[pos+7] + "), .001, " + list[pos+2] + ", " + list[pos+3] + ")\n")
hasFog = True

if hasFog:
fout.write(" sceneManager.ambientLight = (" + list[pos+8] + ", " + list[pos+9] + ", " + list[pos+10] + ")\n")
fout.write(" #sceneManager.shadowTechnique = ogre.SHADOWTYPE_STENCIL_ADDITIVE\n")
background = list[pos+11] + ", " + list[pos+12] + ", " + list[pos+13]
else:
fout.write(" sceneManager.ambientLight = (" + list[pos+2] + ", " + list[pos+3] + ", " + list[pos+4] + ")\n")
fout.write(" #sceneManager.shadowTechnique = ogre.SHADOWTYPE_STENCIL_ADDITIVE\n")
background = list[pos+5] + ", " + list[pos+6] + ", " + list[pos+7]

if len(case) >= 5:
if case[0:5] == 'Empty':
newName = rename(list[pos])
fout.write(" \n")
fout.write(" # " + newName + "\n")
fout.write(" " + newName + "_node = sceneManager.rootSceneNode.createChildSceneNode(\"" + newName + "_Node\")\n")
fout.write(" " + newName + "_node.position = (" + list[pos+1] + ", " + list[pos+2] + ", " + list[pos+3] + ")\n")
fout.write(" " + newName + "_node.rotate(ogre.Quaternion(" + list[pos+7] + ", " + list[pos+4] + ", " + list[pos+5] + ", " + list[pos+6] + "))\n")
fout.write(" " + newName + "_node.scaleBy(" + list[pos+8] + ", " + list[pos+9] + ", " + list[pos+10] + ")\n")

if case[len(case)-5:len(case)] == '.mesh':
newName = rename(list[pos-1])
fout.write(" \n")
fout.write(" # " + newName + "\n")
fout.write(" " + newName + "_ent = sceneManager.createEntity(\"" + newName + "\", \"" + list[pos] + "\")\n")
fout.write(" " + newName + "_node = sceneManager.rootSceneNode.createChildSceneNode(\"" + newName + "_Node\")\n")
fout.write(" " + newName + "_node.attachObject(" + newName + "_ent)\n")
fout.write(" " + newName + "_node.position = (" + list[pos-11] + ", " + list[pos-10] + ", " + list[pos-9] + ")\n")
fout.write(" " + newName + "_node.rotate(ogre.Quaternion(" + list[pos-5] + ", " + list[pos-8] + ", " + list[pos-7] + ", " + list[pos-6] + "))\n")
fout.write(" " + newName + "_node.scaleBy(" + list[pos-4] + ", " + list[pos-3] + ", " + list[pos-2] + ")\n")

if case == 'perspective':
newName = rename(list[pos-2])
c1 = " # " + newName + "\n"
c2 = " self.camera = self.sceneManager.createCamera(\"" + newName + "_per\")\n"
c3 = " self.camera.position = (" + list[pos-12] + ", " + list[pos-11] + ", " + list[pos-10] + ")\n"
c4 = " self.camera.rotate(ogre.Quaternion(" + list[pos-6] + ", " + list[pos-9] + ", " + list[pos-8] + ", " + list[pos-7] + "))\n"
c5 = " self.camera.FOVy = (ogre.Degree(" + list[pos-1] + "))\n"
c6 = " self.camera.setProjectionType(ogre.PT_PERSPECTIVE)\n"
c7 = " self.camera.nearClipDistance = " + list[pos+4] + "\n"
c8 = " self.camera.farClipDistance = " + list[pos+5] + "\n"

if case == 'orthographic':
newName = rename(list[pos-2])
c1 = " # " + newName + "\n"
c2 = " self.camera = self.sceneManager.createCamera(\"" + newName + "_ort\")\n"
c3 = " self.camera.position = (" + list[pos-12] + ", " + list[pos-11] + ", " + list[pos-10] + ")\n"
c4 = " self.camera.rotate(ogre.Quaternion(" + list[pos-6] + ", " + list[pos-9] + ", " + list[pos-8] + ", " + list[pos-7] + "))\n"
c5 = " self.camera.FOVy = (ogre.Degree(" + list[pos-1] + "))\n"
c6 = " self.camera.setProjectionType(ogre.PT_ORTHOGRAPHIC)\n"
c7 = " self.camera.nearClipDistance = " + list[pos+4] + "\n"
c8 = " self.camera.farClipDistance = " + list[pos+5] + "\n"

if c1 != "":
fout.write("\n")
fout.write(" def _createCamera(self):\n")
fout.write(c1)
fout.write(c2)
fout.write(c3)
fout.write(c4)
fout.write(c5)
fout.write(c6)
fout.write(c7)
fout.write(c8)

fout.write("\n")
fout.write(" def _createViewports(self):\n")
fout.write(" # Viewport\n")
fout.write(" vp = self.renderWindow.addViewport(self.camera)\n")
fout.write(" vp.backgroundColour = (" + background + ")\n")
fout.write(" \n")
fout.write(" self.camera.aspectRatio = vp.actualWidth / vp.actualHeight\n")

fout.write("\n")
fout.write("if __name__ == '__main__':\n")
fout.write(" try:\n")
fout.write(" application = dotSceneApplication()\n")
fout.write(" application.go()\n")
fout.write(" except ogre.OgreException, e:\n")
fout.write(" print e")

fout.close()

print
a = input("Done, press ENTER to quit.")


General Usage:
-------------------------------------------
1 Model your scene in blender.
2 Use the Ogre dotScene export script.
3 Use the Ogre XML export script.
4 Run the autoCoder.
5 Copy all the files in place and run your app.

Detailed Usage:
-------------------------------------------
1 Download ogre Command line tools and unzip.

2 Download blender 3d dotScene and Ogre XML export scripts,
use the browse cvs at SourceForge to get the newest versions and be
sure to read the HTML readme the see what gets exported.

3 Edit the ogre XML script to point to the OgreXMLConverter example:

----OGRE_XML_CONVERTER = 'C:\\Ogre\\OgreCommandLineTools-1.0.6\\OgreXMLConverter'

4 Copy the scripts to your to the Blender scripts folder.

5 Model your scene in Blender then select all.

6 In blender bring up a scripts window and export dotScene, with the these options set.

----RotX 90 RotY 0 RotZ 0

----*I know it says -90 on the X axis in the readme, but I coundn't get that to work properly so use positive 90 on the X axis.

7 In blender bring up a scripts window and export ogre XML, with the these options set.

----RotX 0 RotY 0 RotZ 0 and world coorinates unchecked

----* The dotScene takes care of position, rotation and scaling.

8 Copy the autoCoder program in the same folder as the dotScene XML file and run it.

9 Copy the meshes to the media/models folder, textures to the media/materials/textures folder, materials to the media/materials/scripts folder. and the autoCoded source to the ogre demos folder.

Limitations:
------------------------------------------------
Here is a list of things that will cause all of this not to work properly.

1 Naming you blender objects.
-Don't name any of your blender objects any of the following keywords case sensitive.

point, spot, directional, material, Empty, .mesh, perspective, othographic

-Empty's must start with the word Empty (case sensitive) this is blenders default naming.

-No more the one "." in the name (i.e. Lamp.001.whatever = bad)

2 Having multiple definitions of the same material in the scripts folder.

3 Copying Meshes in blender Shift-D is OK but you will need to modify the code to point to the same .mesh file.

4 Exporting multiple cameras is OK but you will only get the one that is at the top of the dotScene XML. (I haven't desided what to do with multiple cams yet)

5 The dot Scene file exports addition information that is not evaluted yet such as:

-Parenting
-if a mesh is static.
-user data

6 Blender primatives tend to be way smaller than the models included in the media folder, change the moveSpeed in the sample framework from 100 to 10, and bring the energy on the lamps way down.

Please let me know of addition limitations or code improvements.

----------
OK I hope all of this is useful. But I have a couple of questions about materials of my own.

1. The specular highlight sometimes shows up and sometime it doesn't(see the isosphere in the pic above no spec) sometimes switching from dx9 to openGL, or changing the scaling on the mesh will fix this. Why is that???

2. Does anyone know how to do per pixel lighing in pyOgre. I've seen the static geometry demo what exactly do you need to get this working???

late 8)