# What is the low level alternative for bpy.ops.object.material_slot_add()?

I have a script which adds a material to an object. The script is using bpy.ops.object.material_slot_add(). However, this only works when the script is used from the text editor. It has polling/context errors when used differently.

What Blender 2.75a API commands provide the low level equivalent to bpy.ops.object.material_slot_add()?

• I use this operator often from the 3D view and console. It is not limited to the text editor. It also works in object mode, edit mode, sculpt mode, weight paint, texture paint... Where are you running the script from? You might have a panel in some specific screen that might be out of context, but you can probably override this context and run the operator anyway (blender.stackexchange.com/search?q=context+override) – TLousky Oct 12 '15 at 13:21
• The command will be used in a function which will be passed to the render handler: bpy.app.handlers.render_pre.append() . Because of challenges in getting the override set up properly and a desire to make create scripts which operate independent of the context, I'm trying to convert all bpy.ops to low level equivalents. – Ed Tate Oct 12 '15 at 13:26
• I don't think you have another way to add a material slot via python. I think I tried finding one before. Maybe some other person here knows something I don't. Anyway, try the solution in the answer below. Small trivial thing, but sometimes those are the hardest to debug. – TLousky Oct 12 '15 at 13:28

Example code

Adds two material slots, m1 (if there is already an active material slot it will replace it with m1) and m2

import bpy

mat1 = bpy.data.materials.new("m1")
mat2 = bpy.data.materials.new("m2")

ob = bpy.context.object

ob.active_material = mat1
ob.data.materials.append(mat2)


An empty slot can be added with

ob.data.materials.append(None)


The only case I can think of where this operator will not work is that the object you want to add a material slot to is not selected and active. Make sure of both in your script before calling the operator:

myObj.select = True
bpy.context.scene.objects.active = myObj