Patch T41093: Cleanup non-manifold
[blender-addons.git] / object_edit_linked.py
blob52b5200986de92aee25128f5d3e0ad60102debe1
1 # ***** 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 LICENCE BLOCK *****
20 bl_info = {
21 "name": "Edit Linked Library",
22 "author": "Jason van Gumster (Fweeb), Bassam Kurdali, Pablo Vazquez",
23 "version": (0, 8, 0),
24 "blender": (2, 65, 0),
25 "location": "View3D > Toolshelf > Edit Linked Library",
26 "description": "Allows editing of objects linked from a .blend library.",
27 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
28 "Scripts/Object/Edit_Linked_Library",
29 "category": "Object",
33 import bpy
34 from bpy.app.handlers import persistent
35 import os
37 settings = {
38 "original_file": "",
39 "linked_file": "",
40 "linked_objects": [],
44 @persistent
45 def linked_file_check(context):
46 if settings["linked_file"] != "":
47 if os.path.samefile(settings["linked_file"], bpy.data.filepath):
48 print("Editing a linked library.")
49 bpy.ops.object.select_all(action='DESELECT')
50 for ob_name in settings["linked_objects"]:
51 bpy.data.objects[ob_name].select = True # XXX Assumes selected object is in the active scene
52 if len(settings["linked_objects"]) == 1:
53 bpy.context.scene.objects.active = bpy.data.objects[settings["linked_objects"][0]]
54 else:
55 # For some reason, the linked editing session ended
56 # (failed to find a file or opened a different file
57 # before returning to the originating .blend)
58 settings["original_file"] = ""
59 settings["linked_file"] = ""
62 class EditLinked(bpy.types.Operator):
63 """Edit Linked Library"""
64 bl_idname = "object.edit_linked"
65 bl_label = "Edit Linked Library"
67 use_autosave = bpy.props.BoolProperty(
68 name="Autosave",
69 description="Save the current file before opening the linked library",
70 default=True)
71 use_instance = bpy.props.BoolProperty(
72 name="New Blender Instance",
73 description="Open in a new Blender instance",
74 default=False)
76 @classmethod
77 def poll(cls, context):
78 return settings["original_file"] == "" and context.active_object is not None and (
79 (context.active_object.dupli_group and
80 context.active_object.dupli_group.library is not None) or
81 (context.active_object.proxy and
82 context.active_object.proxy.library is not None) or
83 context.active_object.library is not None)
84 #return context.active_object is not None
86 def execute(self, context):
87 #print(bpy.context.active_object.library)
88 target = context.active_object
90 if target.dupli_group and target.dupli_group.library:
91 targetpath = target.dupli_group.library.filepath
92 settings["linked_objects"].extend({ob.name for ob in target.dupli_group.objects})
93 elif target.library:
94 targetpath = target.library.filepath
95 settings["linked_objects"].append(target.name)
96 elif target.proxy:
97 target = target.proxy
98 targetpath = target.library.filepath
99 settings["linked_objects"].append(target.name)
101 if targetpath:
102 print(target.name + " is linked to " + targetpath)
104 if self.use_autosave:
105 bpy.ops.wm.save_mainfile()
107 settings["original_file"] = bpy.data.filepath
108 settings["linked_file"] = bpy.path.abspath(targetpath)
110 if self.use_instance:
111 import subprocess
112 try:
113 subprocess.Popen([bpy.app.binary_path, settings["linked_file"]])
114 except:
115 print("Error on the new Blender instance")
116 import traceback
117 traceback.print_exc()
118 else:
119 bpy.ops.wm.open_mainfile(filepath=settings["linked_file"])
121 print("Opened linked file!")
122 else:
123 self.report({'WARNING'}, target.name + " is not linked")
124 print(target.name + " is not linked")
126 return {'FINISHED'}
129 class ReturnToOriginal(bpy.types.Operator):
130 """Load the original file"""
131 bl_idname = "wm.return_to_original"
132 bl_label = "Return to Original File"
134 use_autosave = bpy.props.BoolProperty(
135 name="Autosave",
136 description="Save the current file before opening original file",
137 default=True)
139 @classmethod
140 def poll(cls, context):
141 return (settings["original_file"] != "")
143 def execute(self, context):
144 if self.use_autosave:
145 bpy.ops.wm.save_mainfile()
147 bpy.ops.wm.open_mainfile(filepath=settings["original_file"])
149 settings["original_file"] = ""
150 settings["linked_objects"] = []
151 print("Back to the original!")
152 return {'FINISHED'}
155 # UI
156 # TODO:Add operators to the File menu?
157 # Hide the entire panel for non-linked objects?
158 class PanelLinkedEdit(bpy.types.Panel):
159 bl_label = "Edit Linked Library"
160 bl_space_type = "VIEW_3D"
161 bl_region_type = "TOOLS"
162 bl_category = "Relations"
164 @classmethod
165 def poll(cls, context):
166 return (context.active_object is not None) or (settings["original_file"] != "")
168 def draw(self, context):
169 layout = self.layout
170 scene = context.scene
171 icon = "OUTLINER_DATA_" + context.active_object.type
173 target = None
175 if context.active_object.proxy:
176 target = context.active_object.proxy
177 else:
178 target = context.active_object.dupli_group
180 if settings["original_file"] == "" and (
181 (target and
182 target.library is not None) or
183 context.active_object.library is not None):
185 if (target is not None):
186 props = layout.operator("object.edit_linked", icon="LINK_BLEND",
187 text="Edit Library: %s" % target.name)
188 else:
189 props = layout.operator("object.edit_linked", icon="LINK_BLEND",
190 text="Edit Library: %s" % context.active_object.name)
191 props.use_autosave = scene.use_autosave
192 props.use_instance = scene.use_instance
194 layout.prop(scene, "use_autosave")
195 layout.prop(scene, "use_instance")
197 if (target is not None):
198 layout.label(text="Path: %s" %
199 target.library.filepath)
200 else:
201 layout.label(text="Path: %s" %
202 context.active_object.library.filepath)
204 elif settings["original_file"] != "":
206 if scene.use_instance:
207 layout.operator("wm.return_to_original",
208 text="Reload Current File",
209 icon="FILE_REFRESH").use_autosave = False
211 layout.separator()
213 #XXX - This is for nested linked assets... but it only works
214 # when launching a new Blender instance. Nested links don't
215 # currently work when using a single instance of Blender.
216 props = layout.operator("object.edit_linked",
217 text="Edit Library: %s" % context.active_object.dupli_group.name,
218 icon="LINK_BLEND")
219 props.use_autosave = scene.use_autosave
220 props.use_instance = scene.use_instance
221 layout.prop(scene, "use_autosave")
222 layout.prop(scene, "use_instance")
224 layout.label(text="Path: %s" %
225 context.active_object.dupli_group.library.filepath)
227 else:
228 props = layout.operator("wm.return_to_original", icon="LOOP_BACK")
229 props.use_autosave = scene.use_autosave
231 layout.prop(scene, "use_autosave")
233 else:
234 layout.label(text="%s is not linked" % context.active_object.name,
235 icon=icon)
238 addon_keymaps = []
241 def register():
242 bpy.app.handlers.load_post.append(linked_file_check)
243 bpy.utils.register_class(EditLinked)
244 bpy.utils.register_class(ReturnToOriginal)
245 bpy.utils.register_class(PanelLinkedEdit)
247 # Is there a better place to store this properties?
248 bpy.types.Scene.use_autosave = bpy.props.BoolProperty(
249 name="Autosave",
250 description="Save the current file before opening a linked file",
251 default=True)
252 bpy.types.Scene.use_instance = bpy.props.BoolProperty(
253 name="New Blender Instance",
254 description="Open in a new Blender instance",
255 default=False)
257 # Keymapping (deactivated by default; activated when a library object is selected)
258 kc = bpy.context.window_manager.keyconfigs.addon
259 km = kc.keymaps.new(name="3D View", space_type='VIEW_3D')
260 kmi = km.keymap_items.new("object.edit_linked", 'NUMPAD_SLASH', 'PRESS', shift=True)
261 kmi.active = True
262 addon_keymaps.append((km, kmi))
263 kmi = km.keymap_items.new("wm.return_to_original", 'NUMPAD_SLASH', 'PRESS', shift=True)
264 kmi.active = True
265 addon_keymaps.append((km, kmi))
268 def unregister():
269 bpy.utils.unregister_class(EditLinked)
270 bpy.utils.unregister_class(ReturnToOriginal)
271 bpy.utils.unregister_class(PanelLinkedEdit)
272 bpy.app.handlers.load_post.remove(linked_file_check)
274 del bpy.types.Scene.use_autosave
275 del bpy.types.Scene.use_instance
277 # handle the keymap
278 for km, kmi in addon_keymaps:
279 km.keymap_items.remove(kmi)
280 addon_keymaps.clear()
283 if __name__ == "__main__":
284 register()