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…
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.
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")
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
Post the script, we’re not psychic
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’}