Skip to content

Commit

Permalink
fixed issue where empty material slot will cause menu to break
Browse files Browse the repository at this point in the history
  • Loading branch information
yiannipapazis committed Mar 6, 2022
1 parent 86bae86 commit ce49840
Showing 1 changed file with 44 additions and 33 deletions.
77 changes: 44 additions & 33 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,24 @@ def draw(self, context):
material_list = get_materials_from_selected()
if material_list:
for mat in material_list:
layout.label(text=mat, icon='MATERIAL')
layout.separator()
mat_image_nodes = look_for_images_from_mat(
bpy.data.materials[mat])

# build menu for each material
if mat_image_nodes:
for image_node in mat_image_nodes:
o = layout.operator(
self.operator.bl_idname, text=image_node.image.name, icon='IMAGE_DATA')
o.image_name = image_node.image.name
try:
mat_obj = bpy.data.materials[mat]
except KeyError:
mat_obj = False
if mat_obj:
mat_image_nodes = look_for_images_from_mat(
mat_obj)
layout.label(text=mat, icon='MATERIAL')
layout.separator()
else:
layout.label(text="No images")
# build menu for each material
if mat_image_nodes:
for image_node in mat_image_nodes:
o = layout.operator(
self.operator.bl_idname, text=image_node.image.name, icon='IMAGE_DATA')
o.image_name = image_node.image.name
layout.separator()
else:
layout.label(text="No images")
else:
layout.label(text='No materials')

Expand Down Expand Up @@ -166,21 +170,27 @@ def draw(self, context):
if material_list:
layout.menu(reload_images_menu.bl_idname)
for mat in material_list:
layout.label(text=mat, icon='MATERIAL')
layout.separator()
mat_image_nodes = look_for_images_from_mat(
bpy.data.materials[mat])

# build menu for each material
if mat_image_nodes:
for image_node in mat_image_nodes:
o = layout.operator(
self.operator.bl_idname, text=image_node.image.name, icon='IMAGE_DATA')
o.mat_name = mat
o.node_name = str(image_node)
try:
mat_obj = bpy.data.materials[mat]
except KeyError:
mat_obj = False

if mat_obj:
layout.label(text=mat, icon='MATERIAL')
layout.separator()
else:
layout.label(text="No images")
mat_image_nodes = look_for_images_from_mat(
mat_obj)

# build menu for each material
if mat_image_nodes:
for image_node in mat_image_nodes:
o = layout.operator(
self.operator.bl_idname, text=image_node.image.name, icon='IMAGE_DATA')
o.mat_name = mat
o.node_name = str(image_node)
layout.separator()
else:
layout.label(text="No images")
else:
layout.label(text='No materials')

Expand All @@ -191,12 +201,13 @@ def get_images_from_objects():
materials = active_object.material_slots
for mat in materials:
mat = mat.material
if mat not in checked_mats:
checked_mats.append(mat)
found_images = look_for_images_from_mat(mat)
for img in found_images:
if img not in images:
images.append(img)
if mat:
if mat not in checked_mats:
checked_mats.append(mat)
found_images = look_for_images_from_mat(mat)
for img in found_images:
if img not in images:
images.append(img)
return images

def look_for_images_from_mat(mat = bpy.types.Material):
Expand Down

0 comments on commit ce49840

Please sign in to comment.