2b261703e1979254bfbea0e9e2806517d55dd861
[blender.git] / release / scripts / textplugin_membersuggest.py
1 #!BPY
2 """
3 Name: 'Member Suggest | .'
4 Blender: 246
5 Group: 'TextPlugin'
6 Shortcut: 'Period'
7 Tooltip: 'Lists members of the object preceding the cursor in the current text space'
8 """
9
10 # Only run if we have the required modules
11 try:
12         import bpy
13         from BPyTextPlugin import *
14 except ImportError:
15         OK = False
16 else:
17         OK = True
18
19 def main():
20         txt = bpy.data.texts.active
21         if not txt:
22                 return
23         
24         (line, c) = current_line(txt)
25         
26         # Check we are in a normal context
27         if get_context(txt) != CTX_NORMAL:
28                 return
29         
30         pre = get_targets(line, c)
31         
32         if len(pre) <= 1:
33                 return
34         
35         imports = get_imports(txt)
36         builtins = get_builtins()
37         
38         # Identify the root (root.sub.sub.)
39         obj = None
40         if pre[0] == '':
41                 i = c - len('.'.join(pre)) - 1
42                 if i >= 0:
43                         if line[i] == '"' or line[i] == "'":
44                                 obj = str
45                         elif line[i] == '}':
46                                 obj = dict
47                         elif line[i] == ']': # Could be array elem x[y] or list [y]
48                                 i = line.rfind('[', 0, i) - 1
49                                 while i >= 0:
50                                         if line[i].isalnum() or line[i] == '_':
51                                                 break
52                                         elif line[i] != ' ' and line[i] != '\t':
53                                                 i = -1
54                                                 break
55                                         i -= 1
56                                 if i < 0: 
57                                         obj = list
58         elif imports.has_key(pre[0]):
59                 obj = imports[pre[0]]
60         elif builtins.has_key(pre[0]):
61                 obj = builtins[pre[0]]
62         else:
63                 desc = get_cached_descriptor(txt)
64                 if desc.vars.has_key(pre[0]):
65                         obj = desc.vars[pre[0]].type
66         
67         if not obj:
68                 return
69         
70         # Step through sub-attributes
71         try:
72                 for name in pre[1:-1]:
73                         obj = getattr(obj, name)
74         except AttributeError:
75                 print "Attribute not found '%s' in '%s'" % (name, '.'.join(pre))
76                 return
77         
78         try:
79                 attr = obj.__dict__.keys()
80         except AttributeError:
81                 attr = dir(obj)
82         else:
83                 if not attr:
84                         attr = dir(obj)
85         
86         items = []
87         for k in attr:
88                 try:
89                         v = getattr(obj, k)
90                 except (AttributeError, TypeError): # Some attributes are not readable
91                         pass
92                 else:
93                         items.append((k, type_char(v)))
94         
95         if items != []:
96                 items.sort(cmp = suggest_cmp)
97                 txt.suggest(items, pre[-1])
98
99 # Check we are running as a script and not imported as a module
100 if __name__ == "__main__" and OK:
101         main()