3
$\begingroup$

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

$\endgroup$

2 Answers 2

5
$\begingroup$

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.

$\endgroup$
13
  • $\begingroup$ also notice that the SEL_OBJS line should be separated by a return from the ACT_OBJ line because they are two separate statements. $\endgroup$ Commented Apr 1, 2022 at 19:24
  • $\begingroup$ Cheers, I wasn't trying to write code, I was just giving an idea what I wanted by looking online for examples. $\endgroup$ Commented Apr 1, 2022 at 19:33
  • $\begingroup$ @MarcVolkers My first example above will do what you wanted. So would the code you posted, formatted the way I have it now. $\endgroup$ Commented Apr 1, 2022 at 20:08
  • 1
    $\begingroup$ fwiw I don't think that even indented properly the original code would work, 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$
    – Gorgious
    Commented Apr 1, 2022 at 20:12
  • $\begingroup$ @Gorgious you're absolutely right on bpy.context.object and on unselect. Editing answer now. $\endgroup$ Commented Apr 1, 2022 at 20:16
2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Cheers, I wish I never wrote anything :S I deleted it, I'm looking for some one to write something or point me in the right direction. $\endgroup$ Commented Apr 1, 2022 at 20:05

You must log in to answer this question.

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