Import and replace nodes

I would like to be able to replace in script python in all materials of the selected objects

the node “DIFFUSE” with the BASESKP group ,
and to connect color with base color and Alpha with alpha
this for a scene hundreds of objects and dozens of materials
Thanks for your help

Not sure what exactly you want.you can build node groups with ctrl-g.for me it sounds like you want group instancing with such high numbers of objects.however you can assign/select materials to linked objects for example.

I have many nodes with an assigned image and I just want to change a part of these because the images are so many

for each material I have to keep the image and change the knot spread with a group

maybe baking is a option for you?

I need to change A to B in all the materials in the selected objects without touching the image that is not the same in the various materials
PS
are old files from sketch-up

Tanks

hmm ok not sure, maybe a skript could do delete and insert a node/grp ?sry i cant help you with that much then.

what you can try is copy your node grp to the clipboard (ctrl-c) and paste it (ctrl-v) in all materials by hand.after that connect the inputs.its maybe a bit time consuming, but if you have not thousend of materials its doable i think.(if all materials look the same like this diffuse only mat)

or ask for help with the script in the python support section first,its maybe very easy to do.
https://blenderartists.org/c/coding/python-support

I won’t write a complete script, but here’s the main helpers:

You need to loop over all materials.

for material in bpy.data.materials:
     nodetree = material.node_tree

then you need to loop over the nodes in the material’s nodetree:

for node in nodetree.nodes:
    if node.type=="BSDF_DIFFUSE":

after you have the diffuse node, you need to store it’s input and output links

inputnode1 = node.inputs['Color'].links[0].from_node
inputsocket1 = node.inputs['Color'].links[0].from_socket
outputnode = node.outputs['BSDF'].links[0].to_node
outputsocket = node.outputs['BSDF'].links[0].to_socket

delete the node, create a new group_node and point it to the “BASE_SKP” node_tree.

nodetree.nodes.remove(node)
newnode = nodetree.nodes.new('ShaderNodeGroup')
newnode.node_tree = bpy.data.node_groups['BASE SKP']

And then recreate the links again.

nodetree.links.new(input, output)

import bpy

for material in bpy.data.materials:
nodetree = material.node_tree

 for node in nodetree.nodes:
    if node.type=="BSDF_DIFFUSE":inputnode1 = node.inputs['Color'].links[0].from_node
    inputsocket1 = node.inputs['Color'].links[0].from_socket
    outputnode = node.outputs['BSDF'].links[0].to_node
    outputsocket = node.outputs['BSDF'].links[0].to_socket
    nodetree.nodes.remove(node)
    newnode = nodetree.nodes.new('ShaderNodeGroup')
    newnode.node_tree = bpy.data.node_groups['BASE SKP']
    nodetree.links.new(input, output)

NOT work for me ;-( and not create link from image and link from alpha ;-(
help please :wink:

just copying what i’ve posted isn’t suposed to work… I’ll cite myself:

A bit of knowledge of Python and the API is needed to glue what I’ve posted together, and it seems you have neither…
You should really learn it, if you’re already doing scenes with “hundreds of objects”.

import bpy


#  loop through 'Materials'
for material in bpy.data.materials:
    #  store the reference to the node_tree in a variable
    nodetree = material.node_tree
    
    #  loop through nodes in the nodetree
    for node in nodetree.nodes:
        #  if the node is a Diffuse node....
        if node.type=="BSDF_DIFFUSE":
            
            #  store the nodes that are connected to it
            inputnode = node.inputs['Color'].links[0].from_node
            outputnode = node.outputs['BSDF'].links[0].to_node
            
            #  remove the diffuse node
            nodetree.nodes.remove(node)
            
            #  add the nodegroup
            newnode = nodetree.nodes.new('ShaderNodeGroup')
            newnode.node_tree = bpy.data.node_groups['BASE SKP']
            
            #  relink everything
            nodetree.links.new(inputnode.outputs[1], newnode.inputs[0])
            nodetree.links.new(inputnode.outputs[0], newnode.inputs[1])
            nodetree.links.new(newnode.outputs[0], outputnode.inputs[0])
            
            #  supose that there aren't any more diffuse nodes around,
            #  we can escape the to the next material
            break()

EDIT: Corrected ‘.from_nodes’ to ‘.from_node’ and links based on ‘BASE_SKP’

Tanks

I try to learn it but I have to find a good text because I always block myself on stupid things :wink:


Garanti sans virus. www.avast.com

Le mar. 9 juil. 2019 à 17:44, Secrop via Blender Artists Community [email protected] a écrit :


Secrop

      Member




    July 9

ingoenius:
NOT work for me ;-(

just copying what i’ve posted isn’t suposed to work… I’ll cite myself:

Right at the start:
I won’t write a complete script, but here’s the main helpers:

A bit of knowledge of Python and the API is needed to glue what I’ve posted together, and it seems you have neither…

You should really learn it, if you’re already doing scenes with “hundreds of objects”.






import bpy
# loop through 'Materials'
for material in bpy.data.materials:
# store the reference to the node_tree in a variable
nodetree = material.node_tree
# loop through nodes in the nodetree
for node in nodetree.nodes:
# if the node is a Diffuse node....
if node.type=="BSDF_DIFFUSE":
# store the nodes that are connected to it
inputnode = node.inputs['Color'].links[0].from_nodes
outputnode = node.outputs['BSDF'].links[0].to_node
# remove the diffuse node
nodetree.nodes.remove(node)
# add the nodegroup
newnode = nodetree.nodes.new('ShaderNodeGroup')
newnode.node_tree = bpy.data.node_groups['BASE SKP']
# relink everything
nodetree.links.new(inputnode.outputs[0], newnode.inputs[0])
nodetree.links.new(inputnode.outputs[1], newnode.inputs[1])
nodetree.links.new(newnode.outputs[0], outputnode.inputs[0])
# supose that there aren't any more diffuse nodes around,
# we can escape the to the next material
break()


Visit Topic or reply to this email to respond.


In Reply To


ingoenius

      Member




    July 9

import bpy for material in bpy.data.materials: nodetree = material.node_tree for node in nodetree.nodes:
if node.type==“BSDF_DIFFUSE”:inputnode1 = node.inputs[‘Color’].links[0].from_node
inputsocket1 = node.inputs[‘Color’].links[0].from_socket
outputnode = node.outputs[‘BSDF’].links[…


Visit Topic or reply to this email to respond.

To unsubscribe from these emails, click here.


Garanti sans virus. www.avast.com

This is my base file


Garanti sans virus. www.avast.com

Le mar. 9 juil. 2019 à 17:52, michele Ingenuo [email protected] a écrit :

test scripting.blend (701 KB)

Noticed!! :stuck_out_tongue:

it seems i overread this info.

sketchup is very popular afaik,my self have not used it.however whats about all the import addons for blender?
I did a quick search and found something like this,this addon seems to import mesh and materials.
you have to install the addon in blender 2.79 ,import your sketchup file,and save the blend.after that you can open the blend in blender 2.8

maybe there are better addons that works direct with 2.8 i dont know,just try out or search for a better importer if its not working then.

How to import SketchUp files to Blender 2.8? • Blender 3D Architect

This tutorial does not work well for material conversion
and for this reason I need a script that allows me not to do everything by hand :wink: