【问题标题】:How to handle object groups in blender game engine?如何处理搅拌机游戏引擎中的对象组?
【发布时间】:2015-07-05 03:22:20
【问题描述】:

我知道有一种方法可以找出特定对象在 BGE 中所在的组。 在 API 中,它说您使用 KX_GameObject(SCA_IObject)groupObject 获取对象所在的组,并使用 groupMembers 获取组中所有对象的列表。我尝试了许多不同的方法来让它工作,但我总是得到一个错误。请问有人能告诉我怎么做吗?

这里是 API 的链接: http://blender.org/api/blender_python_api_2_73a_release/bge.types.KX_GameObject.html?highlight=group#bge.types.KX_GameObject.groupMembers

这是代码。

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'

谢谢。

【问题讨论】:

  • 您能否详细说明如何您的程序失败了?从文档中,您编写 my_object.groupObject 以获取 “对象所属的组对象,如果该对象不属于某个组,则为 None” 这是否引发了异常?你有没有收到None 的期待?您确定该对象属于某个组吗?我可以建议你写一个minimal program 来展示你的问题吗?
  • 我添加了代码。感谢您重播。
  • group 可能(也许?)不是KX_GameObject 实例。尝试通过添加 print(type(group))print(group)print(group.__dict__) 之类的内容来更仔细地检查您的 group (resp cube) 对象。
  • 最真实的我......我尝试了一个小时,然后在堆栈溢出时询问它。

标签: python game-engine blender


【解决方案1】:

.groupMembers 仅适用于作为组的添加实例的对象

因此,如果对象不是组实例对象或组实例子对象,则它可能没有组成员。

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

添加=空

added.groupMembers = 使用该空添加的所有对象

【讨论】:

  • 抱歉回复晚了。自从我发布了这个问题后,我发现 bpy 在搅拌机游戏引擎中不起作用。所以这永远不会奏效,但你的替代方案是一个有效的选择。
猜你喜欢
  • 2013-01-17
  • 2015-05-13
  • 2015-07-02
  • 2019-12-22
  • 2013-06-27
  • 2015-01-26
  • 2022-12-20
  • 2017-11-20
  • 2015-04-23
相关资源
最近更新 更多