最近我开始使用C ++和OpenGL构建3D游戏引擎,但我想用Python编写一个脚本,将实际场景中的一些对象导出到文件中。一切都已成功,除了我有一些关于Python API如何与对象和不同类型的对象一起工作的问题?
代码:
for Lampa in Lamp.Get(): # In this case we are working with lamps
Lampatipus=Lampa.getType()
if Lampa.getParent()==Targy\ # Objects have parents in blender, but it shows error that lamps doesn't have functions like getParent()
and Lampatipus=="Lamp":
self.file.write(Lampa.getName())
self.file.write("\00")
# Lampa beallitasai
Lampa_alap_tomb=array('f', [\
Lampa.LocX, # Shows error message that lamps doesnt have position x...
Lampa.LocY,
Lampa.LocZ,
Lampa.R,
Lampa.G,
Lampa.B,
Lampa.getEnergy()/10.0,
Lampa.color[0],
Lampa.color[1],
Lampa.color[2],
Lampa.color[3]\
])
Lampa_alap_tomb.tofile(self.file)
# Another case:
for Lampa in Jelenet.objects: # In this case we are working with objects
Lampatipus=Lampa.getType()
if Lampa.getParent()==Targy\ # no problem here
self.file.write(Lampa.getName())
self.file.write("\00")
# Lampa beallitasai
Lampa_alap_tomb=array('f', [\
Lampa.LocX,
Lampa.LocY,
Lampa.LocZ,
Lampa.R, # Shows error message that objects doesnt have R (red component of color of a light)
Lampa.G,
Lampa.B,
Lampa.getEnergy()/10.0,
Lampa.color[0],
Lampa.color[1],
Lampa.color[2],
Lampa.color[3]\
])
Lampa_alap_tomb.tofile(self.file)
结束代码!!
例如,如果我想浏览所有灯并将其中的一些属性写入文件(名称,颜色,父对象等),则某些灯的属性不会被Python识别为使用的变量由不同的对象。如果我遍历每个对象并首先得到对象的类型(实际上是一个灯),同样的事情就会发生,但是控制台会显示一条错误消息,表明例如点半径或其他任何东西都不是“ Blender对象“。在前面的例子中,我已经解释过Python没有意识到“Blender Lamp”实际上是一个“Blender对象”,但是“Blender Lamp”也应该保留他们原来的属性,这些属性是继承自“Blender Object”的。 。因为在Blender中,无论物体有什么类型,它都有位置旋转,比例等等。到目前为止,你知道每盏灯都有位置(像物体一样)和光线属性(浅色等)但是如果我愿意的话为了得到一个搅拌灯的位置,它不起作用,因为它表明那种灯不是一个物体,但在Blender灯也有位置和一切像普通物体。我也没有在Blender 2.49 python api文档中找到对灯光位置的引用。
请帮忙! 提前谢谢......
P上。 S.对不起英语,我来自匈牙利,没有专业。以及我用匈牙利语写的一些变量,但我希望你能理解这个问题。 THX
答案 0 :(得分:1)
你还在使用2.49吗?它现在已经很老了,如果你没有使用2.49,那么2.49 API文档将无法提供帮助,因为从2.50开始,所有内容都随着python而改变。如果您使用的是更新版本,那么您应该会发现当前blender API documentation更有帮助。
使用最新版本的搅拌机,以下内容应该会有所帮助 -
import bpy
for obj in bpy.context.scene.objects:
if obj.type == 'LAMP' and obj.parent == Targy:
print(obj.name)
print(obj.location.x)
print(obj.data.color.r)
print(obj.data.energy)
不要将obj.color
与obj.data.color
混淆。 obj.color
是一个对象属性,可供所有对象使用,但灯会使用obj.data.color
作为它的光。