added operators for setting rna for each type, this avoids having double "'quoted...
[blender.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 class WM_OT_context_set_boolean(bpy.types.Operator):
159         '''Set a context value.'''
160         __idname__ = "wm.context_set_boolean"
161         __label__ = "Context Set"
162         __props__ = [rna_path_prop, bpy.props.BoolProperty(attr="value", name="Value", description="Assignment value", default= True)]
163         def execute(self, context):
164                 exec("context.%s=%s" % (self.path, self.value)) # security nuts will complain.
165                 return ('FINISHED',)
166
167 class WM_OT_context_set_int(bpy.types.Operator): # same as enum
168         '''Set a context value.'''
169         __idname__ = "wm.context_set_int"
170         __label__ = "Context Set"
171         __props__ = [rna_path_prop, bpy.props.IntProperty(attr="value", name="Value", description="Assignment value", default= 0)]
172         def execute(self, context):
173                 exec("context.%s=%d" % (self.path, self.value)) # security nuts will complain.
174                 return ('FINISHED',)
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         def execute(self, context):
182                 exec("context.%s=%f" % (self.path, self.value)) # security nuts will complain.
183                 return ('FINISHED',)
184
185 class WM_OT_context_set_string(bpy.types.Operator): # same as enum
186         '''Set a context value.'''
187         __idname__ = "wm.context_set_string"
188         __label__ = "Context Set"
189         __props__ = [rna_path_prop, bpy.props.StringProperty(attr="value", name="Value", description="Assignment value", maxlen= 1024, default= "")]
190         def execute(self, context):
191                 exec("context.%s='%s'" % (self.path, self.value)) # security nuts will complain.
192                 return ('FINISHED',)
193
194 class WM_OT_context_set_enum(bpy.types.Operator):
195         '''Set a context value.'''
196         __idname__ = "wm.context_set_enum"
197         __label__ = "Context Set"
198         __props__ = [rna_path_prop, bpy.props.StringProperty(attr="value", name="Value", description="Assignment value (as a string)", maxlen= 1024, default= "")]
199         def execute(self, context):
200                 exec("context.%s='%s'" % (self.path, self.value)) # security nuts will complain.
201                 return ('FINISHED',)
202
203 class WM_OT_context_toggle(bpy.types.Operator):
204         '''Toggle a context value.'''
205         __idname__ = "wm.context_toggle"
206         __label__ = "Context Toggle"
207         __props__ = [rna_path_prop]
208         def execute(self, context):
209                 exec("context.%s=not (context.%s)" % (self.path, self.path)) # security nuts will complain.
210                 return ('FINISHED',)
211
212 class WM_OT_context_toggle_enum(bpy.types.Operator):
213         '''Toggle a context value.'''
214         __idname__ = "wm.context_toggle_enum"
215         __label__ = "Context Toggle Values"
216         __props__ = [
217                 rna_path_prop,
218                 bpy.props.StringProperty(attr="value_1", name="Value", description="Toggle enum", maxlen= 1024, default= ""),
219                 bpy.props.StringProperty(attr="value_2", name="Value", description="Toggle enum", maxlen= 1024, default= "")
220         ]
221         def execute(self, context):
222                 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.
223                 return ('FINISHED',)
224
225 class WM_OT_context_cycle_enum(bpy.types.Operator):
226         '''Toggle a context value.'''
227         __idname__ = "wm.context_cycle_enum"
228         __label__ = "Context Enum Cycle"
229         __props__ = [rna_path_prop, bpy.props.BoolProperty(attr="reverse", name="Reverse", description="Cycle backwards", default= False)]
230         def execute(self, context):
231                 orig_value = eval("context.%s" % self.path) # security nuts will complain.
232                 
233                 # Have to get rna enum values
234                 rna_struct_str, rna_prop_str =  self.path.rsplit('.', 1)
235                 i = rna_prop_str.find('[')
236                 if i != -1: rna_prop_str = rna_prop_str[0:i] # just incse we get "context.foo.bar[0]"
237                 
238                 rna_struct = eval("context.%s.rna_type" % rna_struct_str)
239                 
240                 rna_prop = rna_struct.properties[rna_prop_str]
241                 
242                 if type(rna_prop) != bpy.types.EnumProperty:
243                         raise Exception("expected an enum property")
244                 
245                 enums = rna_struct.properties[rna_prop_str].items.keys()
246                 orig_index = enums.index(orig_value)
247                 
248                 # Have the info we need, advance to the next item
249                 if self.reverse:
250                         if orig_index==0:                       advance_enum = enums[-1]
251                         else:                                   advance_enum = enums[orig_index-1]
252                 else:
253                         if orig_index==len(enums)-1:    advance_enum = enums[0]
254                         else:                                   advance_enum = enums[orig_index+1]
255                 
256                 # set the new value
257                 exec("context.%s=advance_enum" % self.path)
258                 return ('FINISHED',)
259
260 bpy.ops.add(MESH_OT_delete_edgeloop)
261
262 bpy.ops.add(WM_OT_context_set_boolean)
263 bpy.ops.add(WM_OT_context_set_int)
264 bpy.ops.add(WM_OT_context_set_float)
265 bpy.ops.add(WM_OT_context_set_string)
266 bpy.ops.add(WM_OT_context_set_enum)
267 bpy.ops.add(WM_OT_context_toggle)
268 bpy.ops.add(WM_OT_context_toggle_enum)
269 bpy.ops.add(WM_OT_context_cycle_enum)
270