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

functions.py « pose_library - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f0845b2cbb83555f5478554dcb6fa604f32b8ed (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
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

"""
Pose Library mockup - functions.
"""

import dataclasses
from pathlib import Path
from typing import Any, List, Set, cast, Iterable, Optional

Datablock = Any

import bpy
from bpy.types import (
    FileSelectEntry,
    AssetLibraryReference,
    Context,
)


def asset_mark(context: Context, datablock: Any) -> Set[str]:
    asset_mark_ctx = {
        **context.copy(),
        "id": datablock,
    }
    return cast(Set[str], bpy.ops.asset.mark(asset_mark_ctx))


def asset_clear(context: Context, datablock: Any) -> Set[str]:
    asset_clear_ctx = {
        **context.copy(),
        "id": datablock,
    }
    result = bpy.ops.asset.clear(asset_clear_ctx)
    assert isinstance(result, set)
    if "FINISHED" in result:
        datablock.use_fake_user = False
    return result


def load_assets_from(filepath: Path) -> List[Datablock]:
    if not has_assets(filepath):
        # Avoid loading any datablocks when there are none marked as asset.
        return []

    # Append everything from the file.
    with bpy.data.libraries.load(str(filepath)) as (
        data_from,
        data_to,
    ):
        for attr in dir(data_to):
            setattr(data_to, attr, getattr(data_from, attr))

    # Iterate over the appended datablocks to find assets.
    def loaded_datablocks() -> Iterable[Datablock]:
        for attr in dir(data_to):
            datablocks = getattr(data_to, attr)
            for datablock in datablocks:
                yield datablock

    loaded_assets = []
    for datablock in loaded_datablocks():
        if not getattr(datablock, "asset_data", None):
            continue

        # Fake User is lost when appending from another file.
        datablock.use_fake_user = True
        loaded_assets.append(datablock)
    return loaded_assets


def has_assets(filepath: Path) -> bool:
    with bpy.data.libraries.load(str(filepath), assets_only=True) as (
        data_from,
        _,
    ):
        for attr in dir(data_from):
            data_names = getattr(data_from, attr)
            if data_names:
                return True
    return False


@dataclasses.dataclass
class AssetLoadInfo:
    """Everything you need to temp-load an asset."""

    file_path: str
    asset_name: str
    id_type: str


def active_asset_load_info(
    asset_library: AssetLibraryReference, asset: FileSelectEntry
) -> Optional[AssetLoadInfo]:
    asset_lib_path = bpy.types.AssetHandle.get_full_library_path(asset, asset_library)
    if asset_lib_path == "":
        return None

    return AssetLoadInfo(
        asset_lib_path,
        asset.name,
        asset.id_type,
    )