0
$\begingroup$

Textures don't shows in 3d view after loading them into node via python. I can see them in render mode, but not in edit. Also after selecting image texture node they show-up in 3d view, but I can't do that from script. Script works with preloaded (with another script) images

import bpy
import os
import os.path
os.system('cls')

bpy.context.scene.render.engine = 'CYCLES'

tex_name_list = []
mat_names = []
for ob in bpy.data.objects:
   print(ob.name)
   for mat_slot in ob.material_slots:
      for mtex_slot in mat_slot.material.texture_slots:
         if mtex_slot:
            if hasattr(mtex_slot.texture , 'image'):   
               mat_name = "{}".format(mtex_slot.name)
               for img in bpy.data.images:
                  #print(img.name)
                  mat = mat_slot.material
                  if img.name == (mat_name):
                     if not mat.use_nodes:
                        print("{} - {}".format(img.name, mat_name))
                        mat.use_nodes = True
                        tex_node = mat.node_tree.nodes.new('TEX_IMAGE')
                        mixShad_node = mat.node_tree.nodes.new('MIX_SHADER')
                        trns_node = mat.node_tree.nodes.new('BSDF_TRANSPARENT')
                        diff_node = mat.node_tree.nodes['Diffuse BSDF']
                        matOut_node = mat.node_tree.nodes['Material Output']
                        mat.node_tree.links.new(tex_node.outputs['Color'], diff_node.inputs['Color'])
                        mat.node_tree.links.new(tex_node.outputs['Alpha'], mixShad_node.inputs['Fac'])
                        mat.node_tree.links.new(diff_node.outputs['BSDF'], mixShad_node.inputs[2])
                        mat.node_tree.links.new(trns_node.outputs['BSDF'], mixShad_node.inputs['Shader'])
                        mat.node_tree.links.new(mixShad_node.outputs['Shader'], matOut_node.inputs['Surface'])                     
                        tex_node.image = img
$\endgroup$
  • $\begingroup$ Often a tag redraw or view layer update (or some update) is required to redraw the viewport. Can you elaborate re code? Eg In this recent a: blender.stackexchange.com/a/159001/15543 if you remove the tag redraw, the outliner doesn't refresh until you mouse over it. $\endgroup$ – batFINGER Nov 25 '19 at 10:59
  • $\begingroup$ Update doesn't work $\endgroup$ – Ing Nov 25 '19 at 11:22
  • $\begingroup$ @batFINGER Maybe it's about unlinked textures in uv? $\endgroup$ – Ing Nov 25 '19 at 12:03
  • $\begingroup$ @batFINGER You know maybe how to select node by script? Because that works in blender for me, but I don't how to do it with python. $\endgroup$ – Ing Nov 27 '19 at 21:46
0
$\begingroup$

Ok got it finally. You need set active Image Texture node:

mat = bpy.data.materials['<Your material name>']
mat.node_tree.nodes.active = mat.node_tree.nodes.new('TEX_IMAGE')
$\endgroup$

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.