It looks like it would be easy to add a texture to a material using Python, but no matter what i do i cant figure it out!

I can create a texture using:

bpy.data.textures.new("NewTexture", type='IMAGE')

and I can create a new material texture slot:


However i can't link the texture to the Material Slot Texture. I have tried looking through the python API but nothing is working. I would be eternal grateful if someone could point me in the right direction.

  • $\begingroup$ What render engine are you using? Internal? $\endgroup$ – p2or Mar 10 '16 at 13:04
  • $\begingroup$ I was using the blender internal to automatically find the basecolor, roughness, and normal channels to quickly create a game ready material so i could see how the models look before i bring them into the game engine. $\endgroup$ – Ryan Grzesiak Mar 12 '16 at 1:04

Similar to material_slot.material, there is a texture_slot.texture

(I'm assuming Blender internal from the texture_slots.add in q)

mat = bpy.data.materials['Material']
tex = bpy.data.textures.new("SomeName", 'IMAGE')
slot = mat.texture_slots.add()
slot.texture = tex

A simple example using Cycles can be found here https://blender.stackexchange.com/a/14115/15543

| improve this answer | |
  • 1
    $\begingroup$ You are awesome! That was exactly what i was looking for and it worked. Thanks for the link to the Cycles as well $\endgroup$ – Ryan Grzesiak Mar 11 '16 at 4:31
  • 2
    $\begingroup$ That doesn't work in 2.8. There is not material.texture_slots anymore. There is now both material.texture_paint_images and material.texture_paint_slots, but both don't have an add() method. No idea so far how the 2.8 equivalent should look like. $\endgroup$ – bluenote10 Oct 10 '19 at 8:56
  • 4
    $\begingroup$ I have found this question which covers that. $\endgroup$ – bluenote10 Oct 10 '19 at 10:40
  • $\begingroup$ @bluenote10 Thank you so much! I was stuck here. PS THE ABOVE ANSWER IS WRONG FOR BLENDER 2.8. $\endgroup$ – Bob Denny May 1 at 0:07
  • $\begingroup$ ... surprising that an answer from 2016 aimed at blender internal render engine doesn't work in 2.8. Thanks for the heads up. $\endgroup$ – batFINGER May 1 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.