Blender V2.61 - r43446

bl_run_operators.py

Go to the documentation of this file.
00001 # ##### BEGIN GPL LICENSE BLOCK #####
00002 #
00003 #  This program is free software; you can redistribute it and/or
00004 #  modify it under the terms of the GNU General Public License
00005 #  as published by the Free Software Foundation; either version 2
00006 #  of the License, or (at your option) any later version.
00007 #
00008 #  This program is distributed in the hope that it will be useful,
00009 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 #  GNU General Public License for more details.
00012 #
00013 #  You should have received a copy of the GNU General Public License
00014 #  along with this program; if not, write to the Free Software Foundation,
00015 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00016 #
00017 # ##### END GPL LICENSE BLOCK #####
00018 
00019 # <pep8 compliant>
00020 
00021 # semi-useful script, runs all operators in a number of different
00022 # contexts, cheap way to find misc small bugs but is in no way a complete test.
00023 #
00024 # only error checked for here is a segfault.
00025 
00026 import bpy
00027 import sys
00028 
00029 op_blacklist = (
00030     "script.reload",
00031     "export*.*",
00032     "import*.*",
00033     "*.save_*",
00034     "*.read_*",
00035     "*.open_*",
00036     "*.link_append",
00037     "render.render",
00038     "*.*_export",
00039     "*.*_import",
00040     "wm.url_open",
00041     "wm.doc_view",
00042     "wm.path_open",
00043     "help.operator_cheat_sheet",
00044     )
00045 
00046 
00047 def filter_op_list(operators):
00048     from fnmatch import fnmatchcase
00049 
00050     def is_op_ok(op):
00051         for op_match in op_blacklist:
00052             if fnmatchcase(op, op_match):
00053                 print("    skipping: %s (%s)" % (op, op_match))
00054                 return False
00055         return True
00056 
00057     operators[:] = [op for op in operators if is_op_ok(op[0])]
00058 
00059 
00060 def run_ops(operators, setup_func=None):
00061     print("\ncontext:", setup_func.__name__)
00062     # first invoke
00063     for op_id, op in operators:
00064         if op.poll():
00065             print("    operator:", op_id)
00066             sys.stdout.flush()  # incase of crash
00067 
00068             # disable will get blender in a bad state and crash easy!
00069             bpy.ops.wm.read_factory_settings()
00070 
00071             setup_func()
00072 
00073             for mode in {'EXEC_DEFAULT', 'INVOKE_DEFAULT'}:
00074                 try:
00075                     op(mode)
00076                 except:
00077                     #import traceback
00078                     #traceback.print_exc()
00079                     pass
00080 
00081 
00082 # contexts
00083 def ctx_clear_scene():  # copied from batch_import.py
00084     unique_obs = set()
00085     for scene in bpy.data.scenes:
00086         for obj in scene.objects[:]:
00087             scene.objects.unlink(obj)
00088             unique_obs.add(obj)
00089 
00090     # remove obdata, for now only worry about the startup scene
00091     for bpy_data_iter in (bpy.data.objects, bpy.data.meshes, bpy.data.lamps, bpy.data.cameras):
00092         for id_data in bpy_data_iter:
00093             bpy_data_iter.remove(id_data)
00094 
00095 
00096 def ctx_editmode_mesh():
00097     bpy.ops.object.mode_set(mode='EDIT')
00098     bpy.ops.object.vertex_group_add()
00099 
00100 
00101 def ctx_editmode_curves():
00102     bpy.ops.curve.primitive_nurbs_circle_add()
00103     bpy.ops.object.mode_set(mode='EDIT')
00104 
00105 
00106 def ctx_editmode_surface():
00107     bpy.ops.surface.primitive_nurbs_surface_torus_add()
00108     bpy.ops.object.mode_set(mode='EDIT')
00109 
00110 
00111 def ctx_editmode_mball():
00112     bpy.ops.object.metaball_add()
00113     bpy.ops.object.mode_set(mode='EDIT')
00114 
00115 
00116 def ctx_editmode_text():
00117     bpy.ops.object.text_add()
00118     bpy.ops.object.mode_set(mode='EDIT')
00119 
00120 
00121 def ctx_editmode_armature():
00122     bpy.ops.object.armature_add()
00123     bpy.ops.object.mode_set(mode='EDIT')
00124 
00125 
00126 def ctx_editmode_lattice():
00127     bpy.ops.object.add(type='LATTICE')
00128     bpy.ops.object.mode_set(mode='EDIT')
00129     # bpy.ops.object.vertex_group_add()
00130 
00131 
00132 def ctx_object_empty():
00133     bpy.ops.object.add(type='EMPTY')
00134 
00135 
00136 def ctx_weightpaint():
00137     bpy.ops.object.mode_set(mode='WEIGHT_PAINT')
00138 
00139 
00140 def main():
00141     # bpy.ops.wm.read_factory_settings()
00142     import bpy
00143     operators = []
00144     for mod_name in dir(bpy.ops):
00145         mod = getattr(bpy.ops, mod_name)
00146         for submod_name in dir(mod):
00147             op = getattr(mod, submod_name)
00148             operators.append(("%s.%s" % (mod_name, submod_name), op))
00149 
00150     operators.sort(key=lambda op: op[0])
00151 
00152     filter_op_list(operators)
00153 
00154     # for testing, mix the list up.
00155     #operators.reverse()
00156 
00157     #import random
00158     #random.shuffle(operators)
00159 
00160     # Run the operator tests in different contexts
00161     run_ops(operators, setup_func=lambda: None)
00162     run_ops(operators, setup_func=ctx_editmode_surface)
00163     run_ops(operators, setup_func=ctx_object_empty)
00164     run_ops(operators, setup_func=ctx_editmode_armature)
00165     run_ops(operators, setup_func=ctx_editmode_mesh)
00166     run_ops(operators, setup_func=ctx_clear_scene)
00167     run_ops(operators, setup_func=ctx_editmode_curves)
00168     run_ops(operators, setup_func=ctx_editmode_mball)
00169     run_ops(operators, setup_func=ctx_editmode_text)
00170     run_ops(operators, setup_func=ctx_weightpaint)
00171     run_ops(operators, setup_func=ctx_editmode_lattice)
00172 
00173     print("finished")
00174 
00175 if __name__ == "__main__":
00176     main()