2 # ***** BEGIN GPL LICENSE BLOCK *****
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # ***** END GPL LICENSE BLOCK *****
22 # Write out messages.txt from blender
25 # blender --background --python po/update_msg.py
29 CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
30 SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.join(CURRENT_DIR, "..")))
32 FILE_NAME_MESSAGES = os.path.join(CURRENT_DIR, "messages.txt")
33 COMMENT_PREFIX = "#~ "
36 def dump_messages_rna(messages):
40 blacklist_rna_class = [
42 "Context", "Event", "Function", "UILayout",
44 # registerable classes
45 "Panel", "Menu", "Header", "RenderEngine",
46 "Operator", "OperatorMacro", "UnknownType"
48 "WindowManager", "Window"
51 # ---------------------------------------------------------------------
52 # Collect internal operators
54 # extend with all internal operators
55 # note that this uses internal api introspection functions
56 # all possible operator names
57 op_names = list(sorted(set(
58 [cls.bl_rna.identifier for cls in
59 bpy.types.OperatorProperties.__subclasses__()] +
60 [cls.bl_rna.identifier for cls in
61 bpy.types.Operator.__subclasses__()] +
62 [cls.bl_rna.identifier for cls in
63 bpy.types.OperatorMacro.__subclasses__()]
66 get_inatance = __import__("_bpy").ops.get_instance
67 path_resolve = type(bpy.context).__base__.path_resolve
68 for idname in op_names:
69 op = get_inatance(idname)
70 if 'INTERNAL' in path_resolve(op, "bl_options"):
71 blacklist_rna_class.append(idname)
73 # ---------------------------------------------------------------------
74 # Collect builtin classes we dont need to doc
75 blacklist_rna_class.append("Property")
76 blacklist_rna_class.extend(
77 [cls.__name__ for cls in
78 bpy.types.Property.__subclasses__()])
80 # ---------------------------------------------------------------------
81 # Collect classes which are attached to collections, these are api
83 collection_props = set()
84 for cls_id in dir(bpy.types):
85 cls = getattr(bpy.types, cls_id)
86 for prop in cls.bl_rna.properties:
87 if prop.type == 'COLLECTION':
89 if prop_cls is not None:
90 collection_props.add(prop_cls.identifier)
91 blacklist_rna_class.extend(sorted(collection_props))
93 return blacklist_rna_class
95 blacklist_rna_class = classBlackList()
97 def filterRNA(bl_rna):
98 id = bl_rna.identifier
99 if id in blacklist_rna_class:
100 print(" skipping", id)
104 # -------------------------------------------------------------------------
105 # Function definitions
107 def walkProperties(bl_rna):
110 # get our parents properties not to export them multiple times
111 bl_rna_base = bl_rna.base
113 bl_rna_base_props = bl_rna_base.properties.values()
115 bl_rna_base_props = ()
117 for prop in bl_rna.properties:
118 # only write this property is our parent hasn't got it.
119 if prop in bl_rna_base_props:
121 if prop.identifier == "rna_type":
124 msgsrc = "bpy.types.%s.%s" % (bl_rna.identifier, prop.identifier)
125 messages.setdefault(prop.name, []).append(msgsrc)
126 messages.setdefault(prop.description, []).append(msgsrc)
128 if isinstance(prop, bpy.types.EnumProperty):
129 for item in prop.enum_items:
130 msgsrc = "bpy.types.%s.%s, '%s'" % (bl_rna.identifier,
134 messages.setdefault(item.name, []).append(msgsrc)
135 messages.setdefault(item.description, []).append(msgsrc)
139 if filterRNA(bl_rna):
142 msgsrc = "bpy.types.%s" % bl_rna.identifier
144 if bl_rna.name and bl_rna.name != bl_rna.identifier:
145 messages.setdefault(bl_rna.name, []).append(msgsrc)
147 if bl_rna.description:
148 messages.setdefault(bl_rna.description, []).append(msgsrc)
150 if hasattr(bl_rna, 'bl_label') and bl_rna.bl_label:
151 messages.setdefault(bl_rna.bl_label, []).append(msgsrc)
153 walkProperties(bl_rna)
158 def walk_keymap_hierarchy(hier, msgsrc_prev):
160 msgsrc = "%s.%s" % (msgsrc_prev, lvl[1])
161 messages.setdefault(lvl[0], []).append(msgsrc)
164 walk_keymap_hierarchy(lvl[3], msgsrc)
166 # -------------------------------------------------------------------------
169 def full_class_id(cls):
170 """ gives us 'ID.Lamp.AreaLamp' which is best for sorting.
175 cls_id = "%s.%s" % (bl_rna.identifier, cls_id)
179 cls_list = type(bpy.context).__base__.__subclasses__()
180 cls_list.sort(key=full_class_id)
184 cls_list = bpy.types.Space.__subclasses__()
185 cls_list.sort(key=full_class_id)
189 cls_list = bpy.types.Operator.__subclasses__()
190 cls_list.sort(key=full_class_id)
194 cls_list = bpy.types.OperatorProperties.__subclasses__()
195 cls_list.sort(key=full_class_id)
199 cls_list = bpy.types.Menu.__subclasses__()
200 cls_list.sort(key=full_class_id)
204 from bpy_extras.keyconfig_utils import KM_HIERARCHY
206 walk_keymap_hierarchy(KM_HIERARCHY, "KM_HIERARCHY")
209 def dump_messages_pytext(messages):
210 """ dumps text inlined in the python user interface: eg.
212 layout.prop("someprop", text="My Name")
216 # -------------------------------------------------------------------------
217 # Gather function names
221 # val: [(arg_kw, arg_pos), (arg_kw, arg_pos), ...]
222 func_translate_args = {}
224 # so far only 'text' keywords, but we may want others translated later
225 translate_kw = ("text", )
227 for func_id, func in bpy.types.UILayout.bl_rna.functions.items():
228 # check it has a 'text' argument
229 for (arg_pos, (arg_kw, arg)) in enumerate(func.parameters.items()):
230 if ((arg_kw in translate_kw) and
231 (arg.is_output == False) and
232 (arg.type == 'STRING')):
234 func_translate_args.setdefault(func_id, []).append((arg_kw,
236 # print(func_translate_args)
238 # -------------------------------------------------------------------------
239 # Function definitions
241 def extract_strings(fp_rel, node_container):
242 """ Recursively get strings, needed incase we have "Blah" + "Blah",
243 passed as an argument in that case it wont evaluate to a string.
246 for node in ast.walk(node_container):
247 if type(node) == ast.Str:
248 eval_str = ast.literal_eval(node)
250 # print("%s:%d: %s" % (fp, node.lineno, eval_str))
251 msgsrc = "%s:%s" % (fp_rel, node.lineno)
252 messages.setdefault(eval_str, []).append(msgsrc)
254 def extract_strings_from_file(fp):
255 filedata = open(fp, 'r', encoding="utf8")
256 root_node = ast.parse(filedata.read(), fp, 'exec')
259 fp_rel = os.path.relpath(fp, SOURCE_DIR)
261 for node in ast.walk(root_node):
262 if type(node) == ast.Call:
263 # print("found function at")
264 # print("%s:%d" % (fp, node.lineno))
267 if type(node.func) == ast.Name:
270 # getattr(self, con.type)(context, box, con)
271 if not hasattr(node.func, "attr"):
274 translate_args = func_translate_args.get(node.func.attr, ())
276 # do nothing if not found
277 for arg_kw, arg_pos in translate_args:
278 if arg_pos < len(node.args):
279 extract_strings(fp_rel, node.args[arg_pos])
281 for kw in node.keywords:
283 extract_strings(fp_rel, kw.value)
285 # -------------------------------------------------------------------------
288 mod_dir = os.path.join(SOURCE_DIR,
294 files = [os.path.join(mod_dir, fn)
295 for fn in sorted(os.listdir(mod_dir))
296 if not fn.startswith("_")
301 extract_strings_from_file(fp)
306 def filter_message(msg):
308 # check for strings like ": %d"
310 for ignore in ("%d", "%s", "%r", # string formatting
311 "*", ".", "(", ")", "-", "/", "\\", "+", ":", "#", "%"
312 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
313 "x", # used on its own eg: 100x200
314 "X", "Y", "Z", # used alone. no need to include
316 msg_test = msg_test.replace(ignore, "")
317 msg_test = msg_test.strip()
319 # print("Skipping: '%s'" % msg)
322 # we could filter out different strings here
328 messages = collections.OrderedDict()
334 # get strings from RNA
335 dump_messages_rna(messages)
337 # get strings from UI layout definitions text="..." args
338 dump_messages_pytext(messages)
342 message_file = open(FILE_NAME_MESSAGES, 'w', encoding="utf8")
343 # message_file.writelines("\n".join(sorted(messages)))
345 for key, value in messages.items():
347 # filter out junk values
348 if filter_message(key):
352 message_file.write("%s%s\n" % (COMMENT_PREFIX, msgsrc))
353 message_file.write("%s\n" % key)
357 print("Written %d messages to: %r" % (len(messages), FILE_NAME_MESSAGES))
365 print("This script must run from inside blender")
371 if __name__ == "__main__":
372 print("\n\n *** Running %r *** \n" % __file__)