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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
23 from bpy.props import *
24 from rna_prop_ui import rna_idprop_ui_prop_get, rna_idprop_ui_prop_clear
27 class MESH_OT_delete_edgeloop(bpy.types.Operator):
28 '''Delete an edge loop by merging the faces on each side to a single face loop'''
29 bl_idname = "mesh.delete_edgeloop"
30 bl_label = "Delete Edge Loop"
32 def execute(self, context):
33 if 'FINISHED' in bpy.ops.transform.edge_slide(value=1.0):
34 bpy.ops.mesh.select_more()
35 bpy.ops.mesh.remove_doubles()
40 rna_path_prop = StringProperty(name="Context Attributes",
41 description="rna context string", maxlen=1024, default="")
43 rna_reverse_prop = BoolProperty(name="Reverse",
44 description="Cycle backwards", default=False)
46 rna_relative_prop = BoolProperty(name="Relative",
47 description="Apply relative to the current value (delta)",
51 def context_path_validate(context, data_path):
54 value = eval("context.%s" % data_path)
55 except AttributeError:
56 if "'NoneType'" in str(sys.exc_info()[1]):
57 # One of the items in the rna path is None, just ignore this
60 # We have a real error in the rna path, dont ignore that
66 def execute_context_assign(self, context):
67 if context_path_validate(context, self.data_path) is Ellipsis:
68 return {'PASS_THROUGH'}
70 if getattr(self, "relative", False):
71 exec("context.%s+=self.value" % self.data_path)
73 exec("context.%s=self.value" % self.data_path)
78 class BRUSH_OT_set_active_number(bpy.types.Operator):
79 '''Set active sculpt/paint brush from it's number'''
80 bl_idname = "brush.set_active_number"
81 bl_label = "Set Brush Number"
83 mode = StringProperty(name="mode",
84 description="Paint mode to set brush for", maxlen=1024)
85 number = IntProperty(name="number",
86 description="Brush number")
88 _attr_dict = {"sculpt" : "use_paint_sculpt",
89 "vertex_paint": "use_paint_vertex",
90 "weight_paint": "use_paint_weight",
91 "image_paint" : "use_paint_texture"}
93 def execute(self, context):
94 attr = self._attr_dict.get(self.mode)
98 for i, brush in enumerate((cur for cur in bpy.data.brushes if getattr(cur, attr))):
100 getattr(context.tool_settings, self.mode).brush = brush
105 class WM_OT_context_set_boolean(bpy.types.Operator):
106 '''Set a context value.'''
107 bl_idname = "wm.context_set_boolean"
108 bl_label = "Context Set Boolean"
109 bl_options = {'UNDO'}
111 data_path = rna_path_prop
112 value = BoolProperty(name="Value",
113 description="Assignment value", default=True)
115 execute = execute_context_assign
118 class WM_OT_context_set_int(bpy.types.Operator): # same as enum
119 '''Set a context value.'''
120 bl_idname = "wm.context_set_int"
121 bl_label = "Context Set"
122 bl_options = {'UNDO'}
124 data_path = rna_path_prop
125 value = IntProperty(name="Value", description="Assign value", default=0)
126 relative = rna_relative_prop
128 execute = execute_context_assign
131 class WM_OT_context_scale_int(bpy.types.Operator):
132 '''Scale an int context value.'''
133 bl_idname = "wm.context_scale_int"
134 bl_label = "Context Set"
135 bl_options = {'UNDO'}
137 data_path = rna_path_prop
138 value = FloatProperty(name="Value", description="Assign value", default=1.0)
139 always_step = BoolProperty(name="Always Step",
140 description="Always adjust the value by a minimum of 1 when 'value' is not 1.0.",
143 def execute(self, context):
144 if context_path_validate(context, self.data_path) is Ellipsis:
145 return {'PASS_THROUGH'}
148 data_path = self.data_path
150 if value == 1.0: # nothing to do
153 if getattr(self, "always_step", False):
160 exec("context.%s = %s(round(context.%s * value), context.%s + %s)" % (data_path, func, data_path, data_path, add))
162 exec("context.%s *= value" % self.data_path)
167 class WM_OT_context_set_float(bpy.types.Operator): # same as enum
168 '''Set a context value.'''
169 bl_idname = "wm.context_set_float"
170 bl_label = "Context Set Float"
171 bl_options = {'UNDO'}
173 data_path = rna_path_prop
174 value = FloatProperty(name="Value",
175 description="Assignment value", default=0.0)
176 relative = rna_relative_prop
178 execute = execute_context_assign
181 class WM_OT_context_set_string(bpy.types.Operator): # same as enum
182 '''Set a context value.'''
183 bl_idname = "wm.context_set_string"
184 bl_label = "Context Set String"
185 bl_options = {'UNDO'}
187 data_path = rna_path_prop
188 value = StringProperty(name="Value",
189 description="Assign value", maxlen=1024, default="")
191 execute = execute_context_assign
194 class WM_OT_context_set_enum(bpy.types.Operator):
195 '''Set a context value.'''
196 bl_idname = "wm.context_set_enum"
197 bl_label = "Context Set Enum"
198 bl_options = {'UNDO'}
200 data_path = rna_path_prop
201 value = StringProperty(name="Value",
202 description="Assignment value (as a string)",
203 maxlen=1024, default="")
205 execute = execute_context_assign
208 class WM_OT_context_set_value(bpy.types.Operator):
209 '''Set a context value.'''
210 bl_idname = "wm.context_set_value"
211 bl_label = "Context Set Value"
212 bl_options = {'UNDO'}
214 data_path = rna_path_prop
215 value = StringProperty(name="Value",
216 description="Assignment value (as a string)",
217 maxlen=1024, default="")
219 def execute(self, context):
220 if context_path_validate(context, self.data_path) is Ellipsis:
221 return {'PASS_THROUGH'}
222 exec("context.%s=%s" % (self.data_path, self.value))
226 class WM_OT_context_toggle(bpy.types.Operator):
227 '''Toggle a context value.'''
228 bl_idname = "wm.context_toggle"
229 bl_label = "Context Toggle"
230 bl_options = {'UNDO'}
232 data_path = rna_path_prop
234 def execute(self, context):
236 if context_path_validate(context, self.data_path) is Ellipsis:
237 return {'PASS_THROUGH'}
239 exec("context.%s=not (context.%s)" %
240 (self.data_path, self.data_path))
245 class WM_OT_context_toggle_enum(bpy.types.Operator):
246 '''Toggle a context value.'''
247 bl_idname = "wm.context_toggle_enum"
248 bl_label = "Context Toggle Values"
249 bl_options = {'UNDO'}
251 data_path = rna_path_prop
252 value_1 = StringProperty(name="Value", \
253 description="Toggle enum", maxlen=1024, default="")
255 value_2 = StringProperty(name="Value", \
256 description="Toggle enum", maxlen=1024, default="")
258 def execute(self, context):
260 if context_path_validate(context, self.data_path) is Ellipsis:
261 return {'PASS_THROUGH'}
263 exec("context.%s = ['%s', '%s'][context.%s!='%s']" % \
264 (self.data_path, self.value_1,\
265 self.value_2, self.data_path,
271 class WM_OT_context_cycle_int(bpy.types.Operator):
272 '''Set a context value. Useful for cycling active material, '''
273 '''vertex keys, groups' etc.'''
274 bl_idname = "wm.context_cycle_int"
275 bl_label = "Context Int Cycle"
276 bl_options = {'UNDO'}
278 data_path = rna_path_prop
279 reverse = rna_reverse_prop
281 def execute(self, context):
282 data_path = self.data_path
283 value = context_path_validate(context, data_path)
284 if value is Ellipsis:
285 return {'PASS_THROUGH'}
292 exec("context.%s=value" % data_path)
294 if value != eval("context.%s" % data_path):
295 # relies on rna clamping int's out of the range
297 value = (1 << 31) - 1
301 exec("context.%s=value" % data_path)
306 class WM_OT_context_cycle_enum(bpy.types.Operator):
307 '''Toggle a context value.'''
308 bl_idname = "wm.context_cycle_enum"
309 bl_label = "Context Enum Cycle"
310 bl_options = {'UNDO'}
312 data_path = rna_path_prop
313 reverse = rna_reverse_prop
315 def execute(self, context):
317 value = context_path_validate(context, self.data_path)
318 if value is Ellipsis:
319 return {'PASS_THROUGH'}
323 # Have to get rna enum values
324 rna_struct_str, rna_prop_str = self.data_path.rsplit('.', 1)
325 i = rna_prop_str.find('[')
327 # just incse we get "context.foo.bar[0]"
329 rna_prop_str = rna_prop_str[0:i]
331 rna_struct = eval("context.%s.rna_type" % rna_struct_str)
333 rna_prop = rna_struct.properties[rna_prop_str]
335 if type(rna_prop) != bpy.types.EnumProperty:
336 raise Exception("expected an enum property")
338 enums = rna_struct.properties[rna_prop_str].items.keys()
339 orig_index = enums.index(orig_value)
341 # Have the info we need, advance to the next item
344 advance_enum = enums[-1]
346 advance_enum = enums[orig_index - 1]
348 if orig_index == len(enums) - 1:
349 advance_enum = enums[0]
351 advance_enum = enums[orig_index + 1]
354 exec("context.%s=advance_enum" % self.data_path)
358 class WM_OT_context_cycle_array(bpy.types.Operator):
359 '''Set a context array value.
360 Useful for cycling the active mesh edit mode.'''
361 bl_idname = "wm.context_cycle_array"
362 bl_label = "Context Array Cycle"
363 bl_options = {'UNDO'}
365 data_path = rna_path_prop
366 reverse = rna_reverse_prop
368 def execute(self, context):
369 data_path = self.data_path
370 value = context_path_validate(context, data_path)
371 if value is Ellipsis:
372 return {'PASS_THROUGH'}
376 array.insert(0, array.pop())
378 array.append(array.pop(0))
381 exec("context.%s=cycle(context.%s[:])" % (data_path, data_path))
386 class WM_OT_context_set_id(bpy.types.Operator):
387 '''Toggle a context value.'''
388 bl_idname = "wm.context_set_id"
389 bl_label = "Set Library ID"
390 bl_options = {'UNDO'}
392 data_path = rna_path_prop
393 value = StringProperty(name="Value",
394 description="Assign value", maxlen=1024, default="")
396 def execute(self, context):
398 data_path = self.data_path
400 # match the pointer type from the target property to bpy.data.*
401 # so we lookup the correct list.
402 data_path_base, data_path_prop = data_path.rsplit(".", 1)
403 data_prop_rna = eval("context.%s" % data_path_base).rna_type.properties[data_path_prop]
404 data_prop_rna_type = data_prop_rna.fixed_type
408 for prop in bpy.data.rna_type.properties:
409 if prop.rna_type.identifier == "CollectionProperty":
410 if prop.fixed_type == data_prop_rna_type:
411 id_iter = prop.identifier
415 value_id = getattr(bpy.data, id_iter).get(value)
416 exec("context.%s=value_id" % data_path)
421 doc_id = StringProperty(name="Doc ID",
422 description="", maxlen=1024, default="", options={'HIDDEN'})
424 doc_new = StringProperty(name="Edit Description",
425 description="", maxlen=1024, default="")
428 class WM_OT_context_modal_mouse(bpy.types.Operator):
429 '''Adjust arbitrary values with mouse input'''
430 bl_idname = "wm.context_modal_mouse"
431 bl_label = "Context Modal Mouse"
433 data_path_iter = StringProperty(description="The data path relative to the context, must point to an iterable.")
434 data_path_item = StringProperty(description="The data path from each iterable to the value (int or float)")
435 input_scale = FloatProperty(default=0.01, description="Scale the mouse movement by this value before applying the delta")
436 invert = BoolProperty(default=False, description="Invert the mouse input")
437 initial_x = IntProperty(options={'HIDDEN'})
439 def _values_store(self, context):
440 data_path_iter = self.data_path_iter
441 data_path_item = self.data_path_item
443 self._values = values = {}
445 for item in getattr(context, data_path_iter):
447 value_orig = eval("item." + data_path_item)
451 # check this can be set, maybe this is library data.
453 exec("item.%s = %s" % (data_path_item, value_orig))
457 values[item] = value_orig
459 def _values_delta(self, delta):
460 delta *= self.input_scale
464 data_path_item = self.data_path_item
465 for item, value_orig in self._values.items():
466 if type(value_orig) == int:
467 exec("item.%s = int(%d)" % (data_path_item, round(value_orig + delta)))
469 exec("item.%s = %f" % (data_path_item, value_orig + delta))
471 def _values_restore(self):
472 data_path_item = self.data_path_item
473 for item, value_orig in self._values.items():
474 exec("item.%s = %s" % (data_path_item, value_orig))
478 def _values_clear(self):
481 def modal(self, context, event):
482 event_type = event.type
484 if event_type == 'MOUSEMOVE':
485 delta = event.mouse_x - self.initial_x
486 self._values_delta(delta)
488 elif 'LEFTMOUSE' == event_type:
492 elif event_type in ('RIGHTMOUSE', 'ESC'):
493 self._values_restore()
496 return {'RUNNING_MODAL'}
498 def invoke(self, context, event):
499 self._values_store(context)
502 self.report({'WARNING'}, "Nothing to operate on: %s[ ].%s" %
503 (self.data_path_iter, self.data_path_item))
507 self.initial_x = event.mouse_x
509 context.window_manager.add_modal_handler(self)
510 return {'RUNNING_MODAL'}
513 class WM_OT_url_open(bpy.types.Operator):
514 "Open a website in the Webbrowser"
515 bl_idname = "wm.url_open"
518 url = StringProperty(name="URL", description="URL to open")
520 def execute(self, context):
522 webbrowser.open(self.url)
526 class WM_OT_path_open(bpy.types.Operator):
527 "Open a path in a file browser"
528 bl_idname = "wm.path_open"
531 filepath = StringProperty(name="File Path", maxlen=1024)
533 def execute(self, context):
538 filepath = bpy.path.abspath(self.filepath)
539 filepath = os.path.normpath(filepath)
541 if not os.path.exists(filepath):
542 self.report({'ERROR'}, "File '%s' not found" % filepath)
545 if sys.platform == 'win32':
546 subprocess.Popen(['start', filepath], shell=True)
547 elif sys.platform == 'darwin':
548 subprocess.Popen(['open', filepath])
551 subprocess.Popen(['xdg-open', filepath])
553 # xdg-open *should* be supported by recent Gnome, KDE, Xfce
559 class WM_OT_doc_view(bpy.types.Operator):
560 '''Load online reference docs'''
561 bl_idname = "wm.doc_view"
562 bl_label = "View Documentation"
565 _prefix = "http://www.blender.org/documentation/blender_python_api_%s" % "_".join(str(v) for v in bpy.app.version)
567 def _nested_class_string(self, class_string):
569 class_obj = getattr(bpy.types, class_string, None).bl_rna
571 ls.insert(0, class_obj)
572 class_obj = class_obj.nested
573 return '.'.join(class_obj.identifier for class_obj in ls)
575 def execute(self, context):
576 id_split = self.doc_id.split('.')
577 if len(id_split) == 1: # rna, class
578 url = '%s/bpy.types.%s.html' % (self._prefix, id_split[0])
579 elif len(id_split) == 2: # rna, class.prop
580 class_name, class_prop = id_split
582 if hasattr(bpy.types, class_name.upper() + '_OT_' + class_prop):
583 url = '%s/bpy.ops.%s.html#bpy.ops.%s.%s' % \
584 (self._prefix, class_name, class_name, class_prop)
586 # It so happens that epydoc nests these, not sphinx
587 # class_name_full = self._nested_class_string(class_name)
588 url = '%s/bpy.types.%s.html#bpy.types.%s.%s' % \
589 (self._prefix, class_name, class_name, class_prop)
592 return {'PASS_THROUGH'}
600 class WM_OT_doc_edit(bpy.types.Operator):
601 '''Load online reference docs'''
602 bl_idname = "wm.doc_edit"
603 bl_label = "Edit Documentation"
608 _url = "http://www.mindrones.com/blender/svn/xmlrpc.php"
610 def _send_xmlrpc(self, data_dict):
611 print("sending data:", data_dict)
617 docblog = xmlrpc.client.ServerProxy(self._url)
618 docblog.metaWeblog.newPost(1, user, pwd, data_dict, 1)
620 def execute(self, context):
623 doc_new = self.doc_new
625 class_name, class_prop = doc_id.split('.')
628 self.report({'ERROR'}, "No input given for '%s'" % doc_id)
631 # check if this is an operator
632 op_name = class_name.upper() + '_OT_' + class_prop
633 op_class = getattr(bpy.types, op_name, None)
635 # Upload this to the web server
639 rna = op_class.bl_rna
640 doc_orig = rna.description
641 if doc_orig == doc_new:
642 return {'RUNNING_MODAL'}
644 print("op - old:'%s' -> new:'%s'" % (doc_orig, doc_new))
645 upload["title"] = 'OPERATOR %s:%s' % (doc_id, doc_orig)
647 rna = getattr(bpy.types, class_name).bl_rna
648 doc_orig = rna.properties[class_prop].description
649 if doc_orig == doc_new:
650 return {'RUNNING_MODAL'}
652 print("rna - old:'%s' -> new:'%s'" % (doc_orig, doc_new))
653 upload["title"] = 'RNA %s:%s' % (doc_id, doc_orig)
655 upload["description"] = doc_new
657 self._send_xmlrpc(upload)
661 def draw(self, context):
663 layout.label(text="Descriptor ID: '%s'" % self.doc_id)
664 layout.prop(self, "doc_new", text="")
666 def invoke(self, context, event):
667 wm = context.window_manager
668 return wm.invoke_props_dialog(self, width=600)
672 from bpy.props import *
675 rna_path = StringProperty(name="Property Edit",
676 description="Property data_path edit", maxlen=1024, default="", options={'HIDDEN'})
678 rna_value = StringProperty(name="Property Value",
679 description="Property value edit", maxlen=1024, default="")
681 rna_property = StringProperty(name="Property Name",
682 description="Property name edit", maxlen=1024, default="")
684 rna_min = FloatProperty(name="Min", default=0.0, precision=3)
685 rna_max = FloatProperty(name="Max", default=1.0, precision=3)
688 class WM_OT_properties_edit(bpy.types.Operator):
689 '''Internal use (edit a property data_path)'''
690 bl_idname = "wm.properties_edit"
691 bl_label = "Edit Property"
692 bl_options = {'REGISTER'} # only because invoke_props_popup requires.
695 property = rna_property
699 description = StringProperty(name="Tip", default="")
701 def execute(self, context):
702 data_path = self.data_path
705 prop_old = self._last_prop[0]
708 value_eval = eval(value)
713 item = eval("context.%s" % data_path)
715 rna_idprop_ui_prop_clear(item, prop_old)
716 exec_str = "del item['%s']" % prop_old
721 exec_str = "item['%s'] = %s" % (prop, repr(value_eval))
724 self._last_prop[:] = [prop]
726 prop_type = type(item[prop])
728 prop_ui = rna_idprop_ui_prop_get(item, prop)
730 if prop_type in (float, int):
732 prop_ui['soft_min'] = prop_ui['min'] = prop_type(self.min)
733 prop_ui['soft_max'] = prop_ui['max'] = prop_type(self.max)
735 prop_ui['description'] = self.description
739 def invoke(self, context, event):
741 self._last_prop = [self.property]
743 item = eval("context.%s" % self.data_path)
746 prop_ui = rna_idprop_ui_prop_get(item, self.property, False) # dont create
748 self.min = prop_ui.get("min", -1000000000)
749 self.max = prop_ui.get("max", 1000000000)
750 self.description = prop_ui.get("description", "")
752 wm = context.window_manager
753 return wm.invoke_props_popup(self, event)
756 class WM_OT_properties_add(bpy.types.Operator):
757 '''Internal use (edit a property data_path)'''
758 bl_idname = "wm.properties_add"
759 bl_label = "Add Property"
763 def execute(self, context):
764 item = eval("context.%s" % self.data_path)
766 def unique_name(names):
770 while prop_new in names:
771 prop_new = prop + str(i)
776 property = unique_name(item.keys())
782 class WM_OT_properties_remove(bpy.types.Operator):
783 '''Internal use (edit a property data_path)'''
784 bl_idname = "wm.properties_remove"
785 bl_label = "Remove Property"
788 property = rna_property
790 def execute(self, context):
791 item = eval("context.%s" % self.data_path)
792 del item[self.property]
796 class WM_OT_keyconfig_activate(bpy.types.Operator):
797 bl_idname = "wm.keyconfig_activate"
798 bl_label = "Activate Keyconfig"
800 filepath = StringProperty(name="File Path", maxlen=1024)
802 def execute(self, context):
803 bpy.utils.keyconfig_set(self.filepath)
806 class WM_OT_sysinfo(bpy.types.Operator):
807 '''Generate System Info'''
808 bl_idname = "wm.sysinfo"
809 bl_label = "System Info"
811 def execute(self, context):
813 sys_info.write_sysinfo(self)
823 if __name__ == "__main__":