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

textplugin_membersuggest.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c0de78b704dd7363cd434b8aace9f7f9adfb203 (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
#!BPY
"""
Name: 'Member Suggest | .'
Blender: 246
Group: 'TextPlugin'
Shortcut: 'Period'
Tooltip: 'Lists members of the object preceding the cursor in the current text space'
"""

# Only run if we have the required modules
try:
	import bpy
	from BPyTextPlugin import *
except ImportError:
	OK = False
else:
	OK = True

def main():
	txt = bpy.data.texts.active
	if not txt:
		return
	
	(line, c) = current_line(txt)
	
	# Check we are in a normal context
	if get_context(txt) != CTX_NORMAL:
		return
	
	targets = get_targets(line, c)
	
	if targets[0] == '': # Check if we are looking at a constant [] {} '' etc.
		i = c - len('.'.join(targets)) - 1
		if i >= 0:
			if line[i] == '"' or line[i] == "'":
				targets[0] = 'str'
			elif line[i] == '}':
				targets[0] = 'dict'
			elif line[i] == ']': # Could be array elem x[y] or list [y]
				i = line.rfind('[', 0, i) - 1
				while i >= 0:
					if line[i].isalnum() or line[i] == '_':
						break
					elif line[i] != ' ' and line[i] != '\t':
						i = -1
						break
					i -= 1
				if i < 0: 
					targets[0] = 'list'
	
	obj = resolve_targets(txt, targets[:-1])
	if not obj:
		return
	
	items = []
	
	if isinstance(obj, VarDesc):
		obj = obj.type
		
	if isinstance(obj, Definition): # Locally defined
		if hasattr(obj, 'classes'):
			items.extend([(s, 'f') for s in obj.classes.keys()])
		if hasattr(obj, 'defs'):
			items.extend([(s, 'f') for s in obj.defs.keys()])
		if hasattr(obj, 'vars'):
			items.extend([(s, 'v') for s in obj.vars.keys()])
	
	else: # Otherwise we have an imported or builtin object
		try:
			attr = obj.__dict__.keys()
		except AttributeError:
			attr = dir(obj)
		else:
			if not attr: attr = dir(obj)
		
		for k in attr:
			try:
				v = getattr(obj, k)
			except (AttributeError, TypeError): # Some attributes are not readable
				pass
			else:
				items.append((k, type_char(v)))
	
	if items != []:
		items.sort(cmp = suggest_cmp)
		txt.suggest(items, targets[-1])

# Check we are running as a script and not imported as a module
if __name__ == "__main__" and OK:
	main()