.. _snippets: ============================== Blender: often used commands ============================== .. contents:: **Table of contents** .. index:: Blender, commands, snippets, code snippets Template: section blender basics ================================ :: #!bpy """ Name: 'Template' Blender: 2.69 Group: 'Sample' Tooltip: 'Template for new scripts, copy it and start coding...' """ import bpy def function_1(): """ One line describing the task of this function """ pass def function_2(): """ One line describing the task of this function """ print(__name__) print(50 * '*') def function_3(): """ One line describing the task of this function """ bpy.ops.mesh.primitive_cone_add(location=(1, 2, 1)) if __name__ == '__main__': # call the function for testing function_1() #function_2() #function_3() Template: simple sample ======================= :: #!bpy """ Name: '???' Blender: 2.69 Group: 'Experiment' Tooltip: 'Try...' """ import bpy def test(): """ One line describing the task of this function """ pass if __name__ == '__main__': # Stop edit mode if bpy.ops.object.mode_set.poll(): bpy.ops.object.mode_set(mode='OBJECT') # delete all mesh objects from a scene bpy.ops.object.select_by_type(type='MESH') bpy.ops.object.delete() # call the new function test() Find Objects by name ==================== All objecte as list of names ... Version I :: all = [item.name for item in bpy.data.objects] for name in all: print(name) Version II :: # collect all names obj_names = [] for obj in bpy.context.scene.objects: obj_names.append(obj.name) Selection by type ... :: bpy.ops.object.select_by_type(type='MESH') bpy.ops.object.select_by_type(type='CURVE') Selection by pattern... :: bpy.ops.object.select_pattern(pattern="Mein Auto") Deaktivate all objects ====================== :: for obj in bpy.context.selectable_objects: obj.select = False Switch between edit- and object mode ==================================== Works only if a object is selected. :: bpy.ops.object.editmode_toggle() :: bpy.ops.object.mode_set(mode = 'OBJECT') bpy.ops.object.mode_set(mode = 'EDIT') Delete objects ============== Works only, if objects are selected ... :: bpy.ops.object.select_by_type(type='MESH') bpy.ops.object.delete() Activate object =============== :: bpy.context.scene.objects.active = bpy.data.objects["Cube"] .. todo:: check all variants, to set a path ... .. index:: getIndexOfFaces Detect the index of a face =========================== :: def getIndexOfFaces(name): """ Print the index of faces to the console - Switch to the edit-Mode - select faces - the output ist shown in the console """ bpy.context.scene.objects.active = bpy.data.objects[name] bpy.ops.object.mode_set(mode='EDIT') bpy.ops.object.mode_set(mode='OBJECT') me = bpy.context.object.data for poly in me.polygons: if poly.select: print("Polygon index: %d, length: %d" % (poly.index, poly.loop_total)) bpy.ops.object.mode_set(mode='EDIT')