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
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.
25 context: Blender bpy.context instance.
33 file_path
= pg
.pdt_library_path
34 pg
.error
= str(Path(file_path
))
35 debug("PDT Parts Library:")
37 bpy
.context
.window_manager
.popup_menu(
38 oops
, title
="Information - Parts Library File", icon
="INFO"
43 class PDT_OT_Append(Operator
):
44 """Append from Library at cursor Location"""
46 bl_idname
= "pdt.append"
48 bl_options
= {"REGISTER", "UNDO"}
50 def execute(self
, context
):
51 """Appends Objects from PDT Library file.
54 Appended Objects are placed at Cursor Location.
55 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
58 context: Blender bpy.context instance.
66 obj
= context
.view_layer
.objects
.active
68 if obj
.mode
!= "OBJECT":
69 error_message
= PDT_ERR_OBJECTMODE
70 self
.report({"ERROR"}, error_message
)
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":
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
:
87 obj
.location
= Vector(
89 scene
.cursor
.location
.x
,
90 scene
.cursor
.location
.y
,
91 scene
.cursor
.location
.z
,
95 if pg
.lib_mode
== "COLLECTIONS":
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
,
112 if pg
.lib_mode
== "MATERIALS":
115 directory
=str(path
) + "/Material",
116 filename
=pg
.lib_materials
,
120 error_message
= PDT_ERR_NO_LIBRARY
121 self
.report({"ERROR"}, error_message
)
125 class PDT_OT_Link(Operator
):
126 """Link from Library at Object's Origin"""
128 bl_idname
= "pdt.link"
130 bl_options
= {"REGISTER", "UNDO"}
132 def execute(self
, context
):
133 """Links Objects from PDT Library file.
136 Linked Objects are placed at Cursor Location
137 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
140 context: Blender bpy.context instance.
146 scene
= context
.scene
148 obj
= context
.view_layer
.objects
.active
150 if obj
.mode
!= "OBJECT":
151 error_message
= PDT_ERR_OBJECTMODE
152 self
.report({"ERROR"}, error_message
)
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":
162 directory
=str(path
) + "/Object",
163 filename
=pg
.lib_objects
,
165 for obj
in context
.view_layer
.objects
:
166 obj
.select_set(False)
168 if pg
.lib_mode
== "COLLECTIONS":
171 directory
=str(path
) + "/Collection",
172 filename
=pg
.lib_collections
,
174 for obj
in context
.view_layer
.objects
:
175 obj
.select_set(False)
177 if pg
.lib_mode
== "MATERIALS":
180 directory
=str(path
) + "/Material",
181 filename
=pg
.lib_materials
,
185 error_message
= PDT_ERR_NO_LIBRARY
186 self
.report({"ERROR"}, error_message
)