2.5 Object Properties

Hi All,

I am trying to store some values in an object property. But my code does not work.

Why does my code fail?


import bpy

# Kind of out-of-place but required to be declared before used.
def returnIfObject(passedName=""):
    try:
        result = bpy.data.objects[passedName]
    except:
        result = None
    return result

myObject = returnIfObject("Cube")
if myObject != None:
    try:
        myProperty = myObject["myProperty"]
        result = True
    except:
        result = False
    if result == True:
        print("Fetched: " + str(myProperty) + ".")
    else:
        # Assign a property, none exists.
        try:
            myObject.properties["myProperty"] = "myValue"
            result = True
        except:
            result = False
        if result == True:
            print("myPropery is assigned!")
        else:
            print ("Failed to store myValue in myProperty.")
else:
    print ("Failed to fetch object [Cube].")

On the default scene, it prints “Failed to store myValue in myProperty”. Is my syntax wrong?

myObject.properties["myProperty"] = "myValue"

Should be

myObject["myProperty"] = "myValue"

You’re stumbling here because the try/except blocks swallow the exceptions Blender gives you. I avoid them whenever I can, and when I can’t always restrict them to the exception types I expect to receive.

Here’s a cleanup of your code, with that change and a few others:

import bpy

myObject = bpy.data.objects.get("Cube") # same as [] syntax, but no exception if it doesn't exist)
if myObject:
    try:
        myProperty = myObject["myProperty"]
        print("Fetched: " + str(myProperty) + ".")
    except KeyError: # the specific exception that I think myObject["myProperty"] might return
        # try/except removed: errors here are down to the script and should not be swallowed
        myObject["myProperty"] = "myValue"
        print("myPropery is assigned!")
else:
    print ("Failed to fetch object [Cube].")

You could actually use thing.get(prop) everywhere, but sometimes exceptions are useful. :slight_smile:

Edit: I’m talking about 2.5, are you?

i tried to run this in script window and nothing happen with v31315

which version are you using
happy 2.5

@ArtFunkle: Thanks for the clarification. I noticed in your re-write of my code, that you took out the comparison to None in favor of a True/False comparison. Is this “safe”.

Is None equal to False?

Assumptions like that can lead to hard to find bugs…

It works in the SVN from today
but in the console I see this:

C:\Users\Peter\25Blender\blenderLatest>blender
found bundled python: C:\Users\Peter\25Blender\blenderLatest\2.53\python
Memoryblock Data from SCR: end corrupt
myPropery is assigned!
Fetched: myValue.

Some error? end corrupt

None is not equal to False, but it is evaluated as a “False” value.

If you don’t want to use exceptions (that it is expensive) you can use:


if "myproperty" in object: print("yeah")

so you could use something as:


import bpy

# Kind of out-of-place but required to be declared before used.
def returnIfObject(passedName=""):
  if passedName in bpy.data.objects:
    return bpy.data.objects[passedName]
  else:
    return None

myObject = returnIfObject("Cube")
if myObject is not None: # this is the correct way to compare None values, although other ways might work too.
  if "myproperty" not in myObject:
    print(passedName, "  doesn't have a property called:myproperty so creating the property")
    myObject["myproperty"] = "myValue"

else:
  print ("Failed to fetch object 'Cube'")

@Lepes, But will that protect against an exception if one occurs while I am referencing the variable? That is why I simply gave up on all other approaches and use TRY/EXCEPT everywhere because I am sick and tired of Blender throwing exceptions when I am trying to discover if an object exists or is valid.

I really have not noticed any kind of performance problem using TRY/EXCEPT everywhere.

There is a rule while programming: “don’t catch an exception that you can’t control”.

A good code will be:


try
  x = bpy.data.objects['Cube']
except onKeyError: #only catch this kind of errors
  print("ups cube doesn't exists")

But there is already another rule while programming: “don’t launch an exception if you can easily avoid it”. That’s what I am doing:


if not passedname in bpy.data.objects:
  print(passedname, " object is not on bpy.data.objects, ouch!")
  return None

This code do the same than this:


    try:
        result = bpy.data.objects[passedName]
    except:
        result = None
    return result

It’s imposible that an exception is launched on my code, because I ask (wheather the objects exists or not in the collection) before accesing the object.

I am sick and tired of Blender throwing exceptions

I am too, but it is part of the game.