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 # -----------------------------------------------------------------------
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
32 class PDT_OT_LibShow(Operator
):
33 """Show Library File Details."""
34 bl_idname
= "pdt.lib_show"
35 bl_label
= "Show Library Details"
36 bl_options
= {"REGISTER", "UNDO"}
38 def execute(self
, context
):
39 """Shows Location Of PDT Library File.
42 context: Blender bpy.context instance.
50 file_path
= context
.preferences
.addons
[__package__
].preferences
.pdt_library_path
51 pg
.error
= str(Path(file_path
))
52 debug("PDT Parts Library:")
54 bpy
.context
.window_manager
.popup_menu(oops
, title
="Information - Parts Library File", icon
="INFO")
58 class PDT_OT_Append(Operator
):
59 """Append from Library at cursor Location."""
61 bl_idname
= "pdt.append"
63 bl_options
= {"REGISTER", "UNDO"}
65 def execute(self
, context
):
66 """Appends Objects from PDT Library file.
68 Appended Objects are placed at Cursor Location.
71 context: Blender bpy.context instance.
74 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
82 obj_names
= [o
.name
for o
in context
.view_layer
.objects
].copy()
83 file_path
= context
.preferences
.addons
[__package__
].preferences
.pdt_library_path
84 path
= Path(file_path
)
86 if path
.is_file() and ".blend" in str(path
):
87 if pg
.lib_mode
== "OBJECTS":
89 bpy
.ops
.object.mode_set(mode
='OBJECT')
91 filepath
=str(path
), directory
=str(path
) + "/Object", filename
=pg
.lib_objects
93 for obj
in context
.view_layer
.objects
:
94 if obj
.name
not in obj_names
:
96 obj
.location
= Vector(
97 (scene
.cursor
.location
.x
, scene
.cursor
.location
.y
, scene
.cursor
.location
.z
)
100 elif pg
.lib_mode
== "COLLECTIONS":
102 filepath
=str(path
), directory
=str(path
) + "/Collection", filename
=pg
.lib_collections
104 for obj
in context
.view_layer
.objects
:
105 if obj
.name
not in obj_names
:
106 obj
.select_set(False)
107 obj
.location
= Vector(
108 (scene
.cursor
.location
.x
, scene
.cursor
.location
.y
, scene
.cursor
.location
.z
)
111 elif pg
.lib_mode
== "MATERIALS":
113 filepath
=str(path
), directory
=str(path
) + "/Material", filename
=pg
.lib_materials
117 errmsg
= PDT_ERR_NO_LIBRARY
118 self
.report({"ERROR"}, errmsg
)
122 class PDT_OT_Link(Operator
):
123 """Link from Library at Object's Origin."""
125 bl_idname
= "pdt.link"
127 bl_options
= {"REGISTER", "UNDO"}
129 def execute(self
, context
):
130 """Links Objects from PDT Library file.
132 Linked Objects are placed at Cursor Location
138 Uses pg.lib_objects, pg.lib_collections & pg.lib_materials
144 scene
= context
.scene
146 file_path
= context
.preferences
.addons
[__package__
].preferences
.pdt_library_path
147 path
= Path(file_path
)
148 if path
.is_file() and ".blend" in str(path
):
149 if pg
.lib_mode
== "OBJECTS":
151 bpy
.ops
.object.mode_set(mode
='OBJECT')
153 filepath
=str(path
), directory
=str(path
) + "/Object", filename
=pg
.lib_objects
155 for obj
in context
.view_layer
.objects
:
156 obj
.select_set(False)
158 elif pg
.lib_mode
== "COLLECTIONS":
160 filepath
=str(path
), directory
=str(path
) + "/Collection", filename
=pg
.lib_collections
162 for obj
in context
.view_layer
.objects
:
163 obj
.select_set(False)
165 elif pg
.lib_mode
== "MATERIALS":
167 filepath
=str(path
), directory
=str(path
) + "/Material", filename
=pg
.lib_materials
171 errmsg
= PDT_ERR_NO_LIBRARY
172 self
.report({"ERROR"}, errmsg
)