Restrict Selection Script help?

Hi, Scripters!

My better half is trying to write a little script for me that will use a shortcut to toggle selectability on the objects in a scene in the 3D View - not just the Outliner. (we are aware of the [s] shortcut/ (bpy.ops.outliner.selectability_toggle()) thing that works within the Outliner, but its very fiddly for day-to-day use, and i’d prefer a Lock shortcut that works, like the Hide one, in the 3D View itself. Or, ideally, in any view!)

However he don’t seem to be able to do this because the outliner operators are only available when the mouse is over the outliner panel.
Is it possible to side-step this? He wants to know if there’s a way he can loop over the elements in the outliner panel in the same way that he can loop over the objects in the scene?

Any help 'ppreciated! :yes:

That’s correct, (at present) certain operations require your mouse to be in specific areas. No-one who uses Outliner a lot will disagree with the need for major boost in usability of that area. Ideally (and i think i saw some movement about this by developers recently) this will be done to the Blender core, not a script.

Blender Python API is so vast that elaborate documentation is going to take a while, some functionality is exposed specifically with certain functions when certain contexts (mouse in area) are true. But generally the same functionality will be available as a lower level, context independant of view, function.


# run from text editor, with 3dview in object mode and your object selected.
mb = bpy.context.active_object
mb.hide_select = True

Other available options are listed by doing a dir(mb), which is equivalent to writing dir(bpy.context.active_object). You might want to perform such (or comparable) operations on a bunch of selected objects, which might look a little something like this:


# with numerous objects selected, if you want to prevent their selection 'en masse'
for i in bpy.context.selected_objects:
    i.hide_select = True

you might be interested in .hide and .hide_render

Thanks Zeffii, that was super-helpful.

Re the Outliner, I understand someone’s working on drag and drop functionality to improve it, which is a start. But the thing I find most frustrating about it is, for something that’s supposed to help you find things in the scene, its actually incredibly awkward when it comes to identifying which bone you’ve selected within an armature. Why it doesn’t expand automatically to show the bone when you press [.] and why - more fundamentally - the selected bone is not even clearly highlighted is a mystery to me. Does it relate to the bone being treated as a sub-object maybe…?

Anyway, as a result i currently only (reluctantly) use the Outliner for matters of locking and visibility.

Thanks again for your reply!

If you provide me with a small example file (you can upload .blend here) that will increase my curiosity about the rigging issue. Perhaps i can come up with some band-aid.

I’ve often wanted the outliner to be able to display only ‘ungrouped’ objects. This prints the names of objects that are grouped


# make sure before this, that nothing is selected.

import bpy

scene_groups = bpy.data.groups
for i in bpy.context.selectable_objects:
    for group in bpy.data.groups:
        for item in group.objects:
            if item.name == i.name:
                print("i.name", i.name, " is grouped in ", group.name)

with a little bit of reworking i can select ungrouped objects doing


import bpy

scene_groups = bpy.data.groups

# make sure before this, that nothing is selected.
names_of_grouped_objects = []

for i in bpy.context.selectable_objects:
    for group in bpy.data.groups:
        for item in group.objects:
            if item.name == i.name:
                print("i.name", i.name, " is grouped in ", group.name)
                names_of_grouped_objects.append(i.name)

# ungrouped objects
print("ungrouped objects are these")
for i in bpy.context.selectable_objects:
    if i.name not in names_of_grouped_objects:
        print(">>>",i.name)
        bpy.data.objects[i.name].select = True

but, the API together with the power of Python exposes a few functions that can drastically shorten this kind of code.


# in the console, this will display the names of all currently grouped objects
>>>[group.objects[:] for group in bpy.data.groups]

Hi Again Zeffi,

I think i can explain what my issue is with a couple of screenshots as its quite simple and not specific to a particular blend file.

I use [Numpad .] a lot to centre selected objects or bones in the 3D View. I also often want to use it in the outliner to quickly reveal whereabouts any selected bone is in the armature hierarchy. But using [numpad .] in the outliner does not cause the armature to automatically expand and reveal the individual bone i have selected, as I would expect. It just highlights the armature root itself, which is unhelpful to say the least.
https://picasaweb.google.com/112279158624086619551/Outliner_woes#5626294272261321170
(I have since noticed this applies to any hierarchy and is not specific to bones)

And the separate issue, which is specific to bones (and is just aesthetics) is that once I have manually expanded the hierarchy and scrolled around to find my bone, it is still very hard to identify as the selected one (ie, the highlighting is unclear).
https://picasaweb.google.com/112279158624086619551/Outliner_woes#5626294275613344706
Fiddling about with the outliner colours has not helped here so maybe someone else can :0)

The second thing I could put up with reluctantly if the first thing weren’t such an issue.
Surely if I select a bone in the 3D view, then press the ‘center selected’ shortcut over the Outliner, it makes sense that i want the outliner to reveal that individual bone or component of the hierarchy, not just point me to the root of it.
I hope that explains my issues better.

[sorry about the links - for whatever reason my pictures wouldnt show when i embedded them, even tho i made sure the album was public and i copied and pasted the link via the add image window]

Thanks for the tip-off Zeffii - i duly pasted in the whole damn lot!