Cleanup: quiet strict name warnings for addons a..h.
[blender-addons.git] / development_edit_operator.py
blob54d5424e9f4776e2a84e00dc55a8a697410d7e27
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 #####
20 bl_info = {
21 "name": "Edit Operator Source",
22 "author": "scorpion81",
23 "version": (1, 2, 2),
24 "blender": (2, 78, 0),
25 "location": "Text Editor > Edit > Edit Operator",
26 "description": "Opens source file of chosen operator, if it is an add-on one",
27 "warning": "",
28 "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/"
29 "Py/Scripts/Development/Edit_Operator_Source",
30 "category": "Development"}
32 import bpy
33 import sys
34 import inspect
35 from bpy.types import (
36 Operator,
37 Panel,
39 from bpy.props import EnumProperty
42 def getclazz(opname):
43 opid = opname.split(".")
44 opmod = getattr(bpy.ops, opid[0])
45 op = getattr(opmod, opid[1])
46 id = op.get_rna().bl_rna.identifier
47 clazz = getattr(bpy.types, id)
48 return clazz
51 def getmodule(opname):
52 addon = True
53 clazz = getclazz(opname)
54 modn = clazz.__module__
56 try:
57 line = inspect.getsourcelines(clazz)[1]
58 except IOError:
59 line = -1
60 except TypeError:
61 line = -1
63 if modn == 'bpy.types':
64 mod = 'C operator'
65 addon = False
66 elif modn != '__main__':
67 mod = sys.modules[modn].__file__
68 else:
69 addon = False
70 mod = modn
72 return mod, line, addon
75 def get_ops():
76 allops = []
77 opsdir = dir(bpy.ops)
78 for opmodname in opsdir:
79 opmod = getattr(bpy.ops, opmodname)
80 opmoddir = dir(opmod)
81 for o in opmoddir:
82 name = opmodname + "." + o
83 clazz = getclazz(name)
84 if (clazz.__module__ != 'bpy.types'):
85 allops.append(name)
86 del opmoddir
88 # add own operator name too, since its not loaded yet when this is called
89 allops.append("text.edit_operator")
90 l = sorted(allops)
91 del allops
92 del opsdir
94 return [(y, y, "", x) for x, y in enumerate(l)]
97 class EditOperator(Operator):
98 bl_idname = "text.edit_operator"
99 bl_label = "Edit Operator"
100 bl_description = "Opens the source file of operators chosen from Menu"
101 bl_property = "op"
103 items = get_ops()
105 op = EnumProperty(
106 name="Op",
107 description="",
108 items=items
111 def invoke(self, context, event):
112 context.window_manager.invoke_search_popup(self)
113 return {'PASS_THROUGH'}
115 def execute(self, context):
116 found = False
117 path, line, addon = getmodule(self.op)
118 if addon:
119 for t in bpy.data.texts:
120 if t.filepath == path:
121 ctx = context.copy()
122 ctx['edit_text'] = t
123 bpy.ops.text.jump(ctx, line=line)
124 found = True
125 break
127 if (found is False):
128 self.report({'INFO'},
129 "Opened file: " + path)
130 bpy.ops.text.open(filepath=path)
131 bpy.ops.text.jump(line=line)
133 return {'FINISHED'}
134 else:
135 self.report({'WARNING'},
136 "Found no source file for " + self.op)
138 return {'CANCELLED'}
141 class EditOperatorPanel(Panel):
142 bl_idname = "DEVEDIT_PT_operator"
143 bl_space_type = 'TEXT_EDITOR'
144 bl_region_type = 'UI'
145 bl_label = "Edit Operator"
147 def draw(self, context):
148 layout = self.layout
149 layout.operator("text.edit_operator")
152 def register():
153 bpy.utils.register_class(EditOperator)
154 bpy.utils.register_class(EditOperatorPanel)
157 def unregister():
158 bpy.utils.unregister_class(EditOperatorPanel)
159 bpy.utils.unregister_class(EditOperator)
162 if __name__ == "__main__":
163 register()