Accessing linked objects meshes (LOD system with Python)

Hi guys. I’m creating a LOD system for my project. Now i have code that works fine with local objects. I’m simply replacing meshes for objects in arrays. But i want to make the code working with linked objects as well… So here’s the code:


import bge


scene = bge.logic.getCurrentScene()
lodObjects = [ob for ob in scene.objects if 'LOD' in ob]
lodObjectsLevel1 = [ob for ob in scene.objects if 'LOD1' in ob]
lodObjectsLevel2 = [ob for ob in scene.objects if 'LOD2' in ob]
player = scene.objects["Default"]


def main():
    for p, member in enumerate(lodObjects):
        originalObject = lodObjects[p]
        lodLevel1 = scene.objects[str(lodObjectsLevel1[p])]
        lodLevel2 = scene.objects[str(lodObjectsLevel2[p])]
        print (player.getDistanceTo(originalObject))
        if player.getDistanceTo(originalObject) < 20.0:
            originalObject.replaceMesh(str(originalObject),1,0)
            print ("__engine__LOD: "+str(originalObject)+" >> "+str(originalObject))
        if player.getDistanceTo(originalObject) > 20.0:
            originalObject.replaceMesh(str(lodLevel1),1,0)
            print ("__engine__LOD: "+str(originalObject)+" >> "+str(lodLevel1))
        if player.getDistanceTo(originalObject) > 40.0:
            originalObject.replaceMesh(str(lodLevel2),1,0)
            print ("__engine__LOD: "+str(originalObject)+" >> "+str(lodLevel2))

and here’s the object setup:


So, what is the real problem?
You could access the objects from an hidden layer. And if the objects are linked to that layer, they are taken into account on the first loops.
If you scene is busy, it’s best to use a switch (bool properties like 1,2 and 3 for each level) to replaceMesh, and maybe group all objects into 3 lists according to their distance. This will make you have 3 loops to check 4 distances and you can change the tics on each.
You can also save the meshes into an object’s property, so you wont have to look for them in a list… But that is a design choice…

Thank you your little tip about the object property works just fine! Before this change python stuck at creating the second array. Here’s the code so anyone can use it.


import bge

scene = bge.logic.getCurrentScene()
lodObjects = [ob for ob in scene.objects if 'LOD' in ob]
player = scene.objects["Default"]

def main():
    for p, member in enumerate(lodObjects):
        originalObject = lodObjects[p]
        print (player.getDistanceTo(originalObject))
        if player.getDistanceTo(originalObject) < 20.0:
            originalObject.replaceMesh(str(originalObject),1,0)
            print ("__engine__LOD: "+str(originalObject)+" >> "+str(originalObject))
        if player.getDistanceTo(originalObject) > 40.0:
            originalObject.replaceMesh(str(originalObject["LOD2"]),1,0)
            print ("__engine__LOD: "+str(originalObject)+" >> "+str(originalObject["LOD2"]))
        elif player.getDistanceTo(originalObject) > 20.0:
            originalObject.replaceMesh(str(originalObject["LOD1"]),1,0)
            print ("__engine__LOD: "+str(originalObject)+" >> "+str(originalObject["LOD1"]))

and the object setup:


the script setup in LogicBricks

maybe if anyone could thing of better script triggering? :slight_smile: