How do I assign a material to object\face using python

I created some materials that have textures using the UPBGE editor.

What I would like to know is how would I be able to assign them to a block or a specific face of a block.

The goal is that in my game I spawn in my blocks and assign all their properties including textures upon creation. This will allow me to create one base block that I create all of my other block instances from with out having to make one block for every block type and have them sit in an inactive layer.
For example: If I have 3 wood type blocks, Why can’t I just create one base wood block then just spawn an instance of that block and assign it a different texture?

So what I am asking is: How do I assign the texture material to the block when I spawn it, and because each face could have a separate texture, how would I select what face I want the texture material to be assigned to?

I see that there is this function: bpy.ops.object.material_slot_assign()

Would it be possible to use that, and how do I use it?

Since your question is about UPBGE you might consider asking this in the game engine subforum as there are sometimes big differences between the python API. The answers you get here may not be accurate since this subforum is for blender python specifically.

Good call, moved :slight_smile:

you could create the meshes in advance and replace them in game with

cont.owner.replaceMesh(“yourMesh”)

If you want more advanced you should look into Adjusting objects UV coords, so you can make an atlas and move the UV.

When I run your code I keep getting an error that says the Texture is not available. I even made sure to set the path to the texture files.

This is the line causing the problem: obj[‘tex’] = texture.Texture(obj, matID)

I though about that, but that would mean that I would have to have hundreds of inactive meshes loaded in memory. I am making a Minecraft clone and I am trying to figure out how to create all the blocks and items, and I thought the best way to do so would be to make one base block and when I spawn an instance of it I just change the texture and what ever other properties to make what ever block I need, because in Minecraft, 90% of blocks are all the same shape they just have different textures or other properties. Then you also have overlay textures( The breaking animation).

  1. Make sure if you test the .blend file, unpack it, do run it directly from within the .zip file.

  2. New texture name is defined on the object in a property

  3. path should be altered if you use an other location

  4. Works fine in 2.5 no clue if it works in 3.0

I did unpack the file. The textures folder is in the same location as the blend file. And, I even directly added the name of the file to the path. I am using UPBGE 0.30

I fixed it. Turns out that I needed to set a default texture for the material before the texture could be changed.

So now that I have that, how do I display overlay textures?
How would I be able to display a breaking animation on a block as I mine it, or how would I display color layer over a grey scale image like you would for a grass texture to create different colors of grass.

Another thing to note is that if I have more than one object using the same material, then changing the texture on one changes it for all. So, I would have to create a new material for each texture then just assign the material to the object on creation, but if I have materials that are not linked to an object I get a warning about performance decrease due to empty render. Is this something to be concerned about?

I believe it was with a second UV group, they make blood stains trough this as well, not completely sure how to do it but google would help you with it, also no clue if 3.0 can do that.

Nope, it’s just a message saying engine will render/prepare the material even if it isn’t being used, you going to use it so no problem, the warning can be ignored.

You could also just make a few cubes, like: ground.water,magma, then just alter them to be used for that purpose.

Thank you.
One more question, How do I assign the material to the object when I spawn it in?
(How do I set it as the material that is being used by the mesh?)

Use object color to offset the UV
(add obj color to uv in shader graph)

Animate it, using constant keyframe.

Play the animation per object.