Tuesday, October 2, 2018

lock channels and move to layer blender scripting intro

There may be bugs in these but thought these short scripting examples might be helpful to new comers to blender scripting.

#simple script to lock rotations of selected objects
import bpy

#get selected objects
sel = bpy.context.selected_objects

#lock rotations
for obj in sel:
    for idx in [0,1,2]:
        obj.lock_rotation[idx] = True

#simple script to lock translations of selected objects
import bpy

#get selected objects
sel = bpy.context.selected_objects

#lock translations
for obj in sel:
    for idx in [0,1,2]:
        obj.lock_location[idx] = True

#simple script to lock scales
import bpy

#get selected objects
sel = bpy.context.selected_objects

#lock scale
for obj in sel:
    for idx in [0,1,2]:
        obj.lock_scale[idx] = True


####simple script to move all selected to given layer
import bpy

def moveSelectedToLayer(layerIndex = 1):
    """
    layerIndex 0-20
    """
    #try to accept only valid indexes
    if layerIndex not in range(20):
        print('requires layer in 0-20')
        return
        
    #get selected objects
    sel = bpy.context.selected_objects

    #move selected to given layer
    for obj in sel:
        #add object to given layer
        obj.layers[layerIndex] = True
        
        #need to remove it from all other layers
        for idx in [x for x in range(20) if x != layerIndex]:
            obj.layers[idx] = False
                
                
moveSelectedToLayer(1)     
####

Happy Sketching,
Nate

inspired by:
https://blender.stackexchange.com/questions/5781/how-to-list-all-selected-elements-in-python
https://blender.stackexchange.com/questions/34319/how-to-add-object-to-a-specific-layer-with-python