Welcome to mirror list, hosted at ThFree Co, Russian Federation.

epy_doc_gen.py « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9d4c25dd5135d145704b9c27c1ad26a89a83ae2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
 # ***** BEGIN GPL LICENSE BLOCK *****
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
 # as published by the Free Software Foundation; either version 2
 # of the License, or (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software Foundation,
 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #
 # Contributor(s): Campbell Barton
 #
 # #**** END GPL LICENSE BLOCK #****

# Usage,
# run this script from blenders root path once you have compiled blender
# ./blender.bin -b -P source/blender/python/epy_doc_gen.py
# 
# This will generate rna.py, generate html docs  by running...
# epydoc source/blender/python/doc/rna.py -o source/blender/python/doc/html -v --no-sourcecode --name="RNA API" --url="http://brechtvanlommelfanclub.com" --graph=classtree
# 
# if you dont have graphvis installed ommit the --graph arg.

def range_str(val):
	if val < -10000000:	return '-inf'
	if val >  10000000:	return 'inf'
	if type(val)==float:
		return '%g'  % val
	else:
		return str(val)	

def get_array_str(length):
	if length > 0:	return ' array of %d items' % length
	else:		return ''

def full_rna_struct_path(rna_struct):
	'''
	Needed when referencing one struct from another
	'''
	nested = rna_struct.nested
	if nested:
		return "%s.%s" % (full_rna_struct_path(nested), rna_struct.identifier)
	else:
		return rna_struct.identifier


def rna2epy(target_path):
	
	# Use for faster lookups
	# use rna_struct.identifier as the key for each dict
	rna_full_path_dict =	{}	# store the result of full_rna_struct_path(rna_struct)
	rna_children_dict =		{}	# store all rna_structs nested from here
	rna_references_dict =	{}	# store a list of rna path strings that reference this type
	
	def write_struct(rna_struct, ident):
		identifier = rna_struct.identifier
		
		rna_base = rna_struct.base
		
		if rna_base:
			out.write(ident+ 'class %s(%s):\n' % (identifier, rna_base.identifier))
		else:
			out.write(ident+ 'class %s:\n' % identifier)
		
		out.write(ident+ '\t"""\n')
		
		title = 'The %s Object' % rna_struct.name
		
		out.write(ident+ '\t%s\n' %  title)
		out.write(ident+ '\t%s\n' %  ('=' * len(title)))
		out.write(ident+ '\t\t%s\n' %  rna_struct.description)
		
		
		
		# For convenience, give a list of all places were used.
		rna_refs= rna_references_dict[identifier]
		
		if rna_refs:
			out.write(ident+ '\t\t\n')
			out.write(ident+ '\t\tReferences\n')
			out.write(ident+ '\t\t==========\n')
			
			for rna_ref_string in rna_refs:
				out.write(ident+ '\t\t\t- L{%s}\n' % rna_ref_string)
			
			out.write(ident+ '\t\t\n')
		
		else:
			out.write(ident+ '\t\t\n')
			out.write(ident+ '\t\t(no references to this struct found)\n')
			out.write(ident+ '\t\t\n')
		
		for rna_prop_identifier, rna_prop in rna_struct.properties.items():
			
			if rna_prop_identifier=='RNA':
				continue
			
			if rna_prop_identifier=='rna_type':
				continue
			
			rna_desc = rna_prop.description
			if not rna_desc: rna_desc = rna_prop.name
			if not rna_desc: rna_desc = 'Note - No documentation for this property!'
			
			rna_prop_type = rna_prop.type.lower()
			
			if rna_prop_type=='collection':	collection_str = 'Collection of '
			else:							collection_str = ''
			
			try:		rna_prop_ptr = rna_prop.fixed_type
			except:	rna_prop_ptr = None
			
			try:		length = rna_prop.array_length
			except:	length = 0
			
			array_str = get_array_str(length)
			
			if rna_prop.editable:	readonly_str = ''
			else:				readonly_str = ' (readonly)'
			
			if rna_prop_ptr: # Use the pointer type
				out.write(ident+ '\t@ivar %s: %s\n' %  (rna_prop_identifier, rna_desc))
				out.write(ident+ '\t@type %s: %sL{%s}%s%s\n' %  (rna_prop_identifier, collection_str, rna_prop_ptr.identifier, array_str, readonly_str))
			else:
				if rna_prop_type == 'enum':
					out.write(ident+ '\t@ivar %s: %s in (%s)\n' %  (rna_prop_identifier, rna_desc, ', '.join(rna_prop.items.keys())))
					out.write(ident+ '\t@type %s: %s%s%s\n' %  (rna_prop_identifier, rna_prop_type,  array_str, readonly_str))
				elif rna_prop_type == 'int' or rna_prop_type == 'float':
					out.write(ident+ '\t@ivar %s: %s\n' %  (rna_prop_identifier, rna_desc))
					out.write(ident+ '\t@type %s: %s%s%s in [%s, %s]\n' %  (rna_prop_identifier, rna_prop_type, array_str, readonly_str, range_str(rna_prop.hard_min), range_str(rna_prop.hard_max) ))
				elif rna_prop_type == 'string':
					out.write(ident+ '\t@ivar %s: %s (maximum length of %s)\n' %  (rna_prop_identifier, rna_desc, rna_prop.max_length))
					out.write(ident+ '\t@type %s: %s%s%s\n' %  (rna_prop_identifier, rna_prop_type, array_str, readonly_str))
				else:
					out.write(ident+ '\t@ivar %s: %s\n' %  (rna_prop_identifier, rna_desc))
					out.write(ident+ '\t@type %s: %s%s%s\n' %  (rna_prop_identifier, rna_prop_type, array_str, readonly_str))
				
			
		out.write(ident+ '\t"""\n\n')
		
		# Now write children recursively
		for child in rna_children_dict[identifier]:
			write_struct(child, ident + '\t')
	
	out = open(target_path, 'w')

	def base_id(rna_struct):
		try:		return rna_struct.base.identifier
		except:	return '' # invalid id

	#structs = [(base_id(rna_struct), rna_struct.identifier, rna_struct) for rna_struct in bpy.doc.structs.values()]
	'''
	structs = []
	for rna_struct in bpy.doc.structs.values():
		structs.append( (base_id(rna_struct), rna_struct.identifier, rna_struct) )
	'''
	structs = []
	for rna_type_name in dir(bpy.types):
		rna_type = getattr(bpy.types, rna_type_name)
		if hasattr(rna_type, '__rna__'):
			#if not rna_type_name.startswith('__'):
			rna_struct = rna_type.__rna__
			identifier = rna_struct.identifier
			structs.append( (base_id(rna_struct), identifier, rna_struct) )	
			
			
			
			# Store full rna path 'GameObjectSettings' -> 'Object.GameObjectSettings'
			rna_full_path_dict[identifier] = full_rna_struct_path(rna_struct)
			
			# fill in these later
			rna_children_dict[identifier]= []
			rna_references_dict[identifier]= []
			
		else:
			print("Ignoring", rna_type_name)
	
	
	
	structs.sort() # not needed but speeds up sort below, setting items without an inheritance first
	
	# Arrange so classes are always defined in the correct order
	deps_ok = False
	while deps_ok == False:
		deps_ok = True
		rna_done = set()
		
		for i, (rna_base, identifier, rna_struct) in enumerate(structs):
			
			rna_done.add(identifier)
			
			if rna_base and rna_base not in rna_done:
				deps_ok = False
				data = structs.pop(i)
				ok = False
				while i < len(structs):
					if structs[i][1]==rna_base:
						structs.insert(i+1, data) # insert after the item we depend on.
						ok = True
						break
					i+=1
					
				if not ok:
					print('Dependancy "%s" could not be found for "%s"' % (identifier, rna_base))
				
				break
	
	# Done ordering structs
	
	
	# precalc vars to avoid a lot of looping
	for (rna_base, identifier, rna_struct) in structs:
		
		
		# rna_struct_path = full_rna_struct_path(rna_struct)
		rna_struct_path = rna_full_path_dict[identifier]
		
		for rna_prop_identifier, rna_prop in rna_struct.properties.items():
			if rna_prop_identifier=='RNA':
				continue
			
			if rna_prop_identifier=='rna_type':
				continue
			
			try:		rna_prop_ptr = rna_prop.fixed_type
			except:	rna_prop_ptr = None
			
			# Does this property point to me?
			if rna_prop_ptr:
				rna_references_dict[rna_prop_ptr.identifier].append( "%s.%s" % (rna_struct_path, rna_prop_identifier) )
		
		
		
		# Store nested children
		nested = rna_struct.nested
		if nested:
			rna_children_dict[nested.identifier].append(rna_struct)
	
	# Sort the refs, just reads nicer
	for rna_refs in rna_references_dict.values():
		rna_refs.sort()
	
	for (rna_base, identifier, rna_struct) in structs:
		if rna_struct.nested:
			continue
		
		write_struct(rna_struct, '')
		
		
	out.write('\n')
	out.close()
	
	# # We could also just run....
	# os.system('epydoc source/blender/python/doc/rna.py -o ./source/blender/python/doc/html -v')
	
	
	# Write graphviz
	out= open(target_path.replace('.py', '.dot'), 'w')
	out.write('digraph "rna data api" {\n')
	out.write('\tnode [style=filled, shape = "box"];\n')
	out.write('\toverlap=false;\n')
	out.write('\trankdir = LR;\n')
	out.write('\tsplines=true;\n')
	out.write('\tratio=auto;\n')
	
	
	
	out.write('\tsize="10,10"\n')
	#out.write('\tpage="8.5,11"\n')
	#out.write('\tcenter=""\n')
	
	def isop(rna_struct):
		return '_OT_' in rna_struct.identifier
	
	
	for (rna_base, identifier, rna_struct) in structs:
		if isop(rna_struct):
			continue
		
		base = rna_struct.base
		
		
		out.write('\t"%s";\n' % identifier)
	
	for (rna_base, identifier, rna_struct) in structs:
		
		if isop(rna_struct):
			continue
			
		base = rna_struct.base
		
		if base and not isop(base):
			out.write('\t"%s" -> "%s" [label="(base)" weight=1.0];\n' % (base.identifier, identifier))
		
		nested = rna_struct.nested
		if nested and not isop(nested):
			out.write('\t"%s" -> "%s" [label="(nested)"  weight=1.0];\n' % (nested.identifier, identifier))
		
		
		
		rna_refs= rna_references_dict[identifier]
		
		for rna_ref_string in rna_refs:
			
			if '_OT_' in rna_ref_string:
				continue
			
			ref = rna_ref_string.split('.')[-2]
			out.write('\t"%s" -> "%s" [label="%s" weight=0.01];\n' % (ref, identifier, rna_ref_string))
		
		
	
	out.write('}\n')
	out.close()
	
	# # We could also just run....
	# os.system('dot source/blender/python/doc/rna.dot -Tsvg -o ./source/blender/python/doc/rna.svg')
	

def op2epy(target_path):
	out = open(target_path, 'w')
	
	operators = dir(bpy.ops)
	operators.remove('add')
	operators.remove('remove')
	operators.sort()
	
	for op in operators:
		
		if op.startswith('__'):
			continue
		
		# Keyword attributes
		kw_args = [] # "foo = 1", "bar=0.5", "spam='ENUM'"
		kw_arg_attrs = [] # "@type mode: int"
		
		# rna = getattr(bpy.types, op).__rna__
		rna = bpy.ops.__rna__(op)
		
		rna_struct = rna.rna_type
		# print (dir(rna))
		# print (dir(rna_struct))
		for rna_prop_identifier, rna_prop in rna_struct.properties.items():
			if rna_prop_identifier=='rna_type':
				continue
			# ['rna_type', 'name', 'array_length', 'description', 'hard_max', 'hard_min', 'identifier', 'precision', 'readonly', 'soft_max', 'soft_min', 'step', 'subtype', 'type']
			#rna_prop=  op_rna.rna_type.properties[attr]
			rna_prop_type = rna_prop.type.lower() # enum, float, int, boolean
			
			try:		length = rna_prop.array_length
			except:	length = 0
			
			array_str = get_array_str(length)
			
			try:
				val = getattr(rna, rna_prop_identifier)
				val_error = False
			except:
				val = "'<UNDEFINED>'"
				val_error = True
			
			kw_type_str= "@type %s: %s%s" % (rna_prop_identifier, rna_prop_type, array_str)
			kw_param_str= "@param %s: %s" % (rna_prop_identifier, rna_prop.description)
			kw_param_set = False
			
			if val_error:
				val_str = val
			elif rna_prop_type=='float':
				if length==0:
					val_str= '%g' % val
					if '.' not in val_str:
						val_str += '.0'
				else:
					# array
					val_str = str(tuple(val))
				
				kw_param_str += (' in (%s, %s)' % (range_str(rna_prop.hard_min), range_str(rna_prop.hard_max)))
				kw_param_set= True
				
			elif rna_prop_type=='int':
				if length==0:
					val_str='%d' % val
				else:
					val_str = str(tuple(val))
				
				# print(dir(rna_prop))
				kw_param_str += (' in (%s, %s)' % (range_str(rna_prop.hard_min), range_str(rna_prop.hard_max)))
				# These strings dont have a max length???
				#kw_param_str += ' (maximum length of %s)' %  (rna_prop.max_length)
				kw_param_set= True
				
			elif rna_prop_type=='boolean':
				if length==0:
					if val:	val_str='True'
					else:	val_str='False'
				else:
					val_str = str(tuple(val))
			
			elif rna_prop_type=='enum':
				# no array here?
				val_str="'%s'" % val
				kw_param_str += (' in (%s)' % ', '.join(rna_prop.items.keys()))
				kw_param_set= True
				
			elif rna_prop_type=='string':
				# no array here?
				val_str='"%s"' % val
			
			# todo - collection - array
			# print (rna_prop.type)
			
			kw_args.append('%s = %s' % (rna_prop_identifier, val_str))
			
			# stora 
			
			kw_arg_attrs.append(kw_type_str)
			if kw_param_set:
				kw_arg_attrs.append(kw_param_str)
			
		
		
		out.write('def %s(%s):\n' % (op, ', '.join(kw_args)))
		out.write('\t"""\n')
		out.write('\t%s\n' % rna_struct.description)
		for desc in kw_arg_attrs:
			out.write('\t%s\n' % desc)
		out.write('\t@rtype: None\n')
		out.write('\t"""\n')
	
	
	out.write('\n')
	out.close()

if __name__ == '__main__':
	rna2epy('source/blender/python/doc/rna.py')
	op2epy('source/blender/python/doc/bpyoperator.py')