HOME | DD

#blender #script #tutorial
Published: 2017-12-27 19:32:13 +0000 UTC; Views: 554; Favourites: 8; Downloads: 13
Redirect to original
Description
See the bottom for an example script for locking or unlocking all vertex groups: something simple, but that Blender doesn't already do by default (well not that I know of), and something that can be very handy when you've got 400 vertex groups....First step of learning Blender/Python is just learning how to run scripts. You can start out by running scripts that other people have made; as you do it more, if you try to figure out what the script is doing, you ought to find yourself learning more about how to edit these scripts to do what you want, and eventually should be able to do them from scratch.
When you know exactly what you want to do, but don't want to do the commands (and maybe the math) a few thousand times, that's when you want to write a script. In the past, I've made decisions not to do things because they were too tedious; my plan now is to force myself, every time I encounter something tedious, to try to learn how to script it.
I've programmed in C-like languages and in Lua. Python uses data structures similar to Lua's tables. It uses '#' to indicate a comment. Indentation alone creates logical blocks, rather than using any brackets. New lines are used to separate instructions, rather than semi-colons.
You can try to get commands by looking at the console when you do something, by checking out the templates in the text editor, by mousing over an operator or field (apparently ctrl-c on mouseover gives you the Python handle.) You can set bpy.app.debug_wm = True in the Python console to get more verbose messages, so you can get a better handle on things. I don't yet have a good handle on things myself; this is what I've been able to do so far, and I'm creating this to have something to link to for when I write more scripts, to solidify my understanding so far, and to provide a start to understanding Blender's scripting in the way that I would have liked to have been introduced to it.
Copy/paste script. This script can be used to lock or unlock all vertex groups on an object:
print("start")
import bpy
lock = True
#lock = False
#selected object only
obj = bpy.context.object
def lockAll(obj, lock):
for group in obj.vertex_groups:
if lock:
group.lock_weight = True
else:
group.lock_weight = False
lockAll(obj, lock)
print("done")