I'm looking to to disable selection all objects I have selected. I can do 1 at a time but I can't find how to: foreach in current selection group, disable selection objects.
I edited out the code due to people thinking it was mine lol
You normally do this sort of thing with a for loop; using bpy.context.selected_objects
. That's a list of all of the objects you have selected.
Here's an example:
import bpy
for object in bpy.context.selected_objects:
print(object.name)
object.hide_select = True
This isn't exactly what you asked for. The 3rd line is debugging and prints the names of the objects it's deselecting. Take it out if you don't want the noise.
EDIT: Having the broken code in my answer is confusing people in the comments. I've removed it and left only my working code above.
bpy.context.object
points to the active object, so the loop will make the same object unselectable many times. Also although it's valid in this context maybe a word of warning that using object
in python is not advised since it overrides the base type object
. And making an object unselectable automatically unselects it from the viewport (not from the outliner though) :)
$\endgroup$
bpy.context.object
and on unselect. Editing answer now.
$\endgroup$
Commented
Apr 1, 2022 at 20:16
Please take a look at proper code formatting in python. Formatting and indentation are essential for python code. If you don't follow the rules, your code will not work as intended.
You have to indent the content of a for loop. And there must not be any blank line between the header and the content of a for loop.