Seting an objects material via python

In a game i am making im going to make a kind of landscape transformation, i want to keep it simple so im only going to change the coloring.

I have set up two materials:
MALightClouds and
MADarkClouds

I have figured out how to find these materials in my mesh via python but i could not figure out how to set them, am i using outdated code? other then the whole GameLogic thing i know thats old.

here is my script

import GameLogic
sky = GameLogic.getCurrentScene().objects[“Gate Clowds”]
skypoly = sky.meshes[0].getPolygon(0)
skymat = skypoly.getMaterial()
sky.meshes[0].materials = “MALightClouds”

When i did this it told me that the kx_MeshProxy objects are not writable. Can this not be done in the game engine?

ok apperently there is no work around. i will just use scenes and change scenes to switch the materials

You may use replaceMesh, which will replace the materials if you replace the graphics mesh. it is frustrating that such elements are not writeable, but for now there is little we can do but suggest that somebody takes on the role of adding such functionality.

You could also use bge.texture to replace one of the active textures with one loaded from disk. But yeah, unfortunately, you can’t switch materials on the fly.

I’m also intereseted in that question, but still confused after search in the forum…

So, isn’t possible, although where saved in the project?
I have seen also some examples like x.setMaterial but guess is not working in actually API
but debugger response something like object hasn’t this attribute

Not really, no. You can swap textures (and also swap them on any duplicate meshes), or swap meshes, but not change materials. I’m not sure, but I’m guessing the setMaterial function is for Blender’s API, not for the BGE.

Thank you for the clarification :thumbup:, I resolve another question I had posted in another thread.