Search code examples
pythongame-engineblender

How to handle object groups in blender game engine?


I know there's a way to find out the group in which the certain object is in BGE. In the API it says that you use KX_GameObject(SCA_IObject) and groupObject to get the group the object is in and groupMembers to get a list of all objects in the group. I tried many diferent ways to get it working, but I always get an error. Plese can someone tell me how to do it?

Here's a lonk to the API: http://blender.org/api/blender_python_api_2_73a_release/bge.types.KX_GameObject.html?highlight=group#bge.types.KX_GameObject.groupMembers

Here's the code.

import bge
from bge import logic
import bpy

cont = bge.logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()
objs = scene.objects

print(objs)


#here I tried to get the list of members in the group
group = bpy.data.groups["group"]
print(group.groupMembers)
#ERROR: 'Group' object has no attribute 'groupMembers'


#here I tried to get the group object is in
cube = bpy.data.objects["Cube"]
print(cube.groupObject)
#ERROR: 'Object' object has no attribute 'groupObject'

Thanks.


Solution

  • .groupMembers is only for objects that are a added instance of a group

    enter image description here

    so a object may have no group members if it was not a group instance object or group instance subobject.

    like added = own.scene.addObject('GroupInstance',own,0)

    added = a empty

    added.groupMembers = all the objects added with that empty