1
$\begingroup$

I have model from Sketchup which have like 700 materials and I need to create/paste specific image texture node in every material for baking one diffuse texture to later create one material.

My script is not working like I think it should. Can You help?

import bpy
object = bpy.context.object
for num in range(1, 700):
object.active_material_index = num
bpy.context.area.type = 'NODE_EDITOR'
bpy.ops.node.clipboard_paste()
$\endgroup$
1
$\begingroup$

I don't know exactly how to copy/paste nodes between materials, but this solution works anyway.

I suggest to have a reference to the image texture node you want to duplicate inside your blend file data. To do this, choose any material that will be used as source (srcmat in my example), and add the image texture node you want to duplicate in its node tree (set the image too). Assign a name to this node (mynode in my example) from the N sidebar.

setup

This lines of codes will create a new texture image node for all the other materials assigned to the object, then assign the texture you set in mynode node and finally link the texture to the color input of the Principled shader node:

import bpy
from mathutils import *


# replace 'srcmat' with the name of your source material
# replace 'mynode' with the name of your image texture node
srcmat = bpy.data.materials['srcmat']
srctxtnodeimg = bpy.data.images[srcmat.node_tree.nodes['mynode'].image.name]

# select your object first
o = bpy.context.active_object

# create the same node on all the other materials
for i in range(len(o.material_slots)):
    o.active_material_index = i
    m = bpy.data.materials[o.active_material_index]

    if m.name != srcmat.name:

      mnodes = m.node_tree.nodes      

      newnode = mnodes.new("ShaderNodeTexImage")
      newnode.image = srctxtnodeimg
      newnode.location = Vector((-100, 500))

      princshader = mnodes.get("Principled BSDF")
      m.node_tree.links.new(princshader.inputs[0], newnode.outputs[0])

P.s.: you can add other properties of the source node such as color space, interpolation or projection if you need. Moreover I assume that all your materials (created after import from Sketchup) use the Principled BSDF shader, which is (based on my experience) the default shader used by Blender in any situation (eg if you import a model).

$\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.