- improvements from Mathias Panzenböck (panzi) patch [#19695], which avoid conversion...
[blender-staging.git] / release / scripts / modules / bpy_ops.py
1 # for slightly faster access
2 from bpy.__ops__ import add             as op_add
3 from bpy.__ops__ import remove          as op_remove
4 from bpy.__ops__ import dir             as op_dir
5 from bpy.__ops__ import call            as op_call
6 from bpy.__ops__ import as_string       as op_as_string
7 from bpy.__ops__ import get_rna as op_get_rna
8
9 # Keep in sync with WM_types.h
10 context_dict = {
11         'INVOKE_DEFAULT':0,
12         'INVOKE_REGION_WIN':1,
13         'INVOKE_AREA':2,
14         'INVOKE_SCREEN':3,
15         'EXEC_DEFAULT':4,
16         'EXEC_REGION_WIN':5,
17         'EXEC_AREA':6,
18         'EXEC_SCREEN':7,
19 }
20
21 class bpy_ops(object):
22         '''
23         Fake module like class.
24         
25          bpy.ops
26         '''
27         def add(self, pyop):
28                 op_add(pyop)
29         
30         def remove(self, pyop):
31                 op_remove(pyop)
32         
33         def __getattr__(self, module):
34                 '''
35                 gets a bpy.ops submodule
36                 '''
37                 return bpy_ops_submodule(module)
38                 
39         def __dir__(self):
40                 
41                 submodules = set()
42                 
43                 # add this classes functions
44                 for id_name in dir(self.__class__):
45                         if not id_name.startswith('__'):
46                                 submodules.add(id_name)
47                 
48                 for id_name in op_dir():
49                         id_split = id_name.split('_OT_', 1)
50                         
51                         if len(id_split) == 2:
52                                 submodules.add(id_split[0].lower())
53                         else:
54                                 submodules.add(id_split[0])
55                 
56                 return list(submodules)
57                 
58         def __repr__(self):
59                 return "<module like class 'bpy.ops'>"
60
61
62 class bpy_ops_submodule(object):
63         '''
64         Utility class to fake submodules.
65         
66         eg. bpy.ops.object
67         '''
68         __keys__ = ('module',)
69         
70         def __init__(self, module):
71                 self.module = module
72                 
73         def __getattr__(self, func):
74                 '''
75                 gets a bpy.ops.submodule function
76                 '''
77                 return bpy_ops_submodule_op(self.module, func)
78                 
79         def __dir__(self):
80                 
81                 functions = set()
82                 
83                 module_upper = self.module.upper()
84                 
85                 for id_name in op_dir():
86                         id_split = id_name.split('_OT_', 1)
87                         if len(id_split) == 2 and module_upper == id_split[0]:
88                                 functions.add(id_split[1])
89                 
90                 return list(functions)
91         
92         def __repr__(self):
93                 return "<module like class 'bpy.ops.%s'>" % self.module
94
95 class bpy_ops_submodule_op(object):
96         '''
97         Utility class to fake submodule operators.
98         
99         eg. bpy.ops.object.somefunc
100         '''
101         __keys__ = ('module', 'func')
102         def __init__(self, module, func):
103                 self.module = module
104                 self.func = func
105         
106         def idname(self):
107                 # submod.foo -> SUBMOD_OT_foo
108                 return self.module.upper() + '_OT_' + self.func
109         
110         def __call__(self, *args, **kw):
111                 
112                 # Get the operator from blender
113                 if len(args) > 1:
114                         raise ValueError("only one argument for the execution context is supported ")
115                 
116                 if args:
117                         try:
118                                 context = context_dict[args[0]]
119                         except:
120                                 raise ValueError("Expected a single context argument in: " + str(list(context_dict.keys())))
121                         
122                         return op_call(self.idname(), kw, context)
123                 
124                 else:
125                         return op_call(self.idname(), kw)
126         
127         def get_rna(self):
128                 '''
129                 currently only used for '__rna__'
130                 '''
131                 return op_get_rna(self.idname())
132                         
133         
134         def __repr__(self): # useful display, repr(op)
135                 return op_as_string(self.idname())
136         
137         def __str__(self): # used for print(...)
138                 return "<function bpy.ops.%s.%s at 0x%x'>" % (self.module, self.func, id(self))
139
140 import bpy
141 bpy.ops = bpy_ops()
142
143 # TODO, C macro's cant define settings :|
144
145 class MESH_OT_delete_edgeloop(bpy.types.Operator):
146         '''Export a single object as a stanford PLY with normals, colours and texture coordinates.'''
147         __idname__ = "mesh.delete_edgeloop"
148         __label__ = "Delete Edge Loop"
149         
150         def execute(self, context):
151                 bpy.ops.tfm.edge_slide(value=1.0)
152                 bpy.ops.mesh.select_more()
153                 bpy.ops.mesh.remove_doubles()
154                 return ('FINISHED',)
155
156 rna_path_prop = bpy.props.StringProperty(attr="path", name="Context Attributes", description="rna context string", maxlen= 1024, default= "")
157
158 def execute_context_assign(self, context):
159         exec("context.%s=self.value" % self.path)
160         return ('FINISHED',)
161
162 class WM_OT_context_set_boolean(bpy.types.Operator):
163         '''Set a context value.'''
164         __idname__ = "wm.context_set_boolean"
165         __label__ = "Context Set"
166         __props__ = [rna_path_prop, bpy.props.BoolProperty(attr="value", name="Value", description="Assignment value", default= True)]
167         execute = execute_context_assign
168
169 class WM_OT_context_set_int(bpy.types.Operator): # same as enum
170         '''Set a context value.'''
171         __idname__ = "wm.context_set_int"
172         __label__ = "Context Set"
173         __props__ = [rna_path_prop, bpy.props.IntProperty(attr="value", name="Value", description="Assignment value", default= 0)]
174         execute = execute_context_assign
175                 
176 class WM_OT_context_set_float(bpy.types.Operator): # same as enum
177         '''Set a context value.'''
178         __idname__ = "wm.context_set_int"
179         __label__ = "Context Set"
180         __props__ = [rna_path_prop, bpy.props.FloatProperty(attr="value", name="Value", description="Assignment value", default= 0.0)]
181         execute = execute_context_assign
182
183 class WM_OT_context_set_string(bpy.types.Operator): # same as enum
184         '''Set a context value.'''
185         __idname__ = "wm.context_set_string"
186         __label__ = "Context Set"
187         __props__ = [rna_path_prop, bpy.props.StringProperty(attr="value", name="Value", description="Assignment value", maxlen= 1024, default= "")]
188         execute = execute_context_assign
189
190 class WM_OT_context_set_enum(bpy.types.Operator):
191         '''Set a context value.'''
192         __idname__ = "wm.context_set_enum"
193         __label__ = "Context Set"
194         __props__ = [rna_path_prop, bpy.props.StringProperty(attr="value", name="Value", description="Assignment value (as a string)", maxlen= 1024, default= "")]
195         execute = execute_context_assign
196
197 class WM_OT_context_toggle(bpy.types.Operator):
198         '''Toggle a context value.'''
199         __idname__ = "wm.context_toggle"
200         __label__ = "Context Toggle"
201         __props__ = [rna_path_prop]
202         def execute(self, context):
203                 exec("context.%s=not (context.%s)" % (self.path, self.path)) # security nuts will complain.
204                 return ('FINISHED',)
205
206 class WM_OT_context_toggle_enum(bpy.types.Operator):
207         '''Toggle a context value.'''
208         __idname__ = "wm.context_toggle_enum"
209         __label__ = "Context Toggle Values"
210         __props__ = [
211                 rna_path_prop,
212                 bpy.props.StringProperty(attr="value_1", name="Value", description="Toggle enum", maxlen= 1024, default= ""),
213                 bpy.props.StringProperty(attr="value_2", name="Value", description="Toggle enum", maxlen= 1024, default= "")
214         ]
215         def execute(self, context):
216                 exec("context.%s = ['%s', '%s'][context.%s!='%s']" % (self.path, self.value_1, self.value_2, self.path, self.value_2)) # security nuts will complain.
217                 return ('FINISHED',)
218
219 class WM_OT_context_cycle_enum(bpy.types.Operator):
220         '''Toggle a context value.'''
221         __idname__ = "wm.context_cycle_enum"
222         __label__ = "Context Enum Cycle"
223         __props__ = [rna_path_prop, bpy.props.BoolProperty(attr="reverse", name="Reverse", description="Cycle backwards", default= False)]
224         def execute(self, context):
225                 orig_value = eval("context.%s" % self.path) # security nuts will complain.
226                 
227                 # Have to get rna enum values
228                 rna_struct_str, rna_prop_str =  self.path.rsplit('.', 1)
229                 i = rna_prop_str.find('[')
230                 if i != -1: rna_prop_str = rna_prop_str[0:i] # just incse we get "context.foo.bar[0]"
231                 
232                 rna_struct = eval("context.%s.rna_type" % rna_struct_str)
233                 
234                 rna_prop = rna_struct.properties[rna_prop_str]
235                 
236                 if type(rna_prop) != bpy.types.EnumProperty:
237                         raise Exception("expected an enum property")
238                 
239                 enums = rna_struct.properties[rna_prop_str].items.keys()
240                 orig_index = enums.index(orig_value)
241                 
242                 # Have the info we need, advance to the next item
243                 if self.reverse:
244                         if orig_index==0:                       advance_enum = enums[-1]
245                         else:                                   advance_enum = enums[orig_index-1]
246                 else:
247                         if orig_index==len(enums)-1:    advance_enum = enums[0]
248                         else:                                   advance_enum = enums[orig_index+1]
249                 
250                 # set the new value
251                 exec("context.%s=advance_enum" % self.path)
252                 return ('FINISHED',)
253
254 bpy.ops.add(MESH_OT_delete_edgeloop)
255
256 bpy.ops.add(WM_OT_context_set_boolean)
257 bpy.ops.add(WM_OT_context_set_int)
258 bpy.ops.add(WM_OT_context_set_float)
259 bpy.ops.add(WM_OT_context_set_string)
260 bpy.ops.add(WM_OT_context_set_enum)
261 bpy.ops.add(WM_OT_context_toggle)
262 bpy.ops.add(WM_OT_context_toggle_enum)
263 bpy.ops.add(WM_OT_context_cycle_enum)
264