# Seting an objects material via python

**URL:** <https://blenderartists.org/t/seting-an-objects-material-via-python/557603>\
**Category:** Game Engine Support and Discussion\
**Created:** [November 22, 2012, 1:20pm UTC](https://blenderartists.org/t/seting-an-objects-material-via-python/557603 "2012-11-22T13:20:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kylona](https://blenderartists.org/user_avatar/blenderartists.org/kylona/32/18252_2.png) [@Kylona](https://blenderartists.org/u/Kylona)\
**Post date:** [November 22, 2012, 1:20pm UTC](https://blenderartists.org/t/seting-an-objects-material-via-python/557603/1 "2012-11-22T13:20:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kylona](https://blenderartists.org/user_avatar/blenderartists.org/kylona/32/18252_2.png) [@Kylona](https://blenderartists.org/u/Kylona)\
**Post date:** [November 25, 2012, 3:05pm UTC](https://blenderartists.org/t/seting-an-objects-material-via-python/557603/2 "2012-11-25T15:05:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![agoose77](https://blenderartists.org/user_avatar/blenderartists.org/agoose77/32/442949_2.png) [@agoose77](https://blenderartists.org/u/agoose77)\
**Post date:** [November 25, 2012, 3:14pm UTC](https://blenderartists.org/t/seting-an-objects-material-via-python/557603/3 "2012-11-25T15:14:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SolarLune](https://blenderartists.org/user_avatar/blenderartists.org/solarlune/32/7258_2.png) [@SolarLune](https://blenderartists.org/u/SolarLune)\
**Post date:** [November 25, 2012, 3:35pm UTC](https://blenderartists.org/t/seting-an-objects-material-via-python/557603/4 "2012-11-25T15:35:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mandango](https://blenderartists.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mandango](https://blenderartists.org/u/mandango)\
**Post date:** [December 22, 2012, 1:08pm UTC](https://blenderartists.org/t/seting-an-objects-material-via-python/557603/5 "2012-12-22T13:08:19Z")

</div>

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

> [@SolarLune](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![SolarLune](https://blenderartists.org/user_avatar/blenderartists.org/solarlune/32/7258_2.png) [@SolarLune](https://blenderartists.org/u/SolarLune)\
**Post date:** [December 22, 2012, 4:38pm UTC](https://blenderartists.org/t/seting-an-objects-material-via-python/557603/6 "2012-12-22T16:38:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mandango](https://blenderartists.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mandango](https://blenderartists.org/u/mandango)\
**Post date:** [January 16, 2013, 4:19pm UTC](https://blenderartists.org/t/seting-an-objects-material-via-python/557603/7 "2013-01-16T16:19:52Z")

</div>

> [@SolarLune](#):
>
> 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.
