-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlink_utils.py
More file actions
199 lines (155 loc) · 6.07 KB
/
link_utils.py
File metadata and controls
199 lines (155 loc) · 6.07 KB
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
# ***** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****
bl_info = {
"name": "Linked Library Utilities",
"author": "Lucio Rossi",
"version": (0, 0, 0),
"blender": (2, 74, 0),
"location": "View3D > Toolshelf > Linked Library Utilities",
"description": "Lists objects linked from a .blend library.",
"wiki_url": "",
"category": "Object",
}
import bpy
# UI
# Hide the entire panel for non-linked objects?
def get_linked_dict():
linked_dict = {}
linked_list = []
for ob in bpy.context.scene.objects:
if ob.dupli_group and ob.dupli_group.library:
obpath = ob.dupli_group.library.filepath
linked_list.append(ob.dupli_group.name)
linked_dict[ob.dupli_group.name] = [obpath]
if ob.library:
obpath = ob.library.filepath
linked_list.append(ob.name)
linked_dict[ob.name] = [obpath]
elif ob.proxy:
ob = ob.proxy
obpath = ob.library.filepath
linked_list.append(ob.name)
linked_dict[ob.name] = [obpath]
for key in linked_dict.keys():
linked_dict[key].append(linked_list.count(key))
return linked_dict
def dict_to_txt(d,fname,headers):
text = open(fname, 'w')
for h in headers:
text.write(h + ',')
text.write('\n')
keys = list(d.keys())
keys.sort()
for key in keys:
text.write(key + ',')
for i in range(len(d[key])):
text.write(str(d[key][i]) + ',')
text.write('\n')
def dict_to_csv(d,fname,headers):
import csv
with open(fname, 'w') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(headers)
keys = list(d.keys())
keys.sort()
for key in keys:
row = [key]
row.extend(d[key])
writer.writerow(row)
def dict_to_xlsx(d,fname,headers):
import xlsxwriter
xlsx_file = xlsxwriter.Workbook(fname)
xlsx_worksheet = xlsx_file.add_worksheet()
bold = xlsx_file.add_format({'bold': True})
for i,h in enumerate(headers):
xlsx_worksheet.write(0,i,h,bold)
keys = list(d.keys())
keys.sort()
for i, key in enumerate(keys):
xlsx_worksheet.write(i+1,0,key)
for j in range(len(d[key])):
xlsx_worksheet.write(i+1,j+1,d[key][j])
class SaveLinkedList(bpy.types.Operator):
"""Save Linked List"""
bl_idname = "object.save_linked"
bl_label = "Save Linked List"
def execute(self, context):
scene = context.scene
path = bpy.path.abspath(scene.link_utils_path)
fname = bpy.path.basename(bpy.context.blend_data.filepath)
fname = fname[:-6] # cut off extension
linked_dict = get_linked_dict()
headers = ['Obj name','Path','num']
if scene.link_utils_file_ext == '0':
fname = fname + '_linked_list.txt'
dict_to_txt(linked_dict,path + fname, headers)
elif scene.link_utils_file_ext == '1':
fname = fname + '_linked_list.csv'
dict_to_csv(linked_dict,path + fname, headers)
else:
fname = fname + '_linked_list.xlsx'
dict_to_xlsx(linked_dict,path + fname, headers)
return {'FINISHED'}
class PanelLinkedUtils(bpy.types.Panel):
bl_label = "Linked Library Utilities"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_category = "Relations"
def draw(self, context):
layout = self.layout
scene = context.scene
layout.prop(scene,'link_utils_path')
layout.label('File extension:')
layout.prop(scene,'link_utils_file_ext',expand=True)
layout.operator('object.save_linked', icon='LINK_BLEND', text='Save Linked List')
layout.prop(scene,'show_linked_list')
box = layout.box()
if scene.show_linked_list:
row = box.row()
row.label(text='Obj Name')
row.label(text='[Path, num]')
linked_dict = get_linked_dict()
keys = list(linked_dict.keys())
keys.sort()
for key in keys:
row = box.row()
row.label(text=key + ':')
row.label(text=str(linked_dict[key]))
def register():
bpy.utils.register_class(PanelLinkedUtils)
bpy.utils.register_class(SaveLinkedList)
bpy.types.Scene.link_utils_path = bpy.props.StringProperty(
name = "File Path",
default = '//',
description = "Define the path where the linked list file will be saved",
subtype = 'DIR_PATH'
)
bpy.types.Scene.show_linked_list = bpy.props.BoolProperty(
name = "Show Linked List",
description = "Shows a list of linked objects"
)
bpy.types.Scene.link_utils_file_ext = bpy.props.EnumProperty(name='File ext',items=(('0','.txt',''),('1','.csv',''),('2','.xlsx','')))
def unregister():
bpy.utils.unregister_class(PanelLinkedUtils)
bpy.utils.unregister_class(SaveLinkedList)
del bpy.types.Scene.link_utils_path
del bpy.types.Scene.show_linked_list
del bpy.types.Scene.link_utils_file_ext
if __name__ == "__main__":
register()