0
$\begingroup$

I want to analyze the diffuse texture image and normal texture image from obj by using blender python api. So I tried

import bpy
bpy.ops.import_scene.obj(filepath="Kake.obj")
objects = bpy.data.objects
for object in objects:
    for s in object.material_slots:
        if s.material and s.material.use_nodes:
            for n in s.material.node_tree.nodes:
                if n.type == 'TEX_IMAGE':
                    print(object.name, 'uses', n.image.name, 'saved at', n.image.filepath,n.color_space)

And the mtl like this

newmtl CL_LR_Baked1
    map_Kd CL_LR_01DiffuseMap.jpg
    map_bump CL_LR_01NormalsMap.jpg
newmtl 03___Default
    map_Kd CL_LR_02DiffuseMap.jpg
    map_bump CL_LR_02NormalsMap.jpg

But I found I can't know which node is diffuse and which node is normal.Because their name is "Image Texture","Image Texture.001" . Is there any way to help?

$\endgroup$
1
$\begingroup$

I solve it by material.node_tree.links.

for object in objects:
    for s in object.material_slots:
        if s.material and s.material.use_nodes:
            for link in s.material.node_tree.links:
                if link.to_node.name == "Principled BSDF" and link.to_socket.identifier == "Base Color":
                    if link.from_node.type == 'TEX_IMAGE':
                        print(s.material.name, "diffuse",link.from_node.image.filepath)
                if link.to_node.name == "Normal Map" and link.to_socket.identifier == "Color":
                    if link.from_node.type == 'TEX_IMAGE':
                        print(s.material.name, "normal", link.from_node.image.filepath)
$\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.