Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 15, 2011

Blender 2.5 Python Selecting a Layer

read the whole post first before trying any of the code, the pay-off is towards the end.
def selectLayer(layernum):
    bools = []
    for i in range(20):
        if layernum == i:
            bools.append(True)
        else:
            bools.append(False)
    return tuple(bools) #does a typecast from list to tuple

# because selectLayer returns a tuple
print(selectLayer(4))

# you can stick that into the ridiculously long line like
bpy.ops.curve.primitive_nurbs_path_add( view_align=False, 
enter_editmode=False, location=(0, 0, 0), 
rotation=(0, 0, 0), 
layers=(True, False, False, False, False, False, False, False, False, False, 
False, False, False, False, False, False, False, False, False, False))

# write instead, if you really need to select a layer
bpy.ops.curve.primitive_nurbs_path_add(layers=selectLayer(0))

Everything dropped from the first primitive_nurbs_path_add() is default/optional anyway. The benefit of making a function for selectLayer is that you can reuse it in other places and you have abstracted away the layers that are of no interest.

layer selection can be even shorter tho, if you only ever want the first layer.
bpy.ops.curve.primitive_nurbs_path_add(layers=((True,)+(False,)*19)))

but wait! there is an even more elegant way using List Comprehension! Instead of earlier defined selectLayer function, this one works the same!
def selectLayer(layer_nr):
    return tuple(i == layer_nr for i in range(0, 20))
print(selectLayer(2))

# the output would look like
(False, False, True, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False)