FBX IO: Add support for armature data custom properties
[blender-addons.git] / precision_drawing_tools / pdt_library.py
blob7168a5dea19b0102664bd407ca523affed188512
1 # SPDX-FileCopyrightText: 2019-2022 Alan Odom (Clockmender)
2 # SPDX-FileCopyrightText: 2019-2022 Rune Morling (ermo)
4 # SPDX-License-Identifier: GPL-2.0-or-later
6 import bpy
7 from bpy.types import Operator
8 from mathutils import Vector
9 from pathlib import Path
10 from .pdt_functions import debug, oops
11 from .pdt_msg_strings import PDT_ERR_NO_LIBRARY, PDT_ERR_OBJECTMODE
14 class PDT_OT_LibShow(Operator):
15 """Show Library File Details"""
17 bl_idname = "pdt.lib_show"
18 bl_label = "Show Library Details"
19 bl_options = {"REGISTER", "UNDO"}
21 def execute(self, context):
22 """Shows Location Of PDT Library File.
24 Args:
25 context: Blender bpy.context instance.
27 Returns:
28 Status Set.
29 """
31 scene = context.scene
32 pg = scene.pdt_pg
33 file_path = pg.pdt_library_path
34 pg.error = str(Path(file_path))
35 debug("PDT Parts Library:")
36 debug(f"{pg.error}")
37 bpy.context.window_manager.popup_menu(
38 oops, title="Information - Parts Library File", icon="INFO"
40 return {"FINISHED"}
43 class PDT_OT_Append(Operator):
44 """Append from Library at cursor Location"""
46 bl_idname = "pdt.append"
47 bl_label = "Append"
48 bl_options = {"REGISTER", "UNDO"}
50 def execute(self, context):
51 """Appends Objects from PDT Library file.
53 Note:
54 Appended Objects are placed at Cursor Location.
55 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
57 Args:
58 context: Blender bpy.context instance.
60 Returns:
61 Status Set.
62 """
64 scene = context.scene
65 pg = scene.pdt_pg
66 obj = context.view_layer.objects.active
67 if obj is not None:
68 if obj.mode != "OBJECT":
69 error_message = PDT_ERR_OBJECTMODE
70 self.report({"ERROR"}, error_message)
71 return {"FINISHED"}
73 obj_names = [o.name for o in context.view_layer.objects].copy()
74 file_path = pg.pdt_library_path
75 path = Path(file_path)
77 if path.is_file() and str(path).endswith(".blend"):
78 if pg.lib_mode == "OBJECTS":
79 bpy.ops.wm.append(
80 filepath=str(path),
81 directory=str(path) + "/Object",
82 filename=pg.lib_objects,
84 for obj in context.view_layer.objects:
85 if obj.name not in obj_names:
86 obj.select_set(False)
87 obj.location = Vector(
89 scene.cursor.location.x,
90 scene.cursor.location.y,
91 scene.cursor.location.z,
94 return {"FINISHED"}
95 if pg.lib_mode == "COLLECTIONS":
96 bpy.ops.wm.append(
97 filepath=str(path),
98 directory=str(path) + "/Collection",
99 filename=pg.lib_collections,
101 for obj in context.view_layer.objects:
102 if obj.name not in obj_names:
103 obj.select_set(False)
104 obj.location = Vector(
106 scene.cursor.location.x,
107 scene.cursor.location.y,
108 scene.cursor.location.z,
111 return {"FINISHED"}
112 if pg.lib_mode == "MATERIALS":
113 bpy.ops.wm.append(
114 filepath=str(path),
115 directory=str(path) + "/Material",
116 filename=pg.lib_materials,
118 return {"FINISHED"}
120 error_message = PDT_ERR_NO_LIBRARY
121 self.report({"ERROR"}, error_message)
122 return {"FINISHED"}
125 class PDT_OT_Link(Operator):
126 """Link from Library at Object's Origin"""
128 bl_idname = "pdt.link"
129 bl_label = "Link"
130 bl_options = {"REGISTER", "UNDO"}
132 def execute(self, context):
133 """Links Objects from PDT Library file.
135 Note:
136 Linked Objects are placed at Cursor Location
137 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
139 Args:
140 context: Blender bpy.context instance.
142 Returns:
143 Status Set.
146 scene = context.scene
147 pg = scene.pdt_pg
148 obj = context.view_layer.objects.active
149 if obj is not None:
150 if obj.mode != "OBJECT":
151 error_message = PDT_ERR_OBJECTMODE
152 self.report({"ERROR"}, error_message)
153 return {"FINISHED"}
155 file_path = pg.pdt_library_path
156 path = Path(file_path)
158 if path.is_file() and str(path).endswith(".blend"):
159 if pg.lib_mode == "OBJECTS":
160 bpy.ops.wm.link(
161 filepath=str(path),
162 directory=str(path) + "/Object",
163 filename=pg.lib_objects,
165 for obj in context.view_layer.objects:
166 obj.select_set(False)
167 return {"FINISHED"}
168 if pg.lib_mode == "COLLECTIONS":
169 bpy.ops.wm.link(
170 filepath=str(path),
171 directory=str(path) + "/Collection",
172 filename=pg.lib_collections,
174 for obj in context.view_layer.objects:
175 obj.select_set(False)
176 return {"FINISHED"}
177 if pg.lib_mode == "MATERIALS":
178 bpy.ops.wm.link(
179 filepath=str(path),
180 directory=str(path) + "/Material",
181 filename=pg.lib_materials,
183 return {"FINISHED"}
185 error_message = PDT_ERR_NO_LIBRARY
186 self.report({"ERROR"}, error_message)
187 return {"FINISHED"}