Blender/Python Read Properties?

Hi, this is my first post here so bear with me on this one.

I’m trying to make an export script that writes each object’s name and it’s properties ( the ones that I’ve set under the logic buttons)
Basically, I’m trying to make a very simple level editor

This is what I have so far:


#!BPY

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

def write(filename):
    out = file(filename, "w")
    sce= bpy.data.scenes.active
    for ob in sce.objects:
        if ob.type == "Mesh": 
            out.write(ob.name + "
")
            properties = dir(ob)
            for property in properties:
                out.write(property.find("path"))
                out.write("/n")
                out.write(property.find("static") + "
")
            
            
Blender.Window.FileSelector(write, "Export")

I think I’m doing the reading of the properties wrong because it will output the name of the object, then give me an error saying “Line 22, in write: Argument 1 must be string or read-only character buffer”. I set the out.write(property.find(“path”)) path property to string.

Can someone tell me what I’m diong wrong? Thanks

Simple:
You have to turn your returned value, which is an int, into a string first for out.write to proceed.

out.write(str(property.find("path")))

In fact you’d end up with that:

                out.write(str(property.find("path")) +"
")
                out.write(str(property.find("static")) + "
")

PS: welcome to the forum !

Working great now, though it only seems to export a bunch of -1 's even though I’ve defined properties for the objects

Find returns -1 if the substring is not found. For instance:


myStr = 'I like to dance'
fIndex = myStr.find('chicken')
print fIndex #prints -1

What exactly are you trying to export, id properties? The ‘dir’ function just exposes all the members of an object (or module), so you’ll get a list of all the methods as well as internal class variables, but not id properties.

I suppose it’s ID properties. So, what method would you use to loop through all the properties?

ID properties are stored as dictionaries (or rather accessed), so:


for propName, propValue in ob.properties.iteritems():
     # Do Your File I/O Stuff

ok maybe I’m using the wrong type of property. Where do you set the ID properties of a mesh? Is that the most efficient way to add per-Object “extra” data?

Its generally the best way, and can be set as:


ob.properties['someProperty'] = 'someValue'

You might check out some more at:
http://wiki.blender.org/index.php/Dev:Source/Blender/Data_System/ID_Property

What was your present way of associating data?

Is there any way to set the ID property in-editor, like in the gui or something?
I was currently assigning them through the logic properties tab.

Since this is going to be more of a level editor, I need to be able to set them through Blender and not through the code

There’s a script, the ID Property Browser that can do it. Its under the ‘Help’ Menu.

ah, thanks for your help