简体   繁体   中英

I have a serious issue about Python scripting in Blender

Recently I have started to build up a 3D game engine using mainly C++ and OpenGL, but I would like to write a script in Python to export some objects from the actual scene to a file. Everything has been succeeded, except I have some issue about how does the Python API works with objects and different types of objects?

Code:

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)

END OF CODE!!

For example, if I would like to go through all lamps and write some of their properties into a file, (name, color, parent object, etc.) some of a lamp's properties aren't recognized by Python as variables used by different objects. The same thing happens if I go through every objects and first get the type of the object (which is actually a lamp), but the console shows up an error message showing that for example spot radius or anything else is not an attribute of a "Blender Object". In the previous case I have explained that the Python doesn't realize that a "Blender Lamp" is actually a "Blender Object", but "Blender Lamp"s should also keep their original properties inherited from "Blender Object"s as I think. Because in Blender, whatever type an object has, it has a position rotation, scale and etc. So far, as you know every lamp has position (like objects have) and also light properties (light color etc.) But if I would like to get the position of a Blender Lamp, it doesn't work, because it shows that kind of a lamp is not an object but in Blender a lamp also have location and everything like normal objects. I also didn't find references to positions of lights in Blender 2.49 python api documentation.

Please Help! Thanks in advance...

PS sorry for english i am from hungary and have no profession. and some of the variables i wrote in hungarian, but i hope you understand the issue. thx

Are you still using 2.49? It is very old now and if your not using 2.49 then the 2.49 API docs aren't going to help as from 2.50 everything changed with python. If you are using a more recent version then you should find the current blender API documentation more helpful.

Using a recent version of blender the following should be helpful -

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)

Don't confuse obj.color with obj.data.color . obj.color is an object property that is available to all objects but a lamp uses obj.data.color for it's light.

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