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 *****
21 "name": "Edit Linked Library",
22 "author": "Jason van Gumster (Fweeb), Bassam Kurdali, Pablo Vazquez",
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",
34 from bpy
.app
.handlers
import 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]]
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(
69 description
="Save the current file before opening the linked library",
71 use_instance
= bpy
.props
.BoolProperty(
72 name
="New Blender Instance",
73 description
="Open in a new Blender instance",
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
})
94 targetpath
= target
.library
.filepath
95 settings
["linked_objects"].append(target
.name
)
98 targetpath
= target
.library
.filepath
99 settings
["linked_objects"].append(target
.name
)
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
:
113 subprocess
.Popen([bpy
.app
.binary_path
, settings
["linked_file"]])
115 print("Error on the new Blender instance")
117 traceback
.print_exc()
119 bpy
.ops
.wm
.open_mainfile(filepath
=settings
["linked_file"])
121 print("Opened linked file!")
123 self
.report({'WARNING'}, target
.name
+ " is not linked")
124 print(target
.name
+ " is not linked")
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(
136 description
="Save the current file before opening original file",
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!")
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"
165 def poll(cls
, context
):
166 return (context
.active_object
is not None) or (settings
["original_file"] != "")
168 def draw(self
, context
):
170 scene
= context
.scene
171 icon
= "OUTLINER_DATA_" + context
.active_object
.type
175 if context
.active_object
.proxy
:
176 target
= context
.active_object
.proxy
178 target
= context
.active_object
.dupli_group
180 if settings
["original_file"] == "" 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
)
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
)
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
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
,
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
)
228 props
= layout
.operator("wm.return_to_original", icon
="LOOP_BACK")
229 props
.use_autosave
= scene
.use_autosave
231 layout
.prop(scene
, "use_autosave")
234 layout
.label(text
="%s is not linked" % context
.active_object
.name
,
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(
250 description
="Save the current file before opening a linked file",
252 bpy
.types
.Scene
.use_instance
= bpy
.props
.BoolProperty(
253 name
="New Blender Instance",
254 description
="Open in a new Blender instance",
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)
262 addon_keymaps
.append((km
, kmi
))
263 kmi
= km
.keymap_items
.new("wm.return_to_original", 'NUMPAD_SLASH', 'PRESS', shift
=True)
265 addon_keymaps
.append((km
, kmi
))
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
278 for km
, kmi
in addon_keymaps
:
279 km
.keymap_items
.remove(kmi
)
280 addon_keymaps
.clear()
283 if __name__
== "__main__":