0
$\begingroup$

I need a bpy.data.objects["object name"]["property value"] or a command like this.

$\endgroup$
  • $\begingroup$ Welcome to BSE! If you're new to the StackExchange network, please take the tour:blender.stackexchange.com/tour This question does not provide nearly enough information for us to give helpful answers. Please describe what you're trying to do, what you've attempted to achieve that, and what problem you're encountering. This does not necessarily require a lot of words, but it does require specificity. Help us help you! $\endgroup$ – Matt Apr 11 '18 at 2:37
0
$\begingroup$

bpy is the Blender API.

The BGE API is bge.

You can access game properties via dictionary access on the game object

Read

Read and fail when it does not exist

print("property value is ", gameObject["propertyName"])

This is the fastest method, but fails when the property is not present. This is good when the property is mandatory.

Read with fallback value

print("property value is ", gameObject.get("propertyName", "fallbackValue"))

This is pretty fail safe and can be used on optional properties.

Be aware it always creates the fallback value. This costs processing time. Ensure creating the fallback value is fast (or use an existing value).

The property will not be set, when it does not exist.

Read with previous existence check:

if "propertyName" in gameObject:
    print("property value is ", gameObject["propertyName"])
else:
    print("no property set")

Requires more code but is very fail safe. When the property exists the read access is performed twice (once for check existence, once to get the value).

It is good when the property is optional and the fallback situation needs some effort (such as creating larger objects) or leads to complete different behavior. The fallback code will only be executed when the property is not existent. This solution is more efficient when in most of the cases the property is not present (as this results in a single read access).

Read with error handling:

try:
    print("property value is ", gameObject["propertyName"])
except KeyError:
    print("no property set")

More code but fail safe. Pretty efficient as the access will happen just once. The fallback can be complex, it will only be executed when the property is not present.

Write

gameObject["propertyName"] = newValue

It is the exact same code, regardless if you want to add or change a property

Delete

del gameObject["propertyName"]

Example

Initialize Property

try:
    propertyValue = gameObject["propertyName"]
except KeyError:
    propertyValue = "fallbackValue"
    gameObject["propertyName"] = propertyValue 

print("property value is ", propertyValue )

[Edit:]

Getting Game Objects

Just in case you do not know how to get or find game objects:

import bge

v1 = bge.logic.getCurrentScene().objects["v1"] # this will fail when there is no object called v1!
farbe = v1.get("farbe")
print("farbe of v1 is", farbe)
$\endgroup$
  • $\begingroup$ Thanks a lot my GameObject is "v1" and the propertyName is "farbe" I tried it with: import bge if farbe in v1: print("property value is ", v1[farbe]) else: print("no property set") error: Python script error - object 'v1', controller 'Python': Traceback (most recent call last): File "property check.py", line 3, in <module> NameError: name 'farbe' is not defined I think it says that farbe isn't a property of v1 but shouldn't come "no property set" then? and actually I have set the Property in the logic bricks ans also tried v1[farbe] = 0 and its still not working $\endgroup$ – user55068 Apr 11 '18 at 16:56
  • $\begingroup$ sorry I don't know how to answer correctly this doesn't seem right.. :/ $\endgroup$ – user55068 Apr 11 '18 at 16:57
  • $\begingroup$ oh yeah and when i wrote v1[farbe] = 0 this came: Traceback (most recent call last): File "<blender_console>", line 1, in <module> NameError: name 'v1' is not defined $\endgroup$ – user55068 Apr 11 '18 at 16:58
  • $\begingroup$ The property name is a String. So you need to write v1["farbe"]. $\endgroup$ – Monster Apr 12 '18 at 4:14
  • $\begingroup$ The "NameError" means you used a name that Python does not know what it is. This typically happens when using a variable name before creating this variable as you did. There is no variable, function or keyword called "farbe", but you are using this name. So python does not know what to do and raises the "NameError: name 'farbe' is not defined". As stated above: use the String literal "farbe" (with "). Alternative you could create a variable farbe = "farbe" before using it. $\endgroup$ – Monster Apr 12 '18 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.