Skip to content

ayon_usd

USD Addon for AYON - client part.

USDAddon

Bases: AYONAddon, ITrayAddon, IPluginPaths

Addon to add USD Support to AYON.

Addon can also skip distribution of binaries from server and can use path/arguments defined by server.

Cares about supplying USD Framework.

Source code in client/ayon_usd/addon.py
 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
class USDAddon(AYONAddon, ITrayAddon, IPluginPaths):
    """Addon to add USD Support to AYON.

    Addon can also skip distribution of binaries from server and can
    use path/arguments defined by server.

    Cares about supplying USD Framework.
    """

    name = "usd"
    version = __version__
    _download_window = None

    def tray_init(self):
        """Initialize tray module."""
        super(USDAddon, self).tray_init()

    def initialize(self, studio_settings):
        """Initialize USD Addon."""
        self._download_window = None

    def tray_start(self):
        """Start tray module.

        Download USD if needed.
        """
        self._download_global_lakefs_binaries()

    def tray_exit(self):
        """Exit tray module."""
        pass

    def tray_menu(self, tray_menu):
        """Add menu items to tray menu."""
        pass

    def get_launch_hook_paths(self):
        """Get paths to launch hooks."""
        return [os.path.join(USD_ADDON_DIR, "hooks")]

    def get_plugin_paths(self):
        return {
            "publish": [
                os.path.join(USD_ADDON_DIR, "plugins", "publish")
            ]
        }

    def _download_global_lakefs_binaries(self):
        settings = get_studio_settings()
        if not settings["usd"]["distribution"]["enabled"]:
            self.log.info("USD Binary distribution is disabled.")
            return

        os.makedirs(DOWNLOAD_DIR, exist_ok=True)

        if not os.path.exists(ADDON_DATA_JSON_PATH):
            now = datetime.now().astimezone(timezone.utc)
            with open(ADDON_DATA_JSON_PATH, "w+") as json_file:
                init_data = {
                    "ayon_usd_addon_first_init_utc": str(now)
                }
                json.dump(init_data, json_file)

        if not utils.is_usd_lib_download_needed(settings):
            self.log.info("USD Libs already available. Skipping download.")
            return

        lake_fs_usd_lib_path = config.get_lakefs_usdlib_path(settings)

        # Get modified time on LakeFS
        lake_fs = config.get_global_lake_instance(settings)
        usd_lib_lake_fs_time_cest = (
            lake_fs
            .get_element_info(lake_fs_usd_lib_path)
            .get("Modified Time")
        )
        if not usd_lib_lake_fs_time_cest:
            raise ValueError(
                "Unable to find UsdLib date modified timestamp on "
                f"LakeFs server: {lake_fs_usd_lib_path}"
            )

        with open(ADDON_DATA_JSON_PATH, "r+") as json_file:
            addon_data_json = json.load(json_file)
            addon_data_json["usd_lib_lake_fs_time_cest"] = usd_lib_lake_fs_time_cest

            json_file.seek(0)
            json.dump(
                addon_data_json,
                json_file,
            )
            json_file.truncate()

        controller = worker.Controller()

        usd_download_work_item = controller.construct_work_item(
            func=lake_fs.clone_element,
            kwargs={
                "lake_fs_object_uir": lake_fs_usd_lib_path,
                "dist_path": DOWNLOAD_DIR,
            },
            progress_title="Download UsdLib",
        )

        usd_zip_path = os.path.join(
            DOWNLOAD_DIR,
            os.path.basename(config.get_lakefs_usdlib_path(settings))
        )
        usd_lib_path = os.path.splitext(usd_zip_path)[0]
        controller.construct_work_item(
            func=zip.extract_zip_file,
            kwargs={
                "zip_file_path": usd_zip_path,
                "dest_dir": usd_lib_path,
            },
            progress_title="Unzip UsdLib",
            dependency_id=[usd_download_work_item.get_uuid()],
        )

        from .ayon_bin_client.ayon_bin_distro.gui import progress_ui
        download_ui = progress_ui.ProgressDialog(
            controller,
            close_on_finish=True,
            auto_close_timeout=1,
            delete_progress_bar_on_finish=False,
            title="ayon_usd-Addon [UsdLib Download]",
        )
        download_ui.setStyleSheet(style.load_stylesheet())
        download_ui.start()
        self._download_window = download_ui

get_launch_hook_paths()

Get paths to launch hooks.

Source code in client/ayon_usd/addon.py
58
59
60
def get_launch_hook_paths(self):
    """Get paths to launch hooks."""
    return [os.path.join(USD_ADDON_DIR, "hooks")]

initialize(studio_settings)

Initialize USD Addon.

Source code in client/ayon_usd/addon.py
39
40
41
def initialize(self, studio_settings):
    """Initialize USD Addon."""
    self._download_window = None

tray_exit()

Exit tray module.

Source code in client/ayon_usd/addon.py
50
51
52
def tray_exit(self):
    """Exit tray module."""
    pass

tray_init()

Initialize tray module.

Source code in client/ayon_usd/addon.py
35
36
37
def tray_init(self):
    """Initialize tray module."""
    super(USDAddon, self).tray_init()

tray_menu(tray_menu)

Add menu items to tray menu.

Source code in client/ayon_usd/addon.py
54
55
56
def tray_menu(self, tray_menu):
    """Add menu items to tray menu."""
    pass

tray_start()

Start tray module.

Download USD if needed.

Source code in client/ayon_usd/addon.py
43
44
45
46
47
48
def tray_start(self):
    """Start tray module.

    Download USD if needed.
    """
    self._download_global_lakefs_binaries()

extract_zip_file(progress_item, zip_file_path, dest_dir)

Extract a zip file to a destination directory.

Parameters:

Name Type Description Default
zip_file_path str

The path to the zip file.

required
dest_dir str

The directory where the zip file should be extracted.

required
Source code in client/ayon_usd/ayon_bin_client/ayon_bin_distro/util/zip.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def extract_zip_file(progress_item, zip_file_path: str, dest_dir: str) -> str:
    """Extract a zip file to a destination directory.

    Args:
        zip_file_path (str): The path to the zip file.
        dest_dir (str): The directory where the zip file should be extracted.

    """
    with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
        zip_ref.extractall(dest_dir)

    foulder_name = os.path.basename(zip_file_path).replace(".zip", "")
    return os.path.join(dest_dir, foulder_name)

get_download_dir(create_if_missing=True)

Dir path where files are downloaded.

Parameters:

Name Type Description Default
create_if_missing bool

Create dir if missing.

True

Returns:

Name Type Description
str

Path to download dir.

Source code in client/ayon_usd/utils.py
18
19
20
21
22
23
24
25
26
27
28
29
30
def get_download_dir(create_if_missing=True):
    """Dir path where files are downloaded.

    Args:
        create_if_missing (bool): Create dir if missing.

    Returns:
        str: Path to download dir.

    """
    if create_if_missing and not os.path.exists(DOWNLOAD_DIR):
        os.makedirs(DOWNLOAD_DIR, exist_ok=True)
    return DOWNLOAD_DIR

get_usd_pinning_envs(instance)

Get USD pinning file path from published representations.

This sets the rootless path to the USD pinning file and enables the static global cache. It is not setting PROJECT_ROOTS because they are platform dependent and on farm they need to be set on task level.

Parameters:

Name Type Description Default
instance Instance

Published representations.

required

Returns:

Name Type Description
dict dict

environment variables needed for USD pinning.

Source code in client/ayon_usd/utils.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def get_usd_pinning_envs(instance) -> dict:
    """Get USD pinning file path from published representations.

    This sets the rootless path to the USD pinning file and enables
    the static global cache. It is not setting ``PROJECT_ROOTS`` because
    they are platform dependent and on farm they need to be set on
    task level.

    Args:
        instance (pyblish.api.Instance): Published representations.

    Returns:
        dict: environment variables needed for USD pinning.

    """
    usd_pinning_rootless_file_path = None
    if not instance.data.get("published_representations"):
        usd_pinning_file_path = get_pinning_file_path(instance)
        anatomy = instance.context.data["anatomy"]
        usd_pinning_rootless_file_path = anatomy.find_root_template_from_path(
            usd_pinning_file_path,
        )[1]
    else:

        for repre_info in instance.data.get(
                "published_representations").values():
            rep = repre_info["representation"]

            if rep["name"] == "usd_pinning":
                usd_pinning_rootless_file_path = rep["attrib"]["path"]
                break

    if not usd_pinning_rootless_file_path:
        return {}

    return {
        "PINNING_FILE_PATH": usd_pinning_rootless_file_path,
        "ENABLE_STATIC_GLOBAL_CACHE": "1",
    }