Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pdt_library.py « precision_drawing_tools - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7dbc1bec50c54b22f53a2c32a2453a79e6d18499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# SPDX-License-Identifier: GPL-2.0-or-later

# -----------------------------------------------------------------------
# Author: Alan Odom (Clockmender), Rune Morling (ermo) Copyright (c) 2019
# -----------------------------------------------------------------------
#
import bpy
from bpy.types import Operator
from mathutils import Vector
from pathlib import Path
from .pdt_functions import debug, oops
from .pdt_msg_strings import PDT_ERR_NO_LIBRARY, PDT_ERR_OBJECTMODE


class PDT_OT_LibShow(Operator):
    """Show Library File Details"""

    bl_idname = "pdt.lib_show"
    bl_label = "Show Library Details"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        """Shows Location Of PDT Library File.

        Args:
            context: Blender bpy.context instance.

        Returns:
            Status Set.
        """

        scene = context.scene
        pg = scene.pdt_pg
        file_path = pg.pdt_library_path
        pg.error = str(Path(file_path))
        debug("PDT Parts Library:")
        debug(f"{pg.error}")
        bpy.context.window_manager.popup_menu(
            oops, title="Information - Parts Library File", icon="INFO"
        )
        return {"FINISHED"}


class PDT_OT_Append(Operator):
    """Append from Library at cursor Location"""

    bl_idname = "pdt.append"
    bl_label = "Append"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        """Appends Objects from PDT Library file.

        Note:
            Appended Objects are placed at Cursor Location.
            Uses pg.lib_objects, pg.lib_collections & pg.lib_materials

        Args:
            context: Blender bpy.context instance.

        Returns:
            Status Set.
        """

        scene = context.scene
        pg = scene.pdt_pg
        obj = context.view_layer.objects.active
        if obj is not None:
            if obj.mode != "OBJECT":
                error_message = PDT_ERR_OBJECTMODE
                self.report({"ERROR"}, error_message)
                return {"FINISHED"}

        obj_names = [o.name for o in context.view_layer.objects].copy()
        file_path = pg.pdt_library_path
        path = Path(file_path)

        if path.is_file() and str(path).endswith(".blend"):
            if pg.lib_mode == "OBJECTS":
                bpy.ops.wm.append(
                    filepath=str(path),
                    directory=str(path) + "/Object",
                    filename=pg.lib_objects,
                )
                for obj in context.view_layer.objects:
                    if obj.name not in obj_names:
                        obj.select_set(False)
                        obj.location = Vector(
                            (
                                scene.cursor.location.x,
                                scene.cursor.location.y,
                                scene.cursor.location.z,
                            )
                        )
                return {"FINISHED"}
            if pg.lib_mode == "COLLECTIONS":
                bpy.ops.wm.append(
                    filepath=str(path),
                    directory=str(path) + "/Collection",
                    filename=pg.lib_collections,
                )
                for obj in context.view_layer.objects:
                    if obj.name not in obj_names:
                        obj.select_set(False)
                        obj.location = Vector(
                            (
                                scene.cursor.location.x,
                                scene.cursor.location.y,
                                scene.cursor.location.z,
                            )
                        )
                return {"FINISHED"}
            if pg.lib_mode == "MATERIALS":
                bpy.ops.wm.append(
                    filepath=str(path),
                    directory=str(path) + "/Material",
                    filename=pg.lib_materials,
                )
                return {"FINISHED"}

        error_message = PDT_ERR_NO_LIBRARY
        self.report({"ERROR"}, error_message)
        return {"FINISHED"}


class PDT_OT_Link(Operator):
    """Link from Library at Object's Origin"""

    bl_idname = "pdt.link"
    bl_label = "Link"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        """Links Objects from PDT Library file.

        Note:
            Linked Objects are placed at Cursor Location
            Uses pg.lib_objects, pg.lib_collections & pg.lib_materials

        Args:
            context: Blender bpy.context instance.

        Returns:
            Status Set.
        """

        scene = context.scene
        pg = scene.pdt_pg
        obj = context.view_layer.objects.active
        if obj is not None:
            if obj.mode != "OBJECT":
                error_message = PDT_ERR_OBJECTMODE
                self.report({"ERROR"}, error_message)
                return {"FINISHED"}

        file_path = pg.pdt_library_path
        path = Path(file_path)

        if path.is_file() and str(path).endswith(".blend"):
            if pg.lib_mode == "OBJECTS":
                bpy.ops.wm.link(
                    filepath=str(path),
                    directory=str(path) + "/Object",
                    filename=pg.lib_objects,
                )
                for obj in context.view_layer.objects:
                    obj.select_set(False)
                return {"FINISHED"}
            if pg.lib_mode == "COLLECTIONS":
                bpy.ops.wm.link(
                    filepath=str(path),
                    directory=str(path) + "/Collection",
                    filename=pg.lib_collections,
                )
                for obj in context.view_layer.objects:
                    obj.select_set(False)
                return {"FINISHED"}
            if pg.lib_mode == "MATERIALS":
                bpy.ops.wm.link(
                    filepath=str(path),
                    directory=str(path) + "/Material",
                    filename=pg.lib_materials,
                )
                return {"FINISHED"}

        error_message = PDT_ERR_NO_LIBRARY
        self.report({"ERROR"}, error_message)
        return {"FINISHED"}