Update scripts to account for removal of the context override to bpy.ops
[blender-addons.git] / precision_drawing_tools / pdt_library.py
blob7dbc1bec50c54b22f53a2c32a2453a79e6d18499
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # -----------------------------------------------------------------------
4 # Author: Alan Odom (Clockmender), Rune Morling (ermo) Copyright (c) 2019
5 # -----------------------------------------------------------------------
7 import bpy
8 from bpy.types import Operator
9 from mathutils import Vector
10 from pathlib import Path
11 from .pdt_functions import debug, oops
12 from .pdt_msg_strings import PDT_ERR_NO_LIBRARY, PDT_ERR_OBJECTMODE
15 class PDT_OT_LibShow(Operator):
16 """Show Library File Details"""
18 bl_idname = "pdt.lib_show"
19 bl_label = "Show Library Details"
20 bl_options = {"REGISTER", "UNDO"}
22 def execute(self, context):
23 """Shows Location Of PDT Library File.
25 Args:
26 context: Blender bpy.context instance.
28 Returns:
29 Status Set.
30 """
32 scene = context.scene
33 pg = scene.pdt_pg
34 file_path = pg.pdt_library_path
35 pg.error = str(Path(file_path))
36 debug("PDT Parts Library:")
37 debug(f"{pg.error}")
38 bpy.context.window_manager.popup_menu(
39 oops, title="Information - Parts Library File", icon="INFO"
41 return {"FINISHED"}
44 class PDT_OT_Append(Operator):
45 """Append from Library at cursor Location"""
47 bl_idname = "pdt.append"
48 bl_label = "Append"
49 bl_options = {"REGISTER", "UNDO"}
51 def execute(self, context):
52 """Appends Objects from PDT Library file.
54 Note:
55 Appended Objects are placed at Cursor Location.
56 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
58 Args:
59 context: Blender bpy.context instance.
61 Returns:
62 Status Set.
63 """
65 scene = context.scene
66 pg = scene.pdt_pg
67 obj = context.view_layer.objects.active
68 if obj is not None:
69 if obj.mode != "OBJECT":
70 error_message = PDT_ERR_OBJECTMODE
71 self.report({"ERROR"}, error_message)
72 return {"FINISHED"}
74 obj_names = [o.name for o in context.view_layer.objects].copy()
75 file_path = pg.pdt_library_path
76 path = Path(file_path)
78 if path.is_file() and str(path).endswith(".blend"):
79 if pg.lib_mode == "OBJECTS":
80 bpy.ops.wm.append(
81 filepath=str(path),
82 directory=str(path) + "/Object",
83 filename=pg.lib_objects,
85 for obj in context.view_layer.objects:
86 if obj.name not in obj_names:
87 obj.select_set(False)
88 obj.location = Vector(
90 scene.cursor.location.x,
91 scene.cursor.location.y,
92 scene.cursor.location.z,
95 return {"FINISHED"}
96 if pg.lib_mode == "COLLECTIONS":
97 bpy.ops.wm.append(
98 filepath=str(path),
99 directory=str(path) + "/Collection",
100 filename=pg.lib_collections,
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 == "MATERIALS":
114 bpy.ops.wm.append(
115 filepath=str(path),
116 directory=str(path) + "/Material",
117 filename=pg.lib_materials,
119 return {"FINISHED"}
121 error_message = PDT_ERR_NO_LIBRARY
122 self.report({"ERROR"}, error_message)
123 return {"FINISHED"}
126 class PDT_OT_Link(Operator):
127 """Link from Library at Object's Origin"""
129 bl_idname = "pdt.link"
130 bl_label = "Link"
131 bl_options = {"REGISTER", "UNDO"}
133 def execute(self, context):
134 """Links Objects from PDT Library file.
136 Note:
137 Linked Objects are placed at Cursor Location
138 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
140 Args:
141 context: Blender bpy.context instance.
143 Returns:
144 Status Set.
147 scene = context.scene
148 pg = scene.pdt_pg
149 obj = context.view_layer.objects.active
150 if obj is not None:
151 if obj.mode != "OBJECT":
152 error_message = PDT_ERR_OBJECTMODE
153 self.report({"ERROR"}, error_message)
154 return {"FINISHED"}
156 file_path = pg.pdt_library_path
157 path = Path(file_path)
159 if path.is_file() and str(path).endswith(".blend"):
160 if pg.lib_mode == "OBJECTS":
161 bpy.ops.wm.link(
162 filepath=str(path),
163 directory=str(path) + "/Object",
164 filename=pg.lib_objects,
166 for obj in context.view_layer.objects:
167 obj.select_set(False)
168 return {"FINISHED"}
169 if pg.lib_mode == "COLLECTIONS":
170 bpy.ops.wm.link(
171 filepath=str(path),
172 directory=str(path) + "/Collection",
173 filename=pg.lib_collections,
175 for obj in context.view_layer.objects:
176 obj.select_set(False)
177 return {"FINISHED"}
178 if pg.lib_mode == "MATERIALS":
179 bpy.ops.wm.link(
180 filepath=str(path),
181 directory=str(path) + "/Material",
182 filename=pg.lib_materials,
184 return {"FINISHED"}
186 error_message = PDT_ERR_NO_LIBRARY
187 self.report({"ERROR"}, error_message)
188 return {"FINISHED"}