Script to lock/unlock the selected objects

Hello,

I needed to lock many objects, and since I was too lazy to do it by hand, I wrote the following script. It locks/unlocks the location, rotation and/or scale of the selected objects (just as clicking on the lock symbols in the transform properties panel would do).


#!BPY

""" Registration info for Blender menus:
Name: 'Lock/unlock Objects'
Blender: 249
Group: 'Object'
Tooltip: 'Locks/unlocks the location, rotation and/or scale of the selected objects.'
"""

import Blender

lockLoc = Blender.Draw.Create(1)
lockRot = Blender.Draw.Create(1)
lockScale = Blender.Draw.Create(1)

if Blender.Draw.PupBlock("Lock or unlock objects", [
	("Lock location", lockLoc, "Lock the location? (otherwise it is unlocked)"),
	("Lock rotation", lockRot, "Lock the rotation? (otherwise it is unlocked)"),
	("Lock scale", lockScale, "Lock the scale? (otherwise it is unlocked)")
]):
	flags = 0
	if lockLoc.val:
		flags |= Blender.Object.ProtectFlags.LOC
	if lockRot.val:
		flags |= Blender.Object.ProtectFlags.ROT
	if lockScale.val:
		flags |= Blender.Object.ProtectFlags.SCALE

	for obj in Blender.Object.GetSelected():
		obj.protectFlags = flags

good idea, thanks to share.
could you please briefly explain the difference between
a +=5
and
a |=5
?
is it about binaries ?
I never saw that before, don’t find it in the py doc…

I too could use some guidance to how this script works.
I could have used this a day or so ago where I had locked a bunch of obj. Because I was having trouble with them randomly resizing themselves. ‘(8) scenes with about 30 objects’ After I found the culprits were linked to each other, I needed to unlock them all to set them straight.
I wanted a script but at this stage I’m not smart enough to figure it out
Please explain a little how it works. I can see some of it but really don’t know where the actual code/wording of the script comes from. ie: PupBlock: and what exactly does “flag” do?
Forgive my 99% noob to this…
Unfortunately I need much bigger scripts and don’t know how to write them!

“a += 5” adds 5 to “a”; “a |=5” performs a bitwise OR between “a” and 5 (binary 101) and stores the result in “a”. C++ has it, so I assumed that Python, since it has the “+=” operator, also has it. And I assumed correctly:yes:

Quite simple. After installing, you call it from the Object menu. It displays a small dialog with three toggle buttons that specify whether the selected objects should be locked (pressed, default) or unlocked (released). After confirming, locking/unlocking is performed.

#!BPY

""" Registration info for Blender menus:
Name: 'Lock/unlock Objects'
Blender: 249
Group: 'Object'
Tooltip: 'Locks/unlocks the location, rotation and/or scale of the selected objects.'
"""

This simply tells Blender what the script is called, which Blender version is required, in what script group it should be listed in as well as the tooltip displayed when hovering over the menu entry. As said, the script is listed in the “Object” menu.

import Blender

This imports the “Blender” module, so its functions can be used as in “Blender.function(parameters)”

lockLoc = Blender.Draw.Create(1)
lockRot = Blender.Draw.Create(1)
lockScale = Blender.Draw.Create(1)

Here three toggle buttons are created. Look up the documentation of “Create” in the “Draw” module in Blender’s Python documentation.

if Blender.Draw.PupBlock("Lock or unlock objects", [
	("Lock location", lockLoc, "Lock the location? (otherwise it is unlocked)"),
	("Lock rotation", lockRot, "Lock the rotation? (otherwise it is unlocked)"),
	("Lock scale", lockScale, "Lock the scale? (otherwise it is unlocked)")
]):

Here the dialog is drawn:Its title is “Lock or unlock objects”, then the dialog elements are specified (again, please look up the “PupBlock” function in the Python documentation). If the function returns a non-zero result, the user has confirmed the dialog.

	flags = 0
	if lockLoc.val:
		flags |= Blender.Object.ProtectFlags.LOC
	if lockRot.val:
		flags |= Blender.Object.ProtectFlags.ROT
	if lockScale.val:
		flags |= Blender.Object.ProtectFlags.SCALE

Blender stores the lock status of location, rotation and scale in a bitfield (each bit specifying whether the given property is locked or not). So now the user selection is transformed into such a bitfield (“flags”) by combining the different bits using bitwise OR. The predefined constants in “Blender.Object.ProtectFlags” are used for convenience and clarity.

	for obj in Blender.Object.GetSelected():
		obj.protectFlags = flags

Finally, the script iterates over all selected objects and sets their “protectFlag” to the new lock status.

And that’s it:)

If you need bigger scripts, you could ask in the forum. Maybe the scripts are considered to be useful to many people, and someone will write them.

Thanks for the fast & detailed explanation.
It’ll take me a while to absorb all this. But little by little I may be getting it. I hope.
I’ll have to get my thoughts in order before asking for a script.
My knowledge in g/e logic bricks & python is so limited, I’m not quite sure what I need yet.
I do know that some of what I tried doesn’t work.

Thanks again

“a += 5” adds 5 to “a”; “a |=5” performs a bitwise OR between “a” and 5 (binary 101) and stores the result in “a”. C++ has it, so I assumed that Python, since it has the “+=” operator, also has it. And I assumed correctly

really interesting and useful, thanks !
I also assumed that the b &=a and b ^=a would work :
so :
|
0 or 1 -> 1
1 or 1 -> 1
0 or 0 -> 0

&
0 and 1 -> 0
1 and 1 -> 1
0 and 0 -> 0

^
0 xor 1 -> 1
1 xor 1 -> 0
0 xor 0 -> 0

is there another operator to obtain :
0 … 0 -> 1
1 … 1 -> 1
0 … 1 -> 0
?
in guess not, but just in case :slight_smile:

That would be “a ^= ~b”. Not a single operator, I’m afraid.

See Unary arithmetic and bitwise operations and Augmented assignment statements.

had a coffee and some useful readings this last couple of hours. I didn’t know the x < y < z one instance thing and the withstatement.
(… though I don’t see in which case one could need to use a while followed by an else as in all cases what’s next while will be executed but anyway, I’ll think about it)
thanks again for this thread.

Quoting from The while statement (emphasis mine):

A break statement executed in the first suite terminates the loop without executing the else clause’s suite.

So you can for example do the following (“for” also allows for “else”):


for obj in Blender.Object.GetSelected():
  if obj.whatever == whatever:
    break;
else:
  error("No valid object with &lt;whatever&gt; found")

When break-ing out of a loop, the else part is not executed. So you can distinguish between leaving the loop via break or because it has terminated normally.

:slight_smile: it’s really more clean than the kind of thing below I used to write :


nothing=True
for obj in Blender.Object.GetSelected():
  if obj.whatever == whatever:
    nothing=False
    break
if nothing :
  error("No valid object with &lt;whatever&gt; found")

more ! more ! (just kidding :slight_smile: thanks for your valuable infos, I stop to pollute this thread now)