简体   繁体   中英

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.

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM