# How can I tell a For Loop to do something based on an object's custom property?

I have two spheres, both have a MATTYPE custom property, one has MATTYPE: A, and one has MATTYPE:B. I want to do something based on the result of MATTYPE. I'm doing a for loop and can do one thing to all the spheres in this example, but I want to be able to do two different things based on the result of MATTYPE.

• maybe this helps you: datacamp.com/community/tutorials/… Jul 6 at 15:37
• This helps, it doesn't go into how one might use an object's custom property to do something, which is the part I'm struggling with Jul 6 at 15:49

You can access the custom property in the form object["property_name"], for example if you created a few spheres with different property MATTYPE, you could iterate over them to print something different for each value of the custom property in this way:

import bpy

spheres = [o for o in bpy.data.objects if o.name.startswith('Sphere')]

for sphere in spheres:
mattype = sphere["MATTYPE"]
if mattype == 0:
color = "red"
elif mattype == 1:
color = "green"
else:
color = "blue"
print(f'sphere "{sphere.name}" should have {color} color')

• I can tell this is almost exactly what I need, I'm not actually working with spheres. During mattype = sphere["MATTYPE"] it's going to throw an error up because "sphere" is going to be called any number of things. At the moment I'm doing Jul 6 at 16:03
• bpy.ops.object.mode_set(mode='VERTEX_PAINT') for object in context.selected_objects: mattype = object["MATTYPE"] if mattype == 0: context.view_layer.objects.active = object bpy.ops.object.mode_set(mode='VERTEX_PAINT') bpy.data.brushes["Draw"].color = (0.057191, 0.292894, 1) else: bpy.data.brushes["Draw"].color = (1, 1, 1) bpy.ops.paint.vertex_color_set() bpy.ops.object.mode_set(mode='OBJECT') ## but its going to error where sphereis Jul 6 at 16:04
• @frog2nite better put that in question in code formatting. Keep in mind it doesn't matter that I named a variable on line 5 sphere, what matters is that on line 3 I'm looking for objects which name starts with a Sphere. Jul 6 at 16:09
• I think you've understood the problem dispite my fo-par lack of putting my question into coding formatting. I'll have the object selected, can I do something with that? Jul 6 at 16:11
• @frog2nite not sure what you mean, you can use bpy.context.selected_objects and bpy.context.active_object Jul 6 at 16:20