add_curve_extra_objects: fix invalid identity checks
[blender-addons.git] / precision_drawing_tools / pdt_library.py
blob096cdcf38bb3c749ecdaec5ca559d6f91f77881a
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 # -----------------------------------------------------------------------
21 # Author: Alan Odom (Clockmender), Rune Morling (ermo) Copyright (c) 2019
22 # -----------------------------------------------------------------------
24 import bpy
25 from bpy.types import Operator
26 from mathutils import Vector
27 from pathlib import Path
28 from .pdt_functions import debug, oops
29 from .pdt_msg_strings import PDT_ERR_NO_LIBRARY
32 class PDT_OT_LibShow(Operator):
33 """Show Library File Details."""
34 bl_idname = "pdt.lib_show"
35 bl_label = "Show Library Details"
36 bl_options = {"REGISTER", "UNDO"}
38 def execute(self, context):
39 """Shows Location Of PDT Library File.
41 Args:
42 context: Blender bpy.context instance.
44 Returns:
45 Status Set.
46 """
48 scene = context.scene
49 pg = scene.pdt_pg
50 file_path = context.preferences.addons[__package__].preferences.pdt_library_path
51 pg.error = str(Path(file_path))
52 debug("PDT Parts Library:")
53 debug(f"{pg.error}")
54 bpy.context.window_manager.popup_menu(oops, title="Information - Parts Library File", icon="INFO")
55 return {"FINISHED"}
58 class PDT_OT_Append(Operator):
59 """Append from Library at cursor Location."""
61 bl_idname = "pdt.append"
62 bl_label = "Append"
63 bl_options = {"REGISTER", "UNDO"}
65 def execute(self, context):
66 """Appends Objects from PDT Library file.
68 Appended Objects are placed at Cursor Location.
70 Args:
71 context: Blender bpy.context instance.
73 Notes:
74 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
76 Returns:
77 Status Set.
78 """
80 scene = context.scene
81 pg = scene.pdt_pg
82 obj_names = [o.name for o in context.view_layer.objects].copy()
83 file_path = context.preferences.addons[__package__].preferences.pdt_library_path
84 path = Path(file_path)
86 if path.is_file() and ".blend" in str(path):
87 if pg.lib_mode == "OBJECTS":
88 # Force object Mode
89 bpy.ops.object.mode_set(mode='OBJECT')
90 bpy.ops.wm.append(
91 filepath=str(path), directory=str(path) + "/Object", filename=pg.lib_objects
93 for obj in context.view_layer.objects:
94 if obj.name not in obj_names:
95 obj.select_set(False)
96 obj.location = Vector(
97 (scene.cursor.location.x, scene.cursor.location.y, scene.cursor.location.z)
99 return {"FINISHED"}
100 elif pg.lib_mode == "COLLECTIONS":
101 bpy.ops.wm.append(
102 filepath=str(path), directory=str(path) + "/Collection", filename=pg.lib_collections
104 for obj in context.view_layer.objects:
105 if obj.name not in obj_names:
106 obj.select_set(False)
107 obj.location = Vector(
108 (scene.cursor.location.x, scene.cursor.location.y, scene.cursor.location.z)
110 return {"FINISHED"}
111 elif pg.lib_mode == "MATERIALS":
112 bpy.ops.wm.append(
113 filepath=str(path), directory=str(path) + "/Material", filename=pg.lib_materials
115 return {"FINISHED"}
116 else:
117 errmsg = PDT_ERR_NO_LIBRARY
118 self.report({"ERROR"}, errmsg)
119 return {"FINISHED"}
122 class PDT_OT_Link(Operator):
123 """Link from Library at Object's Origin."""
125 bl_idname = "pdt.link"
126 bl_label = "Link"
127 bl_options = {"REGISTER", "UNDO"}
129 def execute(self, context):
130 """Links Objects from PDT Library file.
132 Linked Objects are placed at Cursor Location
134 Args:
135 context
137 Notes:
138 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
140 Returns:
141 Status Set.
144 scene = context.scene
145 pg = scene.pdt_pg
146 file_path = context.preferences.addons[__package__].preferences.pdt_library_path
147 path = Path(file_path)
148 if path.is_file() and ".blend" in str(path):
149 if pg.lib_mode == "OBJECTS":
150 # Force object Mode
151 bpy.ops.object.mode_set(mode='OBJECT')
152 bpy.ops.wm.link(
153 filepath=str(path), directory=str(path) + "/Object", filename=pg.lib_objects
155 for obj in context.view_layer.objects:
156 obj.select_set(False)
157 return {"FINISHED"}
158 elif pg.lib_mode == "COLLECTIONS":
159 bpy.ops.wm.link(
160 filepath=str(path), directory=str(path) + "/Collection", filename=pg.lib_collections
162 for obj in context.view_layer.objects:
163 obj.select_set(False)
164 return {"FINISHED"}
165 elif pg.lib_mode == "MATERIALS":
166 bpy.ops.wm.link(
167 filepath=str(path), directory=str(path) + "/Material", filename=pg.lib_materials
169 return {"FINISHED"}
170 else:
171 errmsg = PDT_ERR_NO_LIBRARY
172 self.report({"ERROR"}, errmsg)
173 return {"FINISHED"}