The function was using the `BKE_id_material_len_p` with the ID of the object rather than the object data ID, which is what the function expects. Instead make sure to use `BKE_object_material_len_p` to get the right number of material slots. Additionally use the correct slot in `BKE_object_material_get` which is the index of the material + 1. Pull Request: https://projects.blender.org/blender/blender/pulls/145323