Skip to main content
added 167 characters in body
Source Link
Gorgious
  • 31.6k
  • 2
  • 47
  • 104

As of this commit you can use Context.selected_ids (undocumented for now) to get references for all object types selected in the outliner. Notice that the context of selected_ids attribute is restricted to the Outliner so you'd have to implement an Operator.


I'd also suggest use the bl_rna.identifier attribute to test against the actual type of object in order to filter the selection. Run the script, move your mouse over to the ouliner, press F3 and type Simple Outliner Op... and hit Return:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "outliner.simple_operator"
    bl_label = "Simple Outliner Operator"
    
    @classmethod
    def poll(cls, context):
        return context.area.type == 'OUTLINER'

    def execute(self, context):
        objects_in_selection = {}
        for item in context.selected_ids:
            if item.bl_rna.identifier == "Collection":
                objects_in_selection.setdefault("Collections",[]).append(item.name)
            ifelif item.bl_rna.identifier == "Object":
                if item.type == 'MESH':
                    objects_in_selection.setdefault("Meshes",[]).append(item.name)
                ifelif item.type == 'LIGHT':
                    objects_in_selection.setdefault("Lights",[]).append(item.name)
                ifelif item.type == 'CAMERA':
                    objects_in_selection.setdefault("Cameras",[]).append(item.name)
            elif item.bl_rna.identifier == "Material":
                objects_in_selection.setdefault("Materials",[]).append(item.name)
        
        # Print the dict to the console
        print (objects_in_selection)
        return {'FINISHED'}

def register():
    bpy.utils.register_class(SimpleOperator)
def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

Console Output: {'Collections': ['Collection', 'Collection.001'], 'Cameras': ['Camera', 'Camera.001'], 'Meshes': ['Cube', 'Cube.001'], 'Lights': ['Light', 'Light.001']}

As of this commit you can use Context.selected_ids (undocumented for now) to get references for all object types selected in the outliner. Notice that the context of selected_ids attribute is restricted to the Outliner so you'd have to implement an Operator.


I'd also suggest use the bl_rna.identifier attribute to test against the actual type of object in order to filter the selection. Run the script, move your mouse over to the ouliner, press F3 and type Simple Outliner Op... and hit Return:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "outliner.simple_operator"
    bl_label = "Simple Outliner Operator"
    
    @classmethod
    def poll(cls, context):
        return context.area.type == 'OUTLINER'

    def execute(self, context):
        objects_in_selection = {}
        for item in context.selected_ids:
            if item.bl_rna.identifier == "Collection":
                objects_in_selection.setdefault("Collections",[]).append(item.name)
            if item.bl_rna.identifier == "Object":
                if item.type == 'MESH':
                    objects_in_selection.setdefault("Meshes",[]).append(item.name)
                if item.type == 'LIGHT':
                    objects_in_selection.setdefault("Lights",[]).append(item.name)
                if item.type == 'CAMERA':
                    objects_in_selection.setdefault("Cameras",[]).append(item.name)
        # Print the dict to the console
        print (objects_in_selection)
        return {'FINISHED'}

def register():
    bpy.utils.register_class(SimpleOperator)
def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

Console Output: {'Collections': ['Collection', 'Collection.001'], 'Cameras': ['Camera', 'Camera.001'], 'Meshes': ['Cube', 'Cube.001'], 'Lights': ['Light', 'Light.001']}

As of this commit you can use Context.selected_ids (undocumented for now) to get references for all object types selected in the outliner. Notice that the context of selected_ids attribute is restricted to the Outliner so you'd have to implement an Operator.


I'd also suggest use the bl_rna.identifier attribute to test against the actual type of object in order to filter the selection. Run the script, move your mouse over to the ouliner, press F3 and type Simple Outliner Op... and hit Return:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "outliner.simple_operator"
    bl_label = "Simple Outliner Operator"
    
    @classmethod
    def poll(cls, context):
        return context.area.type == 'OUTLINER'

    def execute(self, context):
        objects_in_selection = {}
        for item in context.selected_ids:
            if item.bl_rna.identifier == "Collection":
                objects_in_selection.setdefault("Collections",[]).append(item.name)
            elif item.bl_rna.identifier == "Object":
                if item.type == 'MESH':
                    objects_in_selection.setdefault("Meshes",[]).append(item.name)
                elif item.type == 'LIGHT':
                    objects_in_selection.setdefault("Lights",[]).append(item.name)
                elif item.type == 'CAMERA':
                    objects_in_selection.setdefault("Cameras",[]).append(item.name)
            elif item.bl_rna.identifier == "Material":
                objects_in_selection.setdefault("Materials",[]).append(item.name)
        
        # Print the dict to the console
        print (objects_in_selection)
        return {'FINISHED'}

def register():
    bpy.utils.register_class(SimpleOperator)
def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

Console Output: {'Collections': ['Collection', 'Collection.001'], 'Cameras': ['Camera', 'Camera.001'], 'Meshes': ['Cube', 'Cube.001'], 'Lights': ['Light', 'Light.001']}

poll
Source Link
brockmann
  • 12.7k
  • 4
  • 50
  • 93

As of this commit you can use Context.selected_ids (undocumented for now) to get references for all object types selected in the outliner. Notice that the context of selected_ids attribute is restricted to the Outliner so you'd have to implement an Operator.


I'd also suggest use the bl_rna.identifier attribute to test against the actual type of object in order to filter the selection. Run the script, move your mouse over to the ouliner, press F3 and type Simple Outliner Op... and hit Return:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "outliner.simple_operator"
    bl_label = "Simple Outliner Operator"
    
    @classmethod
    def poll(cls, context):
        return context.area.type == 'OUTLINER'

    def execute(self, context):
 
        objects_in_selection = {}
        for item in context.selected_ids:
            if item.bl_rna.identifier == "Collection":
                objects_in_selection.setdefault("Collections",[]).append(item.name)
            
            if item.bl_rna.identifier == "Object":
                if item.type == 'MESH':
                    objects_in_selection.setdefault("Meshes",[]).append(item.name)
                if item.type == 'LIGHT':
                    objects_in_selection.setdefault("Lights",[]).append(item.name)
                if item.type == 'CAMERA':
                    objects_in_selection.setdefault("Cameras",[]).append(item.name)
        # Print the dict to the console
        print (objects_in_selection)
        return {'FINISHED'}
 

def register():
    bpy.utils.register_class(SimpleOperator)
 
def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

Console Output: {'Collections': ['Collection', 'Collection.001'], 'Cameras': ['Camera', 'Camera.001'], 'Meshes': ['Cube', 'Cube.001'], 'Lights': ['Light', 'Light.001']}

As of this commit you can use Context.selected_ids (undocumented for now) to get references for all object types selected in the outliner. Notice that the context of selected_ids attribute is restricted to the Outliner so you'd have to implement an Operator.


I'd also suggest use the bl_rna.identifier attribute to test against the actual type of object in order to filter the selection. Run the script, move your mouse over to the ouliner, press F3 and type Simple Outliner Op... and hit Return:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "outliner.simple_operator"
    bl_label = "Simple Outliner Operator"

    def execute(self, context):
 
        objects_in_selection = {}
        for item in context.selected_ids:
            if item.bl_rna.identifier == "Collection":
                objects_in_selection.setdefault("Collections",[]).append(item.name)
            
            if item.bl_rna.identifier == "Object":
                if item.type == 'MESH':
                    objects_in_selection.setdefault("Meshes",[]).append(item.name)
                if item.type == 'LIGHT':
                    objects_in_selection.setdefault("Lights",[]).append(item.name)
                if item.type == 'CAMERA':
                    objects_in_selection.setdefault("Cameras",[]).append(item.name)
        
        print (objects_in_selection)
        return {'FINISHED'}
 

def register():
    bpy.utils.register_class(SimpleOperator)
 
def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

Console Output: {'Collections': ['Collection', 'Collection.001'], 'Cameras': ['Camera', 'Camera.001'], 'Meshes': ['Cube', 'Cube.001'], 'Lights': ['Light', 'Light.001']}

As of this commit you can use Context.selected_ids (undocumented for now) to get references for all object types selected in the outliner. Notice that the context of selected_ids attribute is restricted to the Outliner so you'd have to implement an Operator.


I'd also suggest use the bl_rna.identifier attribute to test against the actual type of object in order to filter the selection. Run the script, move your mouse over to the ouliner, press F3 and type Simple Outliner Op... and hit Return:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "outliner.simple_operator"
    bl_label = "Simple Outliner Operator"
    
    @classmethod
    def poll(cls, context):
        return context.area.type == 'OUTLINER'

    def execute(self, context):
        objects_in_selection = {}
        for item in context.selected_ids:
            if item.bl_rna.identifier == "Collection":
                objects_in_selection.setdefault("Collections",[]).append(item.name)
            if item.bl_rna.identifier == "Object":
                if item.type == 'MESH':
                    objects_in_selection.setdefault("Meshes",[]).append(item.name)
                if item.type == 'LIGHT':
                    objects_in_selection.setdefault("Lights",[]).append(item.name)
                if item.type == 'CAMERA':
                    objects_in_selection.setdefault("Cameras",[]).append(item.name)
        # Print the dict to the console
        print (objects_in_selection)
        return {'FINISHED'}

def register():
    bpy.utils.register_class(SimpleOperator)
def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

Console Output: {'Collections': ['Collection', 'Collection.001'], 'Cameras': ['Camera', 'Camera.001'], 'Meshes': ['Cube', 'Cube.001'], 'Lights': ['Light', 'Light.001']}

Source Link
brockmann
  • 12.7k
  • 4
  • 50
  • 93

As of this commit you can use Context.selected_ids (undocumented for now) to get references for all object types selected in the outliner. Notice that the context of selected_ids attribute is restricted to the Outliner so you'd have to implement an Operator.


I'd also suggest use the bl_rna.identifier attribute to test against the actual type of object in order to filter the selection. Run the script, move your mouse over to the ouliner, press F3 and type Simple Outliner Op... and hit Return:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "outliner.simple_operator"
    bl_label = "Simple Outliner Operator"

    def execute(self, context):

        objects_in_selection = {}
        for item in context.selected_ids:
            if item.bl_rna.identifier == "Collection":
                objects_in_selection.setdefault("Collections",[]).append(item.name)
            
            if item.bl_rna.identifier == "Object":
                if item.type == 'MESH':
                    objects_in_selection.setdefault("Meshes",[]).append(item.name)
                if item.type == 'LIGHT':
                    objects_in_selection.setdefault("Lights",[]).append(item.name)
                if item.type == 'CAMERA':
                    objects_in_selection.setdefault("Cameras",[]).append(item.name)
        
        print (objects_in_selection)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)

def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

Console Output: {'Collections': ['Collection', 'Collection.001'], 'Cameras': ['Camera', 'Camera.001'], 'Meshes': ['Cube', 'Cube.001'], 'Lights': ['Light', 'Light.001']}