Blender and bpy Modules

Hello,

I’m trying to write an export script which exports an object’s location to a file. I’ve created a little test script which works perfectly when run from Blender’s text editor (using Alt-P):


import Blender as B

selection = B.Object.GetSelected()

myObject = selection[0]

print myObject.loc

As expected, the object’s coordinates are printed to the Console. Now I want to do the same thing with an export script, but rather than printing the output to the Console, I want to save it to a file. Using the code from this Wikibooks export tutorial, I tried to substitute in the previous code like this:


#!BPY

"""
Name: 'Wikibooks'
Blender: 244
Group: 'Export'
Tooltip: 'Wikibooks sample exporter'
"""
import Blender as B
import bpy

def write(filename):
	out = file(filename, "w")
	selection = B.Object.GetSelected()
	myObject = selection[0]
	out.write(myObject.loc)

Blender.Window.FileSelector(write, "Export")

I got this error in Console:


Traceback (most recent call last):
  File "<string>", line 18, in ?
NameError: name 'Blender' is not defined

Since in all the export scripts I had seen only the bpy module was used, I tried adapting another Wikibooks tutorial script which used that module. Instead of writing the object’s type and name (like in the example script), however, I tried to write the object’s type and location:


def write(filename):
    out = file(filename, "w")
    sce = bpy.data.scenes.active
    for ob in sce.objects:
        out.write(ob.type + ": " + ob.loc + "
")

Blender.Window.FileSelector(write, "Export")

I got this error in Console:


Traceback (most recent call last):
  File "<string>", line 16, in write
TypeError: cannot concatenate 'str' and 'tuple' objects

What does this mean? How can I successfully write to a file the location of an object using an export script? I’m using Blender 2.44 on Mac OS X 10.4 Tiger…

Thanks,
Steve

You have imported the Blender module as ‘B’ so you need to used that in the line "
Blender.Window.FileSelector(write, “Export”)"

It should be “B.Window.FileSelector(write, “Export”)”

I propose the first error is solved.

The second one comes from the point, that Python don’t know how to combine a tuple and a string object. You have to convert the tuple object into a string.
In you case you have to convert:
“ob.loc”, which is the tuple via:


str(ob.loc)

into a string.
Or you can use:


"x: " + str(ob.locX) + "y: " + str(ob.locY) + "z: " + str(ob.locZ) 

depends on the string formating you want.