1
$\begingroup$

Recently, I asked a question about how to select a parent directly from the viewport by right-clicking.

However, this time, I would like to know if it is achievable to select a collection using the right-click menu when I right-click on an object part of the collection, or, if it is possible, just directly right-click anywhere in the 3D Viewport and have an option to select a collection.

In a broader perspective, when using Shift+G, you can select only a collection in a collection. For example, I have some objects in a collection and a few collections in a collection. Is there an option to choose which one (the big one or the sub-collections) I can select from the Right-Click menu in the 3D Viewport or on an object part of the collection?

$\endgroup$

1 Answer 1

0
$\begingroup$

Select all collection objects (including subcollections) from an object

enter image description here

bl_info = {
    "name": "My Addon",
    "author": "X Y",
    "version": (0, 1),
    "blender": (2, 80, 0),
    "location": "View3D",
    "description": "2 Operators for selecting collection objects",
    "category": "Object",
}

import bpy

class MY_OP(bpy.types.Operator):
    bl_idname = "view3d.collection_objects"
    bl_label = "Select Collection objects"
    bl_option = {
        'REGISTER', # Register – Display in the info window and support the redo toolbar panel.
        'UNDO',     # Undo – Push an undo event (needed for operator redo).
    }
    bl_description = "Select all subcollections of collections and objects"

    def execute(self, context):
        try:
            # access active object, so you need to click on a object first
            obj = bpy.context.object
            # get collection of obj
            coll = obj.users_collection[0]
            # Objects that are in this collection and its child collections
            objs = coll.all_objects
            # select them
            for ob in objs:
                ob.select_set(True)
            return {'FINISHED'}
        except:
            self.report({'WARNING'}, "Something went wrong, operator could not be executed.")
            return {'CANCELLED'}

class MY_OP_2(bpy.types.Operator):
    bl_idname = "view3d.subcollection_objects"
    bl_label = "Select Subcollection objects"
    bl_option = {
        'REGISTER', # Register – Display in the info window and support the redo toolbar panel.
        'UNDO',     # Undo – Push an undo event (needed for operator redo).
    }
    bl_description = "Select all objects of subcollection(s)"

    def execute(self, context):
        try:
            # access active object, so you need to click on a object first
            obj = bpy.context.object
            # get collection of obj
            coll = obj.users_collection[0]
            # Collections that are immediate children of this collection
            children = coll.children
            for c in children:
                for ob in c.all_objects:
                    ob.select_set(True)

            return {'FINISHED'}
        except:
            self.report({'WARNING'}, "Something went wrong, operator could not be executed.")
            return {'CANCELLED'}


def menu_func(self, context):
    self.layout.operator(MY_OP.bl_idname)

def menu_func2(self, context):
    self.layout.operator(MY_OP_2.bl_idname)

classes = {MY_OP, MY_OP_2}
functions = [menu_func, menu_func2]

def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    for fn in functions:
        bpy.types.VIEW3D_MT_object_context_menu.append(fn)

def unregister():
    for cls in {MY_OP, MY_OP_2}:
        bpy.utils.unregister_class(cls)
    for fn in functions:
        bpy.types.VIEW3D_MT_object_context_menu.remove(fn)

if __name__ == "__main__":
    register()
$\endgroup$
4
  • $\begingroup$ Hello, thanks for the answer! However, is it possible for you to tell me what part of the script to separately add or subtract to give me an option to add an addon for sub-collection only also? Again, thanks! $\endgroup$ Jun 14, 2022 at 21:14
  • $\begingroup$ Actually, it's not that necessary so I accepted your answer. Thanks for the help !) $\endgroup$ Jun 14, 2022 at 21:47
  • $\begingroup$ I have updated, you can take a look. $\endgroup$
    – X Y
    Jun 14, 2022 at 21:58
  • $\begingroup$ Thanks a lot (! $\endgroup$ Jun 14, 2022 at 22:27

You must log in to answer this question.

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