1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # -----------------------------------------------------------------------
4 # Author: Alan Odom (Clockmender), Rune Morling (ermo) Copyright (c) 2019
5 # -----------------------------------------------------------------------
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.
26 context: Blender bpy.context instance.
34 file_path
= pg
.pdt_library_path
35 pg
.error
= str(Path(file_path
))
36 debug("PDT Parts Library:")
38 bpy
.context
.window_manager
.popup_menu(
39 oops
, title
="Information - Parts Library File", icon
="INFO"
44 class PDT_OT_Append(Operator
):
45 """Append from Library at cursor Location"""
47 bl_idname
= "pdt.append"
49 bl_options
= {"REGISTER", "UNDO"}
51 def execute(self
, context
):
52 """Appends Objects from PDT Library file.
55 Appended Objects are placed at Cursor Location.
56 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
59 context: Blender bpy.context instance.
67 obj
= context
.view_layer
.objects
.active
69 if obj
.mode
!= "OBJECT":
70 error_message
= PDT_ERR_OBJECTMODE
71 self
.report({"ERROR"}, error_message
)
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":
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
:
88 obj
.location
= Vector(
90 scene
.cursor
.location
.x
,
91 scene
.cursor
.location
.y
,
92 scene
.cursor
.location
.z
,
96 if pg
.lib_mode
== "COLLECTIONS":
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
,
113 if pg
.lib_mode
== "MATERIALS":
116 directory
=str(path
) + "/Material",
117 filename
=pg
.lib_materials
,
121 error_message
= PDT_ERR_NO_LIBRARY
122 self
.report({"ERROR"}, error_message
)
126 class PDT_OT_Link(Operator
):
127 """Link from Library at Object's Origin"""
129 bl_idname
= "pdt.link"
131 bl_options
= {"REGISTER", "UNDO"}
133 def execute(self
, context
):
134 """Links Objects from PDT Library file.
137 Linked Objects are placed at Cursor Location
138 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
141 context: Blender bpy.context instance.
147 scene
= context
.scene
149 obj
= context
.view_layer
.objects
.active
151 if obj
.mode
!= "OBJECT":
152 error_message
= PDT_ERR_OBJECTMODE
153 self
.report({"ERROR"}, error_message
)
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":
163 directory
=str(path
) + "/Object",
164 filename
=pg
.lib_objects
,
166 for obj
in context
.view_layer
.objects
:
167 obj
.select_set(False)
169 if pg
.lib_mode
== "COLLECTIONS":
172 directory
=str(path
) + "/Collection",
173 filename
=pg
.lib_collections
,
175 for obj
in context
.view_layer
.objects
:
176 obj
.select_set(False)
178 if pg
.lib_mode
== "MATERIALS":
181 directory
=str(path
) + "/Material",
182 filename
=pg
.lib_materials
,
186 error_message
= PDT_ERR_NO_LIBRARY
187 self
.report({"ERROR"}, error_message
)