I have some objects with their corresponding materials defined as "Principled BSDF" I am trying to write a python script that transforms these materials to "Diffuse BSDF" and applies the corresponding texture image on them. For example currently I have the following:
Then this should be transformed to the following:
My texture files are already loaded so once I select that Color
should be Image Texture
I can then select the corresponding image file from the drop down list which matches my material name in this case it is Tiles07
with a sufix _2K_Color.jpg
I've tried to loop through each object material and apply each change gradually:
import bpy
def map_correct_materials(obj):
print("Obj name: {}".format(obj.name))
for m in obj.material_slots:
print("Material name: {}".format(m.name))
mat = m.material
if mat.use_nodes == True:
for node in mat.node_tree.nodes:
if node.type == 'BSDF_PRINCIPLED':
print("Node name: {}".format(node.name))
print("Node type: {}".format(node.type))
print("Node input Base Color: {}".format(node.inputs['Base Color']))
node.type = 'BSDF_DIFFUSE'
objs = [o for o in bpy.data.objects
if o.type == 'MESH' and not "Light" in o.name]
for obj in objs:
map_correct_materials(obj)
however I got a read only error when I tried to change the material type:
AttributeError: bpy_struct: attribute "type" from "ShaderNodeBsdfPrincipled" is read-only
Searching around I couldn't find a reason for that though. Thus, any help would be appreciated.
Thanks.
context.object.type = 'LATTICE'
to change an object into a lattice object. Hence will need to add a new BSDF_DIFFUSE node , make links to it using links of old, then remove old. Pretty sure there is a qa re exactly this. (or vice versa) @brockmann ? $\endgroup$layout.template_node_view(ntree, node, input)
Not an enum of the node type property. Not aware of these being exposed to python Have asked re the magic done bytemplate_constraint
using instance panels. Re (aforementioned) textures and changing type blender.stackexchange.com/questions/215897/… $\endgroup$