Assigning materials to objects of the same name(ERROR)

I searched the internet and found a very useful Python script.
This script has the ability to automatically assign materials with the same name to multiple objects in Blender.
By the way. When I run the script. I see an error message and it does not work. How can I fix it I need your help.

You could start by following the instructions you have conveniently circled. Under the Window menu click ‘toggle system console’ and post your error here. You could also copy and paste the code here, or provide a link to where you found it. Very few people are going to be generous enough to transcribe your screenshot in order to reproduce your issue.

1 Like

Sorry. I lacked my thoughts.

import bpy

for obj in bpy.data.objects:
objName = obj.name
mat = bpy.data.materials.get(objName)
if(mat != None):
if(mat.name not in obj.data.materials):
obj.data.materials.append(mat)
obj.active_material_index = len(obj.data.materials)-1
else:
obj.active_material_index = [*obj.data.materials].index(mat)
else:
print(‘No material available for’, objName)


No material available for building_08.007
No material available for building_08.008
No material available for building_08.009
No material available for building_08.010
No material available for building_08.011
No material available for building_08.012
No material available for building_08.013
No material available for Bus_toy:Bus_Geo_Group
No material available for Bus_toy:bus_Grp
No material available for Bus_toy:door.001
No material available for Bus_toy:Geometry_Group
No material available for Bus_toy:handle
No material available for Bus_toy:Hi
No material available for Bus_toy:Lo
No material available for CAMERA_EP0017_SC005_F100_F156:Camera_grp
No material available for CAMERA_EP0017_SC005_F100_F156:ctrl_in_1
No material available for CAMERA_EP0017_SC005_F100_F156:ctrl_in_2
No material available for CAMERA_EP0017_SC005_F100_F156:ctrl_master1
No material available for CAMERA_EP0017_SC005_F100_F156:TEST_CAMERA_00
No material available for Cube
No material available for driver_hat_coco:driver_hat2
No material available for driver_hat_coco:driver_hat_coco_ctl
No material available for driver_hat_coco_ctl_parentConstraint1
No material available for Empty
Traceback (most recent call last):
File “D:\baby_yoyo\EG_17\SCENE\S05.blend\Text”, line 7, in
AttributeError: ‘NoneType’ object has no attribute ‘materials’
Error: Python script failed, check the message in the system console

for future reference, you can format your code using three backticks `

import bpy

for obj in bpy.data.objects:
    objName = obj.name
    mat = bpy.data.materials.get(objName)
    if(mat != None):
        if(mat.name not in obj.data.materials):
            obj.data.materials.append(mat)
            obj.active_material_index = len(obj.data.materials)-1
        else:
            obj.active_material_index = [*obj.data.materials].index(mat)
    else:
            print(‘No material available for’, objName)

The error you’re getting is on line 7:

if(mat.name not in obj.data.materials):

The error says 'NoneType' object has no attribute ‘materials’, this means the object it’s currently working on has no data, which is probably an Empty. You should add a check for hasattr.

if hasattr(obj.data, 'materials'):

2 Likes

Thank you for answer.
With your kind teachings, I will fix my problems. Sorry for the late expression of gratitude.

1 Like