Fix mesh_snap_utilities_line running without key-maps available
[blender-addons.git] / precision_drawing_tools / pdt_library.py
blob4850481b01d21ffe0f059d148b10a54262e8f9ab
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, PDT_ERR_OBJECTMODE
32 class PDT_OT_LibShow(Operator):
33 """Show Library File Details."""
35 bl_idname = "pdt.lib_show"
36 bl_label = "Show Library Details"
37 bl_options = {"REGISTER", "UNDO"}
39 def execute(self, context):
40 """Shows Location Of PDT Library File.
42 Args:
43 context: Blender bpy.context instance.
45 Returns:
46 Status Set.
47 """
49 scene = context.scene
50 pg = scene.pdt_pg
51 file_path = pg.pdt_library_path
52 pg.error = str(Path(file_path))
53 debug("PDT Parts Library:")
54 debug(f"{pg.error}")
55 bpy.context.window_manager.popup_menu(
56 oops, title="Information - Parts Library File", icon="INFO"
58 return {"FINISHED"}
61 class PDT_OT_Append(Operator):
62 """Append from Library at cursor Location."""
64 bl_idname = "pdt.append"
65 bl_label = "Append"
66 bl_options = {"REGISTER", "UNDO"}
68 def execute(self, context):
69 """Appends Objects from PDT Library file.
71 Note:
72 Appended Objects are placed at Cursor Location.
73 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
75 Args:
76 context: Blender bpy.context instance.
78 Returns:
79 Status Set.
80 """
82 scene = context.scene
83 pg = scene.pdt_pg
84 obj = context.view_layer.objects.active
85 if obj is not None:
86 if obj.mode != "OBJECT":
87 error_message = PDT_ERR_OBJECTMODE
88 self.report({"ERROR"}, error_message)
89 return {"FINISHED"}
91 obj_names = [o.name for o in context.view_layer.objects].copy()
92 file_path = pg.pdt_library_path
93 path = Path(file_path)
95 if path.is_file() and str(path).endswith(".blend"):
96 if pg.lib_mode == "OBJECTS":
97 bpy.ops.wm.append(
98 filepath=str(path),
99 directory=str(path) + "/Object",
100 filename=pg.lib_objects,
102 for obj in context.view_layer.objects:
103 if obj.name not in obj_names:
104 obj.select_set(False)
105 obj.location = Vector(
107 scene.cursor.location.x,
108 scene.cursor.location.y,
109 scene.cursor.location.z,
112 return {"FINISHED"}
113 if pg.lib_mode == "COLLECTIONS":
114 bpy.ops.wm.append(
115 filepath=str(path),
116 directory=str(path) + "/Collection",
117 filename=pg.lib_collections,
119 for obj in context.view_layer.objects:
120 if obj.name not in obj_names:
121 obj.select_set(False)
122 obj.location = Vector(
124 scene.cursor.location.x,
125 scene.cursor.location.y,
126 scene.cursor.location.z,
129 return {"FINISHED"}
130 if pg.lib_mode == "MATERIALS":
131 bpy.ops.wm.append(
132 filepath=str(path),
133 directory=str(path) + "/Material",
134 filename=pg.lib_materials,
136 return {"FINISHED"}
138 error_message = PDT_ERR_NO_LIBRARY
139 self.report({"ERROR"}, error_message)
140 return {"FINISHED"}
143 class PDT_OT_Link(Operator):
144 """Link from Library at Object's Origin."""
146 bl_idname = "pdt.link"
147 bl_label = "Link"
148 bl_options = {"REGISTER", "UNDO"}
150 def execute(self, context):
151 """Links Objects from PDT Library file.
153 Note:
154 Linked Objects are placed at Cursor Location
155 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
157 Args:
158 context: Blender bpy.context instance.
160 Returns:
161 Status Set.
164 scene = context.scene
165 pg = scene.pdt_pg
166 obj = context.view_layer.objects.active
167 if obj is not None:
168 if obj.mode != "OBJECT":
169 error_message = PDT_ERR_OBJECTMODE
170 self.report({"ERROR"}, error_message)
171 return {"FINISHED"}
173 file_path = pg.pdt_library_path
174 path = Path(file_path)
176 if path.is_file() and str(path).endswith(".blend"):
177 if pg.lib_mode == "OBJECTS":
178 bpy.ops.wm.link(
179 filepath=str(path),
180 directory=str(path) + "/Object",
181 filename=pg.lib_objects,
183 for obj in context.view_layer.objects:
184 obj.select_set(False)
185 return {"FINISHED"}
186 if pg.lib_mode == "COLLECTIONS":
187 bpy.ops.wm.link(
188 filepath=str(path),
189 directory=str(path) + "/Collection",
190 filename=pg.lib_collections,
192 for obj in context.view_layer.objects:
193 obj.select_set(False)
194 return {"FINISHED"}
195 if pg.lib_mode == "MATERIALS":
196 bpy.ops.wm.link(
197 filepath=str(path),
198 directory=str(path) + "/Material",
199 filename=pg.lib_materials,
201 return {"FINISHED"}
203 error_message = PDT_ERR_NO_LIBRARY
204 self.report({"ERROR"}, error_message)
205 return {"FINISHED"}