Button operator, active objects and message question

Hi all,

Getting my feet wet with some scripting in Blender.
Thanks to the template examples, I have a working menu with a bunch of row buttons. So far so good.

My first (working) button is a simple material link, made out of the following operator script bit:

class MatLink(bpy.types.Operator):
    """Link Material to Selections"""
    bl_idname = "myops.mat_link"
    bl_label = "Link Material to Selections"

    @classmethod
    def poll (cls, context):
        return bpy.context.active_object is not None

    def execute(self, context):
        bpy.ops.object.make_links_data(type='MATERIAL')
        print("Material linked to Selected Objects")
        return {'FINISHED'}

What I would like is to have a check to see if there are two or more objects active, and have a message if there’s not.
So how can I set bpy.context.active_object to look at a selection of two or higher?
How can I add a message into the above?

Ah… And the print function doesn’t seem to give me a message in the log…

Cheers for any tips!

rob

Hi,
Correct me if I’m wrong, there only one active object but can have multiple selected objects.
bpy.context.selected_objects should be what you looking for.

Prolly another way to do it ,but it works.

sel_objs = bpy.context.selected_objects
act_obj = bpy.context.active_object

#Check for active object and selected objects is at least 2
if act_obj and len(sel_objs) >= 2:
	bpy.ops.object.make_links_data(type='MATERIAL')
else:
#If only a active object selected
	self.report({'ERROR'}, "Need at least two objects selected")

Thanks!

But how exactly would I change this into my current script?
Not entirely sure what goes where now regarding the first two lines… :wink:

‘execute’ is what happens when the button is pressed.
You’ll want to put the logic to check selection there, at the time of the button press.

I get a runtime error on top of the 'Need at least…" warning:

RuntimeError: class MYOPS_OT_mat_link, function execute: incompatible return value , , Function.result expected a set, not a NoneType

location: <unknown location>:-1


Not sure what goes wrong.

Post the script, we’re not psychic :wink:
If you post the thing in it’s entirety someone (like myself) can just copy paste it and try it out for ourselves and with such little effort that someone will probably do it. At least though, you have to post the part you suspect of error.

Anyway, it’s probably that you’re not putting return {‘FINISHED’}

Ugh… That was it.
Looked at the code multiple times, and still didn’t see it was missing after the code replace.

Thanks! :slight_smile: