0
$\begingroup$

I've done an addon with a button that creates a color animation of sorts where the colors change with regular intervals. But my code only works for a cube. When I try to enable the code for selected objects the script runs fine, but when I press the button I'm getting an error:

Python: Traceback (most recent call last): line 84, in execute AttributeError: 'list' object has no attribute 'active_material'

What is wrong here?

The code for the color loop:

class COLORANIM_OT_my_operator(bpy.types.Operator):
"""Click OK and press space to start animation"""    
bl_label = "Color Animation"
bl_idname = "coloranim.my_operator"


def execute(self, context):

    mat = bpy.data.materials.new(name='colored')
    obj = bpy.context.selected_objects

    obj.active_material = mat

   
    colors = [(1, 1, 0, 1), #yellow
                (0, 1, 0, 1), #green
                (0, 0.7, 1, 1), #blue
                (0.7, 0, 1, 1), #purple
                (1, 0, 0, 1), #red 
                (0, 0, 0, 1),] #black
               
    frames = [1 , 30, 60, 90, 120, 150, 180]
 
    for f, c in zip(frames, colors):
         mat.diffuse_color = c
         mat.keyframe_insert(data_path='diffuse_color', frame=f, index=-1)       
        
                    
    return {'FINISHED'}
    
def invoke(self, context, event):
    
    return context.window_manager.invoke_props_dialog(self)
$\endgroup$

1 Answer 1

1
$\begingroup$

bpy.context.selected_objects returns a list of every selected object ; so you need to iterate through it :

def execute(self, context):
    mat = bpy.data.materials.new(name='colored')

    for obj in bpy.context.selected_objects:
        obj.active_material = mat

        #...the rest of your code

    return {'FINISHED'}
$\endgroup$
1
  • $\begingroup$ Thank you. You're a saving angel. $\endgroup$
    – Blendome
    Commented Oct 10, 2022 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .