1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # ##### END GPL LICENSE BLOCK #####
21 # for slightly faster access
22 from _bpy import ops as ops_module
24 op_add = ops_module.add
25 op_remove = ops_module.remove
26 op_dir = ops_module.dir
27 op_call = ops_module.call
28 op_as_string = ops_module.as_string
29 op_get_rna = ops_module.get_rna
31 # Keep in sync with WM_types.h
34 'INVOKE_REGION_WIN': 1,
44 class bpy_ops(object):
46 Fake module like class.
51 def __getattr__(self, module):
53 gets a bpy.ops submodule
55 if module.startswith('__'):
56 raise AttributeError(module)
57 return bpy_ops_submodule(module)
62 def remove(self, pyop):
69 # add this classes functions
70 for id_name in dir(self.__class__):
71 if not id_name.startswith('__'):
72 submodules.add(id_name)
74 for id_name in op_dir():
75 id_split = id_name.split('_OT_', 1)
77 if len(id_split) == 2:
78 submodules.add(id_split[0].lower())
80 submodules.add(id_split[0])
82 return list(submodules)
85 return "<module like class 'bpy.ops'>"
88 class bpy_ops_submodule(object):
90 Utility class to fake submodules.
94 __keys__ = ('module',)
96 def __init__(self, module):
99 def __getattr__(self, func):
101 gets a bpy.ops.submodule function
103 if func.startswith('__'):
104 raise AttributeError(func)
105 return bpy_ops_submodule_op(self.module, func)
111 module_upper = self.module.upper()
113 for id_name in op_dir():
114 id_split = id_name.split('_OT_', 1)
115 if len(id_split) == 2 and module_upper == id_split[0]:
116 functions.add(id_split[1])
118 return list(functions)
121 return "<module like class 'bpy.ops.%s'>" % self.module
124 class bpy_ops_submodule_op(object):
126 Utility class to fake submodule operators.
128 eg. bpy.ops.object.somefunc
131 __keys__ = ('module', 'func')
135 return op_as_string(self.idname())
137 __doc__ = property(_get_doc)
139 def __init__(self, module, func):
144 # submod.foo -> SUBMOD_OT_foo
145 return self.module.upper() + '_OT_' + self.func
147 def __call__(self, *args, **kw):
149 # Get the operator from blender
151 raise ValueError("1 or 2 args execution context is supported")
157 C_exec = 'EXEC_DEFAULT'
163 if type(args[0]) != str:
169 context = context_dict[C_exec]
171 raise ValueError("Expected a single context argument in: " + \
172 str(list(context_dict.keys())))
177 return op_call(self.idname(), C_dict, kw, context)
180 return op_call(self.idname(), C_dict, kw)
184 currently only used for 'bl_rna'
186 return op_get_rna(self.idname())
188 def __repr__(self): # useful display, repr(op)
189 return op_as_string(self.idname())
191 def __str__(self): # used for print(...)
192 return "<function bpy.ops.%s.%s at 0x%x'>" % \
193 (self.module, self.func, id(self))
195 ops_fake_module = bpy_ops()