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.

  • $\begingroup$ maybe this helps you: datacamp.com/community/tutorials/… $\endgroup$
    – Chris
    Jul 6 at 15:37
  • $\begingroup$ 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 $\endgroup$
    – frog2nite
    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"
        color = "blue"
    print(f'sphere "{sphere.name}" should have {color} color')
  • $\begingroup$ 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 $\endgroup$
    – frog2nite
    Jul 6 at 16:03
  • $\begingroup$ 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 $\endgroup$
    – frog2nite
    Jul 6 at 16:04
  • $\begingroup$ @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. $\endgroup$ Jul 6 at 16:09
  • $\begingroup$ 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? $\endgroup$
    – frog2nite
    Jul 6 at 16:11
  • $\begingroup$ @frog2nite not sure what you mean, you can use bpy.context.selected_objects and bpy.context.active_object $\endgroup$ Jul 6 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.