我对混合编程中的Python脚本有一个严重的问题

2024-05-29 03:38:20 发布

您现在位置:Python中文网/ 问答频道 /正文

最近我开始使用C++和OpenGL构建3D游戏引擎,但是我想用Python编写一个脚本,将一些对象从实际场景导出到文件。一切都成功了,除了我有一些关于pythonapi如何处理对象和不同类型对象的问题之外?在

代码:

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)

代码结束!!在

例如,如果我想浏览所有lamps并将它们的一些属性写入文件(名称、颜色、父对象等),Python无法将lamp的某些属性识别为不同对象使用的变量。如果我遍历每个对象并首先获得对象的类型(实际上是一个灯),则会发生同样的情况,但控制台显示一条错误消息,显示例如spot radius或其他任何东西都不是“Blender object”的属性。在前面的例子中,我已经解释过Python没有意识到“Blender Lamp”实际上是一个“Blender Object”,但是“Blender Lamp”也应该像我所想的那样保持从“Blender Object”继承的原始属性。因为在Blender中,不管一个物体有什么类型,它都有一个位置旋转,比例等等。到目前为止,你知道每个灯都有位置(就像物体一样)和光属性(光的颜色等等),但是如果我想得到一个混合灯的位置,它就不起作用了,因为它表明一种灯不是一个物体,但在搅拌机里,灯也有位置和一切和正常物体一样的东西。我在Blender2.49PythonAPI文档中也没有找到对灯光位置的引用。在

请帮忙! 提前谢谢。。。在

对不起,我是匈牙利人,没有职业。我用匈牙利语写了一些变量,但我希望你能理解这个问题。泰铢


Tags: 对象self类型属性havefilewritecolor
1条回答
网友
1楼 · 发布于 2024-05-29 03:38:20

你还在用2.49吗?它现在已经很旧了,如果你不使用2.49,那么2.49API文档将不会有帮助,因为从2.50开始,python的一切都改变了。如果您使用的是较新的版本,那么您应该会发现当前的blender API documentation更有帮助。在

使用最新版本的blender,以下内容会有所帮助-

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.colorobj.data.color混淆。obj.color是一个可用于所有对象的对象属性,但lamp使用obj.data.color作为灯光。在

相关问题 更多 >

    热门问题