Fix Print3D Toolbox: fix button alignment
[blender-addons.git] / object_edit_linked.py
blobe880b9fd9bfcb8777f8deeffa1489a9bb273a6b8
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?title=Extensions:2.6/Py/Scripts/Object/Edit_Linked_Library",
28 "tracker_url": "http://projects.blender.org/tracker/index.php?func=detail&aid=29630",
29 "category": "Object"}
32 import bpy
33 from bpy.app.handlers import persistent
34 import os
36 settings = {
37 "original_file": "",
38 "linked_file": "",
39 "linked_objects": [],
43 @persistent
44 def linked_file_check(context):
45 if settings["linked_file"] != "":
46 if os.path.samefile(settings["linked_file"], bpy.data.filepath):
47 print("Editing a linked library.")
48 bpy.ops.object.select_all(action='DESELECT')
49 for ob_name in settings["linked_objects"]:
50 bpy.data.objects[ob_name].select = True # XXX Assumes selected object is in the active scene
51 if len(settings["linked_objects"]) == 1:
52 bpy.context.scene.objects.active = bpy.data.objects[settings["linked_objects"][0]]
53 else:
54 # For some reason, the linked editing session ended
55 # (failed to find a file or opened a different file
56 # before returning to the originating .blend)
57 settings["original_file"] = ""
58 settings["linked_file"] = ""
61 class EditLinked(bpy.types.Operator):
62 """Edit Linked Library"""
63 bl_idname = "object.edit_linked"
64 bl_label = "Edit Linked Library"
66 use_autosave = bpy.props.BoolProperty(
67 name="Autosave",
68 description="Save the current file before opening the linked library",
69 default=True)
70 use_instance = bpy.props.BoolProperty(
71 name="New Blender Instance",
72 description="Open in a new Blender instance",
73 default=False)
75 @classmethod
76 def poll(cls, context):
77 return settings["original_file"] == "" and context.active_object is not None and (
78 (context.active_object.dupli_group and
79 context.active_object.dupli_group.library is not None) or
80 (context.active_object.proxy and
81 context.active_object.proxy.library is not None) or
82 context.active_object.library is not None)
83 #return context.active_object is not None
85 def execute(self, context):
86 #print(bpy.context.active_object.library)
87 target = context.active_object
89 if target.dupli_group and target.dupli_group.library:
90 targetpath = target.dupli_group.library.filepath
91 settings["linked_objects"].extend({ob.name for ob in target.dupli_group.objects})
92 elif target.library:
93 targetpath = target.library.filepath
94 settings["linked_objects"].append(target.name)
95 elif target.proxy:
96 target = target.proxy
97 targetpath = target.library.filepath
98 settings["linked_objects"].append(target.name)
100 if targetpath:
101 print(target.name + " is linked to " + targetpath)
103 if self.use_autosave:
104 bpy.ops.wm.save_mainfile()
106 settings["original_file"] = bpy.data.filepath
107 settings["linked_file"] = bpy.path.abspath(targetpath)
109 if self.use_instance:
110 import subprocess
111 try:
112 subprocess.Popen([bpy.app.binary_path, settings["linked_file"]])
113 except:
114 print("Error on the new Blender instance")
115 import traceback
116 traceback.print_exc()
117 else:
118 bpy.ops.wm.open_mainfile(filepath=settings["linked_file"])
120 print("Opened linked file!")
121 else:
122 self.report({'WARNING'}, target.name + " is not linked")
123 print(target.name + " is not linked")
125 return {'FINISHED'}
128 class ReturnToOriginal(bpy.types.Operator):
129 """Load the original file"""
130 bl_idname = "wm.return_to_original"
131 bl_label = "Return to Original File"
133 use_autosave = bpy.props.BoolProperty(
134 name="Autosave",
135 description="Save the current file before opening original file",
136 default=True)
138 @classmethod
139 def poll(cls, context):
140 return (settings["original_file"] != "")
142 def execute(self, context):
143 if self.use_autosave:
144 bpy.ops.wm.save_mainfile()
146 bpy.ops.wm.open_mainfile(filepath=settings["original_file"])
148 settings["original_file"] = ""
149 settings["linked_objects"] = []
150 print("Back to the original!")
151 return {'FINISHED'}
154 # UI
155 # TODO:Add operators to the File menu?
156 # Hide the entire panel for non-linked objects?
157 class PanelLinkedEdit(bpy.types.Panel):
158 bl_label = "Edit Linked Library"
159 bl_space_type = "VIEW_3D"
160 bl_region_type = "TOOLS"
162 @classmethod
163 def poll(cls, context):
164 return (context.active_object is not None) or (settings["original_file"] != "")
166 def draw(self, context):
167 layout = self.layout
168 scene = context.scene
169 icon = "OUTLINER_DATA_" + context.active_object.type
171 target = None
173 if context.active_object.proxy:
174 target = context.active_object.proxy
175 else:
176 target = context.active_object.dupli_group
178 if settings["original_file"] == "" and (
179 (target and
180 target.library is not None) or
181 context.active_object.library is not None):
183 if (target is not None):
184 props = layout.operator("object.edit_linked", icon="LINK_BLEND",
185 text="Edit Library: %s" % target.name)
186 else:
187 props = layout.operator("object.edit_linked", icon="LINK_BLEND",
188 text="Edit Library: %s" % context.active_object.name)
189 props.use_autosave = scene.use_autosave
190 props.use_instance = scene.use_instance
192 layout.prop(scene, "use_autosave")
193 layout.prop(scene, "use_instance")
195 if (target is not None):
196 layout.label(text="Path: %s" %
197 target.library.filepath)
198 else:
199 layout.label(text="Path: %s" %
200 context.active_object.library.filepath)
202 elif settings["original_file"] != "":
204 if scene.use_instance:
205 layout.operator("wm.return_to_original",
206 text="Reload Current File",
207 icon="FILE_REFRESH").use_autosave = False
209 layout.separator()
211 #XXX - This is for nested linked assets... but it only works
212 # when launching a new Blender instance. Nested links don't
213 # currently work when using a single instance of Blender.
214 props = layout.operator("object.edit_linked",
215 text="Edit Library: %s" % context.active_object.dupli_group.name,
216 icon="LINK_BLEND")
217 props.use_autosave = scene.use_autosave
218 props.use_instance = scene.use_instance
219 layout.prop(scene, "use_autosave")
220 layout.prop(scene, "use_instance")
222 layout.label(text="Path: %s" %
223 context.active_object.dupli_group.library.filepath)
225 else:
226 props = layout.operator("wm.return_to_original", icon="LOOP_BACK")
227 props.use_autosave = scene.use_autosave
229 layout.prop(scene, "use_autosave")
231 else:
232 layout.label(text="%s is not linked" % context.active_object.name,
233 icon=icon)
236 addon_keymaps = []
239 def register():
240 bpy.app.handlers.load_post.append(linked_file_check)
241 bpy.utils.register_class(EditLinked)
242 bpy.utils.register_class(ReturnToOriginal)
243 bpy.utils.register_class(PanelLinkedEdit)
245 # Is there a better place to store this properties?
246 bpy.types.Scene.use_autosave = bpy.props.BoolProperty(
247 name="Autosave",
248 description="Save the current file before opening a linked file",
249 default=True)
250 bpy.types.Scene.use_instance = bpy.props.BoolProperty(
251 name="New Blender Instance",
252 description="Open in a new Blender instance",
253 default=False)
255 # Keymapping (deactivated by default; activated when a library object is selected)
256 kc = bpy.context.window_manager.keyconfigs.addon
257 km = kc.keymaps.new(name="3D View", space_type='VIEW_3D')
258 kmi = km.keymap_items.new("object.edit_linked", 'NUMPAD_SLASH', 'PRESS', shift=True)
259 kmi.active = True
260 addon_keymaps.append((km, kmi))
261 kmi = km.keymap_items.new("wm.return_to_original", 'NUMPAD_SLASH', 'PRESS', shift=True)
262 kmi.active = True
263 addon_keymaps.append((km, kmi))
266 def unregister():
267 bpy.utils.unregister_class(EditLinked)
268 bpy.utils.unregister_class(ReturnToOriginal)
269 bpy.utils.unregister_class(PanelLinkedEdit)
270 bpy.app.handlers.load_post.remove(linked_file_check)
272 del bpy.types.Scene.use_autosave
273 del bpy.types.Scene.use_instance
275 # handle the keymap
276 for km, kmi in addon_keymaps:
277 km.keymap_items.remove(kmi)
278 addon_keymaps.clear()
281 if __name__ == "__main__":
282 register()