RNA: Object.select_set use boolean, only select
- Was setting active state, making it necessary to backup/restore active object in cases where this isn't needed. Existing scripts are explicitly setting the active object when needed. - Use a boolean select arg (toggle selection wasn't used anywhere). - Add an optional view layer argument since scripts should be able to operate outside the user context.
This commit is contained in:
		@@ -25,5 +25,5 @@ view_layer.active_layer_collection.collection.objects.link(light_object)
 | 
			
		||||
light_object.location = (5.0, 5.0, 5.0)
 | 
			
		||||
 | 
			
		||||
# And finally select it and make it active.
 | 
			
		||||
light_object.select_set('SELECT')
 | 
			
		||||
light_object.select_set(True)
 | 
			
		||||
view_layer.objects.active = light_object
 | 
			
		||||
 
 | 
			
		||||
@@ -313,7 +313,7 @@ class CLIP_OT_bundles_to_mesh(Operator):
 | 
			
		||||
            ob = bpy.data.objects.new(name="Tracks", object_data=mesh)
 | 
			
		||||
            ob.matrix_world = matrix
 | 
			
		||||
            context.collection.objects.link(ob)
 | 
			
		||||
            ob.select_set('SELECT')
 | 
			
		||||
            ob.select_set(True)
 | 
			
		||||
            context.view_layer.objects.active = ob
 | 
			
		||||
        else:
 | 
			
		||||
            self.report({'WARNING'}, "No usable tracks selected")
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,7 @@ bpy.ops.object.select_all(action='DESELECT')
 | 
			
		||||
 | 
			
		||||
for obj in selection:
 | 
			
		||||
 | 
			
		||||
    obj.select_set(action='SELECT')
 | 
			
		||||
    obj.select_set(True)
 | 
			
		||||
 | 
			
		||||
    # some exporters only use the active object
 | 
			
		||||
    view_layer.objects.active = obj
 | 
			
		||||
@@ -31,7 +31,7 @@ for obj in selection:
 | 
			
		||||
    # Can be used for multiple formats
 | 
			
		||||
    # bpy.ops.export_scene.x3d(filepath=fn + ".x3d", use_selection=True)
 | 
			
		||||
 | 
			
		||||
    obj.select_set(action='DESELECT')
 | 
			
		||||
    obj.select_set(False)
 | 
			
		||||
 | 
			
		||||
    print("written:", fn)
 | 
			
		||||
 | 
			
		||||
@@ -39,4 +39,4 @@ for obj in selection:
 | 
			
		||||
view_layer.objects.active = obj_active
 | 
			
		||||
 | 
			
		||||
for obj in selection:
 | 
			
		||||
    obj.select_set(action='SELECT')
 | 
			
		||||
    obj.select_set(True)
 | 
			
		||||
 
 | 
			
		||||
@@ -67,7 +67,7 @@ def main(context, event):
 | 
			
		||||
    # now we have the object under the mouse cursor,
 | 
			
		||||
    # we could do lots of stuff but for the example just select.
 | 
			
		||||
    if best_obj is not None:
 | 
			
		||||
        best_obj.select_set(action='SELECT')
 | 
			
		||||
        best_obj.select_set(True)
 | 
			
		||||
        context.view_layer.objects.active = best_obj
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -92,9 +92,13 @@ static const EnumPropertyItem space_items[] = {
 | 
			
		||||
 | 
			
		||||
#include "MEM_guardedalloc.h"
 | 
			
		||||
 | 
			
		||||
static void rna_Object_select_set(Object *ob, bContext *C, ReportList *reports, int action)
 | 
			
		||||
static void rna_Object_select_set(
 | 
			
		||||
        Object *ob, bContext *C, ReportList *reports,
 | 
			
		||||
        bool select, ViewLayer *view_layer)
 | 
			
		||||
{
 | 
			
		||||
	ViewLayer *view_layer = CTX_data_view_layer(C);
 | 
			
		||||
	if (view_layer == NULL) {
 | 
			
		||||
		view_layer = CTX_data_view_layer(C);
 | 
			
		||||
	}
 | 
			
		||||
	Base *base = BKE_view_layer_base_find(view_layer, ob);
 | 
			
		||||
 | 
			
		||||
	if (!base) {
 | 
			
		||||
@@ -102,29 +106,16 @@ static void rna_Object_select_set(Object *ob, bContext *C, ReportList *reports,
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (action == 2) { /* TOGGLE */
 | 
			
		||||
		if ((base->flag & BASE_SELECTED) != 0) {
 | 
			
		||||
			action = 1; /* DESELECT */
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			action = 0; /* SELECT */
 | 
			
		||||
		}
 | 
			
		||||
	if (select) {
 | 
			
		||||
		BKE_view_layer_base_select(base);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	switch (action) {
 | 
			
		||||
		case 1: /* DESELECT */
 | 
			
		||||
			base->flag &= ~BASE_SELECTED;
 | 
			
		||||
			break;
 | 
			
		||||
		case 0: /* SELECT */
 | 
			
		||||
		default:
 | 
			
		||||
			BKE_view_layer_base_select_and_set_active(view_layer, base);
 | 
			
		||||
			break;
 | 
			
		||||
	else {
 | 
			
		||||
		base->flag &= ~BASE_SELECTED;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Scene *scene = CTX_data_scene(C);
 | 
			
		||||
	DEG_id_tag_update(&scene->id, DEG_TAG_SELECT_UPDATE);
 | 
			
		||||
	WM_main_add_notifier(NC_SCENE | ND_OB_SELECT, scene);
 | 
			
		||||
	WM_main_add_notifier(NC_SCENE | ND_OB_ACTIVE, scene);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool rna_Object_select_get(Object *ob, bContext *C, ReportList *reports)
 | 
			
		||||
@@ -497,19 +488,13 @@ void RNA_api_object(StructRNA *srna)
 | 
			
		||||
	};
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
	static EnumPropertyItem object_select_items[] = {
 | 
			
		||||
	    {0, "SELECT", 0, "Select", "Select object from the active view layer"},
 | 
			
		||||
	    {1, "DESELECT", 0, "Deselect", "Deselect object from the active view layer"},
 | 
			
		||||
	    {2, "TOGGLE", 0, "Toggle", "Toggle object selection from the active view layer"},
 | 
			
		||||
	    {0, NULL, 0, NULL, NULL}
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	/* Special wrapper to access the base selection value */
 | 
			
		||||
	func = RNA_def_function(srna, "select_set", "rna_Object_select_set");
 | 
			
		||||
	RNA_def_function_ui_description(func, "Select the object (for the active view layer)");
 | 
			
		||||
	RNA_def_function_flag(func, FUNC_USE_CONTEXT | FUNC_USE_REPORTS);
 | 
			
		||||
	parm = RNA_def_enum(func, "action", object_select_items, 0, "Action", "Select mode");
 | 
			
		||||
	parm = RNA_def_boolean(func, "state", 0, "", "");
 | 
			
		||||
	RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
 | 
			
		||||
	parm = RNA_def_pointer(func, "view_layer", "ViewLayer", "", "Operate on this view layer instead of the context");
 | 
			
		||||
 | 
			
		||||
	func = RNA_def_function(srna, "select_get", "rna_Object_select_get");
 | 
			
		||||
	RNA_def_function_ui_description(func, "Get the object selection for the active view layer");
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,7 @@ class UnitTesting(ViewLayerTesting):
 | 
			
		||||
 | 
			
		||||
        layer_collection_mom.enabled = False
 | 
			
		||||
        bpy.context.scene.update()  # update depsgraph
 | 
			
		||||
        cube.select_set('SELECT')
 | 
			
		||||
        cube.select_set(True)
 | 
			
		||||
 | 
			
		||||
        self.assertTrue(cube.visible_get(), "Cube should be visible")
 | 
			
		||||
        self.assertTrue(cube.select_get(), "Cube should be selected")
 | 
			
		||||
 
 | 
			
		||||
@@ -36,7 +36,7 @@ class UnitTesting(ViewLayerTesting):
 | 
			
		||||
        layer_collection_mom = layer.collections.link(scene_collection_mom)
 | 
			
		||||
        layer_collection_kid = layer.collections.link(scene_collection_kid)
 | 
			
		||||
        bpy.context.scene.update()  # update depsgraph
 | 
			
		||||
        cube.select_set('SELECT')
 | 
			
		||||
        cube.select_set(True)
 | 
			
		||||
 | 
			
		||||
        layer_collection_mom.collections[layer_collection_kid.name].enabled = False
 | 
			
		||||
        layer_collection_kid.enabled = False
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,7 @@ class UnitTesting(ViewLayerTesting):
 | 
			
		||||
 | 
			
		||||
        layer_collection_mom.enabled = True
 | 
			
		||||
        bpy.context.scene.update()  # update depsgraph
 | 
			
		||||
        cube.select_set('SELECT')
 | 
			
		||||
        cube.select_set(True)
 | 
			
		||||
 | 
			
		||||
        self.assertTrue(cube.visible_get(), "Cube should be visible")
 | 
			
		||||
        self.assertTrue(cube.select_get(), "Cube should be selected")
 | 
			
		||||
 
 | 
			
		||||
@@ -39,7 +39,7 @@ class UnitTesting(ViewLayerTesting):
 | 
			
		||||
        layer_collection_mom.enabled = True
 | 
			
		||||
        bpy.context.scene.update()  # update depsgraph
 | 
			
		||||
 | 
			
		||||
        cube.select_set('SELECT')
 | 
			
		||||
        cube.select_set(True)
 | 
			
		||||
        layer_collection_mom.collections[layer_collection_kid.name].selectable = False
 | 
			
		||||
 | 
			
		||||
        bpy.context.scene.update()  # update depsgraph
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,7 @@ class UnitTesting(ViewLayerTesting):
 | 
			
		||||
        layer_collection_kid = layer.collections.link(scene_collection_kid)
 | 
			
		||||
 | 
			
		||||
        layer_collection_mom.enabled = True
 | 
			
		||||
        cube.select_set('SELECT')
 | 
			
		||||
        cube.select_set(True)
 | 
			
		||||
        layer_collection_mom.collections[layer_collection_kid.name].selectable = False
 | 
			
		||||
        layer_collection_kid.enabled = False
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -51,9 +51,9 @@ class UnitTesting(ViewLayerTesting):
 | 
			
		||||
        # we could just pass an overridden context
 | 
			
		||||
        # but let's do it the old fashion way
 | 
			
		||||
        view_layer.objects.active = ob
 | 
			
		||||
        ob.select_set('SELECT')
 | 
			
		||||
        ob.select_set(True)
 | 
			
		||||
        self.assertTrue(ob.select_get())
 | 
			
		||||
        empty.select_set('DESELECT')
 | 
			
		||||
        empty.select_set(False)
 | 
			
		||||
        self.assertFalse(empty.select_get())
 | 
			
		||||
 | 
			
		||||
        # update depsgraph
 | 
			
		||||
 
 | 
			
		||||
@@ -36,7 +36,7 @@ class UnitTesting(ViewLayerTesting):
 | 
			
		||||
                master_collection.collections[0])
 | 
			
		||||
 | 
			
		||||
        view_layer.collections.link(master_collection)
 | 
			
		||||
        ob.select_set('SELECT')
 | 
			
		||||
        ob.select_set(True)
 | 
			
		||||
 | 
			
		||||
        # update depsgraph
 | 
			
		||||
        scene.update()
 | 
			
		||||
 
 | 
			
		||||
@@ -26,7 +26,7 @@ class UnitTesting(ViewLayerTesting):
 | 
			
		||||
        layer = bpy.context.view_layer
 | 
			
		||||
 | 
			
		||||
        original_cube = layer.objects.get('Cube')
 | 
			
		||||
        original_cube.select_set('SELECT')
 | 
			
		||||
        original_cube.select_set(True)
 | 
			
		||||
        self.assertTrue(original_cube.select_get())
 | 
			
		||||
 | 
			
		||||
        bpy.ops.scene.new(type='FULL_COPY')
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user