Linking material to Object from Python?

Is there a way to add an object-level material slot from Python? I don’t mean adding a material to a mesh, but adding it to an Object, like you can do in the material editor with setting the Link selector to Object, followed by assigning a material. [1] seems to suggest you can at least update an exisiting slot, but I don’t see methods to create one. E.g. Object.material_slots doesn’t have an append() method.

I’m doing flipbook animation by updating a placeholder object’s mesh depending on the frame number in a frame_change_pre handler. However, updating the mesh seems to remove the material linked to the object that I have set manually. I don’t want to link the material to the meshes in the animation as I bpy.data.libraries.load(link=True) them from individual blender files (one per timestep). I’d like to avoid yet another level of indirection of having to link from the timestep files to a file containing only a material. The material needs to be editable, hence my desire so simply set it on the placeholder object.

[1] http://blender.stackexchange.com/questions/53188/how-can-i-make-it-so-materials-are-linked-to-objects-instead-of-data-with-pytho

Was your issue solved? I am having a problem like yours.

Yes, there are several ways to add an object-level material slot in Python. One of the most common ways is to use the ctypes module to create C data structures that can be passed to the functions of the Blender library. For example, here’s how you can define a new object-level material slot: