Skip to main content
added 38 characters in body; edited title
Source Link
Marty Fouts
  • 33.3k
  • 10
  • 36
  • 79

How to assign a material through pythoPython

I have an .obj file that contains hundreds of materials. These are however not correctly named. The vast majority of them are duplicates of the original materials. Somehow every instance of the original materials have been assigned their own variant of that material.

For example we have mtl_brick, and in every other instance where that material is assigned a duplicate has been created called, mtl_brick.001. In some cases, where that material is assigned in a lot of places, there are more than 50 variants of this.

The material variants are exactly the same they just have different names. So I tried to rename it correctly using its Base Color texture name. So in our example, mtl_brick would use brick_c.png. I tried to write a simple script doing the renaming.

import bpy

i = 0
while i < len(bpy.data.materials):
    mat = bpy.data.materials[i]
    if mat.name != 'Dots Stroke' and mat.name != 'Material':
        mat = bpy.data.materials[i]
        l = len(mat.node_tree.nodes['Image Texture'].image.name)
        mat.name = mat.node_tree.nodes['Image Texture'].image.name[l:-6]
    i = i + 1

This script does not work. When it encounters a duplicate it simply renames it into Material.001,002,003 etc.

The process that I want to script, and not do manually is going into the Material Properties tab, selecting the variant materials, Browse Material to be linked and Choose Material data-block to be assigned to this user, in order to reassign the variant materials to the original.

enter image description herescreen shot of material slots showing multiple identical materials

How do I use python scripting to re-assign all of these variants to its original material?

How to assign a material through pytho

I have an .obj file that contains hundreds of materials. These are however not correctly named. The vast majority of them are duplicates of the original materials. Somehow every instance of the original materials have been assigned their own variant of that material.

For example we have mtl_brick, and in every other instance where that material is assigned a duplicate has been created called, mtl_brick.001. In some cases, where that material is assigned in a lot of places, there are more than 50 variants of this.

The material variants are exactly the same they just have different names. So I tried to rename it correctly using its Base Color texture name. So in our example, mtl_brick would use brick_c.png. I tried to write a simple script doing the renaming.

import bpy

i = 0
while i < len(bpy.data.materials):
    mat = bpy.data.materials[i]
    if mat.name != 'Dots Stroke' and mat.name != 'Material':
        mat = bpy.data.materials[i]
        l = len(mat.node_tree.nodes['Image Texture'].image.name)
        mat.name = mat.node_tree.nodes['Image Texture'].image.name[l:-6]
    i = i + 1

This script does not work. When it encounters a duplicate it simply renames it into Material.001,002,003 etc.

The process that I want to script, and not do manually is going into the Material Properties tab, selecting the variant materials, Browse Material to be linked and Choose Material data-block to be assigned to this user, in order to reassign the variant materials to the original.

enter image description here

How do I use python scripting to re-assign all of these variants to its original material?

How to assign a material through Python

I have an .obj file that contains hundreds of materials. These are however not correctly named. The vast majority of them are duplicates of the original materials. Somehow every instance of the original materials have been assigned their own variant of that material.

For example we have mtl_brick, and in every other instance where that material is assigned a duplicate has been created called, mtl_brick.001. In some cases, where that material is assigned in a lot of places, there are more than 50 variants of this.

The material variants are exactly the same they just have different names. So I tried to rename it correctly using its Base Color texture name. So in our example, mtl_brick would use brick_c.png. I tried to write a simple script doing the renaming.

import bpy

i = 0
while i < len(bpy.data.materials):
    mat = bpy.data.materials[i]
    if mat.name != 'Dots Stroke' and mat.name != 'Material':
        mat = bpy.data.materials[i]
        l = len(mat.node_tree.nodes['Image Texture'].image.name)
        mat.name = mat.node_tree.nodes['Image Texture'].image.name[l:-6]
    i = i + 1

This script does not work. When it encounters a duplicate it simply renames it into Material.001,002,003 etc.

The process that I want to script, and not do manually is going into the Material Properties tab, selecting the variant materials, Browse Material to be linked and Choose Material data-block to be assigned to this user, in order to reassign the variant materials to the original.

screen shot of material slots showing multiple identical materials

How do I use python scripting to re-assign all of these variants to its original material?

deleted 15 characters in body; edited title
Source Link

(scripting) How to assign a material through python?pytho

I have an .obj file that contains hundreds of materials. These are however not correctly named. The vast majority of them are duplicates of the original materials. Somehow every instance of the original materials have been assigned their own variant of that material.

forFor example. We we have mtl_brickmtl_brick, and in every other instance where that material is assigned a duplicate has been created called, mtl_brick.001mtl_brick.001. In some cases, where that material is assigned in a lot of places, there are more than 50 variants of this.

The material variants are exactly the same they just have different names. So I tried to rename it correctly using its 'Base Color'Base Color texture name. So in our example, mtl_brickmtl_brick would use brick_c.pngbrick_c.png. I tried to write a simple script doing the renaming.

import bpy

i = 0
while i < len(bpy.data.materials):
    mat = bpy.data.materials[i]
    if mat.name != 'Dots Stroke' and mat.name != 'Material':
        mat = bpy.data.materials[i]
        l = len(mat.node_tree.nodes['Image Texture'].image.name)
        mat.name = mat.node_tree.nodes['Image Texture'].image.name[l:-6]
    i = i + 1

This script does not work. When it encounters a duplicate it simply renames it into Material.001Material.001,002002,003003 etc.

The process that I want to script, and not do manually is going into the "Material Propreties"Material Properties tab, selecting the variant materials, "Browse Material to be linked"Browse Material to be linked and "Choose Material data-block to be assigned to this user"Choose Material data-block to be assigned to this user, in order to reassign the variant materials to the original.

enter image description here

How do I use python scripting to re-assign all of these variants to its original material?

(scripting) How to assign a material through python?

I have an .obj file that contains hundreds of materials. These are however not correctly named. The vast majority of them are duplicates of the original materials. Somehow every instance of the original materials have been assigned their own variant of that material.

for example. We have mtl_brick, and in every other instance where that material is assigned a duplicate has been created called, mtl_brick.001. In some cases, where that material is assigned in a lot of places, there are more than 50 variants of this.

The material variants are exactly the same they just have different names. So I tried to rename it correctly using its 'Base Color' texture name. So in our example, mtl_brick would use brick_c.png. I tried to write a simple script doing the renaming.

import bpy

i = 0
while i < len(bpy.data.materials):
    mat = bpy.data.materials[i]
    if mat.name != 'Dots Stroke' and mat.name != 'Material':
        mat = bpy.data.materials[i]
        l = len(mat.node_tree.nodes['Image Texture'].image.name)
        mat.name = mat.node_tree.nodes['Image Texture'].image.name[l:-6]
    i = i + 1

This script does not work. When it encounters a duplicate it simply renames it into Material.001,002,003 etc.

The process that I want to script, and not do manually is going into the "Material Propreties" tab, selecting the variant materials, "Browse Material to be linked" and "Choose Material data-block to be assigned to this user", in order to reassign the variant materials to the original.

enter image description here

How do I use python scripting to re-assign all of these variants to its original material?

How to assign a material through pytho

I have an .obj file that contains hundreds of materials. These are however not correctly named. The vast majority of them are duplicates of the original materials. Somehow every instance of the original materials have been assigned their own variant of that material.

For example we have mtl_brick, and in every other instance where that material is assigned a duplicate has been created called, mtl_brick.001. In some cases, where that material is assigned in a lot of places, there are more than 50 variants of this.

The material variants are exactly the same they just have different names. So I tried to rename it correctly using its Base Color texture name. So in our example, mtl_brick would use brick_c.png. I tried to write a simple script doing the renaming.

import bpy

i = 0
while i < len(bpy.data.materials):
    mat = bpy.data.materials[i]
    if mat.name != 'Dots Stroke' and mat.name != 'Material':
        mat = bpy.data.materials[i]
        l = len(mat.node_tree.nodes['Image Texture'].image.name)
        mat.name = mat.node_tree.nodes['Image Texture'].image.name[l:-6]
    i = i + 1

This script does not work. When it encounters a duplicate it simply renames it into Material.001,002,003 etc.

The process that I want to script, and not do manually is going into the Material Properties tab, selecting the variant materials, Browse Material to be linked and Choose Material data-block to be assigned to this user, in order to reassign the variant materials to the original.

enter image description here

How do I use python scripting to re-assign all of these variants to its original material?

Source Link

(scripting) How to assign a material through python?

I have an .obj file that contains hundreds of materials. These are however not correctly named. The vast majority of them are duplicates of the original materials. Somehow every instance of the original materials have been assigned their own variant of that material.

for example. We have mtl_brick, and in every other instance where that material is assigned a duplicate has been created called, mtl_brick.001. In some cases, where that material is assigned in a lot of places, there are more than 50 variants of this.

The material variants are exactly the same they just have different names. So I tried to rename it correctly using its 'Base Color' texture name. So in our example, mtl_brick would use brick_c.png. I tried to write a simple script doing the renaming.

import bpy

i = 0
while i < len(bpy.data.materials):
    mat = bpy.data.materials[i]
    if mat.name != 'Dots Stroke' and mat.name != 'Material':
        mat = bpy.data.materials[i]
        l = len(mat.node_tree.nodes['Image Texture'].image.name)
        mat.name = mat.node_tree.nodes['Image Texture'].image.name[l:-6]
    i = i + 1

This script does not work. When it encounters a duplicate it simply renames it into Material.001,002,003 etc.

The process that I want to script, and not do manually is going into the "Material Propreties" tab, selecting the variant materials, "Browse Material to be linked" and "Choose Material data-block to be assigned to this user", in order to reassign the variant materials to the original.

enter image description here

How do I use python scripting to re-assign all of these variants to its original material?