Skip to content

usd_chunks.chunk_registry

The ChunkRegistry singleton, which finds, loads and unloads chunk modules.

The USD_CHUNK_DIRS environment variable tells the registry where to scan for chunk modules (.py files), nested folders included. The registry also holds the scene, which is how running code finds a chunk by name:

reg = ChunkRegistry()
my_chunk = reg.scene.get_chunk("my_chunk")  # or chunk.get_chunk("my_chunk")

ChunkDependencyManager ⚓︎

Installs and tracks the pip packages chunk modules declare as requirements.

Packages go into USD_CHUNK_DEPENDENCY_DIR, which is added to sys.path. Nothing is installed without the scene's say-so; see USD_CHUNK_AUTO_INSTALL.

initialize ⚓︎

initialize()

Put the dependency directory on sys.path and in PYTHONPATH for subprocesses.

Source code in usd_chunks/chunk_registry.py
def initialize(self):
    """Put the dependency directory on sys.path and in PYTHONPATH for subprocesses."""
    before = getattr(self, "package_dir", "")
    # One folder per Python version. usdview, Blender and a CLI venv run different
    # interpreters against the same USD_CHUNK_DEPENDENCY_DIR, and a compiled wheel
    # (numpy, mujoco) pip-installed for one cannot be imported by another -- while
    # `pip list --path` would still report it as installed to all of them.
    self.package_dir = ""
    self.package_dir = self.target_dir()
    # Kept across the initialize() that every stage reload runs: `_listed` stays True,
    # so nothing would list the packages again, and an emptied cache read numpy as
    # missing on opening the next network -- Blender asked to install the numpy it
    # ships. Started afresh, listing included, only for another folder.
    if not hasattr(self, "_cache") or self.package_dir != before:
        self._cache = defaultdict(set)
        type(self)._listed = False

    if self.package_dir and _no_packages_in(self.package_dir):
        try:
            migrated = migrate_legacy_dependencies(
                _legacy_dependency_dirs(USD_CHUNK_DEPENDENCY_DIR()), self.package_dir
            )
        except OSError:
            logger.warning("Could not migrate installed chunk dependencies", exc_info=True)
            migrated = []
        if migrated:
            logger.info("Migrated chunk dependencies to %s: %s", self.package_dir, ", ".join(migrated))

    if self.package_dir:
        if self.package_dir not in sys.path:
            sys.path.append(str(self.package_dir))

            # When a chunk runs a subprocess, the os.environ is passed as the env. Therefore we should
            # also make sure the PYTHONPATH is updated in order for the subprocess to find the packages
            os.environ["PYTHONPATH"] = os.environ.get("PYTHONPATH", "") + os.pathsep + str(self.package_dir)

        already_initialized = self.initialized
        self.initialized = True
        self._load_cache()

        # Once per process, not once per initialize(). This runs again on every stage
        # reload and on every ChunkRegistry().initialize(), so an unguarded register()
        # accumulates a handler per call -- each one rewriting the same cache file at
        # exit, and each one logging after the interpreter has closed its streams, which
        # surfaces as a wall of "ValueError: I/O operation on closed file" once the
        # process ends. Harmless but alarming, and it hides real shutdown errors.
        if not already_initialized:
            atexit.register(self._write_cache)

install ⚓︎

install(packages, chunk_name='')

pip-install packages into the dependency dir, now, without asking; whether pip succeeded.

The scene runs pip (ChunkScene.run_install) so it can show progress; the bookkeeping after it stays here, on the calling thread.

Parameters:

Name Type Description Default
packages

Requirement strings.

required
chunk_name str

The chunk that needs them, for the progress display.

''
Source code in usd_chunks/chunk_registry.py
def install(self, packages, chunk_name: str = "") -> bool:
    """pip-install `packages` into the dependency dir, now, without asking; whether pip succeeded.

    The scene runs pip (ChunkScene.run_install) so it can show progress; the bookkeeping
    after it stays here, on the calling thread.

    Args:
        packages: Requirement strings.
        chunk_name: The chunk that needs them, for the progress display.
    """
    if not self.initialized or not packages:
        return False
    packages = list(packages)
    target = [python_executable(), "-m", "pip", "install", "-t", self.package_dir]

    def run(on_line=None):
        # With -t, pip ignores what the running Python already has and installs every
        # dependency again: scipy brought a second numpy into the dependency dir, next
        # to Blender's own. So ask pip first what it would install with the host's
        # packages (and the dependency dir, on PYTHONPATH) counted, and install just
        # that, pinned, without dependencies. If that resolve fails -- an old pip with
        # no --report, say -- install the plain way.
        pins = _resolve_missing(packages, on_line)
        if pins is None:
            return _run_pip(target + packages, on_line)
        if not pins:
            return 0, ""
        return _run_pip(target + ["--no-deps"] + pins, on_line)

    scene = ChunkRegistry().scene
    returncode, output = scene.run_install(packages, chunk_name, run) if scene else run()

    # Only cache what pip actually installed, after checking its return code. A failed
    # install (an embedded interpreter shipping no pip, no network, a bad package name)
    # remembered as a success would short-circuit package_in_cache forever: never
    # retried, never prompted again, and the chunk erroring on every creation.
    if returncode != 0:
        if "No module named pip" in output:
            # usdview's bundled Python ships no pip. "pip exited 1" hid that, and the
            # line that said so sat under it, past where a pasted error usually ends.
            logger.error(
                "Failed to install chunk dependencies %s: %s has no pip. Add it with:\n"
                "    %s -m ensurepip\n"
                "then restart. They will be retried the next time a chunk requires them.",
                packages, python_executable(), python_executable(),
            )
            return False
        logger.error(
            "Failed to install chunk dependencies %s (pip exited %s). "
            "They will be retried the next time a chunk requires them.\n%s",
            packages, returncode, output.strip(),
        )
        return False

    if (pip_dir := self.package_dir) and pip_dir in sys.path_importer_cache:
        sys.path_importer_cache.pop(pip_dir)
    importlib.invalidate_caches()
    # A "no" given earlier no longer applies once the package is in.
    type(self)._declined.difference_update(packages)

    # By the package's name, not the requirement string: "numpy>=2,<3" is what a chunk
    # declares, and the cache is keyed the way `pip list` spells things. The version is
    # the one that is importable now that pip has run; None when that cannot be told,
    # which package_in_cache reads as satisfying whatever was asked for.
    for package in packages:
        name = split_requirement(package)[0]
        self._cache[name].add(installed_version(name))
    return True

install_later ⚓︎

install_later(packages, chunk_name='')

Whether the scene installs packages in the background; False with no scene.

Source code in usd_chunks/chunk_registry.py
def install_later(self, packages, chunk_name: str = "") -> bool:
    """Whether the scene installs `packages` in the background; False with no scene."""
    # getattr: a registry not initialized yet has no scene attribute at all.
    scene = getattr(ChunkRegistry(), "scene", None)
    if not scene or not self.initialized:
        return False
    install_later = getattr(scene, "install_later", None)
    return bool(install_later and install_later(list(packages), chunk_name))

install_packages ⚓︎

install_packages(requirements, chunk_name='')

Install whichever of these requirements are not already satisfied.

The scene is asked once for all of them (should_install_packages), naming the chunk that needs them; nothing is installed without its yes. With USD_CHUNK_AUTO_INSTALL set falsey, or a scene that cannot ask, the missing packages are named in a warning instead. With it set truthy, a scene that installs in the background (install_later) takes them over, and they count as missing until it has. Only what pip actually installed is cached.

Parameters:

Name Type Description Default
requirements

Requirement strings, ranges allowed.

required
chunk_name str

The chunk module that declares them, for the question and the warning.

''

Returns:

Type Description
list[str]

The requirements still not installed afterwards -- declined, failed, or never

list[str]

asked. Empty when the manager is not initialized, since it cannot tell.

Source code in usd_chunks/chunk_registry.py
def install_packages(self, requirements, chunk_name: str = "") -> list[str]:
    """Install whichever of these requirements are not already satisfied.

    The scene is asked once for all of them (should_install_packages), naming the chunk
    that needs them; nothing is installed without its yes. With USD_CHUNK_AUTO_INSTALL
    set falsey, or a scene that cannot ask, the missing packages are named in a warning
    instead. With it set truthy, a scene that installs in the background (install_later)
    takes them over, and they count as missing until it has. Only what pip actually
    installed is cached.

    Args:
        requirements: Requirement strings, ranges allowed.
        chunk_name: The chunk module that declares them, for the question and the warning.

    Returns:
        The requirements still not installed afterwards -- declined, failed, or never
        asked. Empty when the manager is not initialized, since it cannot tell.
    """
    if not self.initialized:
        return []

    missing = []

    for package in requirements:
        if self.package_in_cache(package):
            if self._still_installed(package):
                continue
            # The cache file outlived the package: removed by hand, or recorded by a
            # session that had another folder on sys.path. Trusted, it skipped the
            # install prompt and the chunk fell back or failed on import -- Blender's
            # Scatter Points ran its pure-Python path for minutes that way.
            self._forget(package)

        # One `pip list` per process, not one per chunk that misses. A package that is
        # genuinely absent stays absent for every chunk that wants it, and each of those
        # loads at once when a host generates its node classes -- Blender paid two
        # subprocesses per such chunk on the first open of a network. Installing through
        # this manager records what it installed, so nothing here goes stale short of an
        # install made by hand mid-session, which a restart picks up.
        if not self._listed:
            self.update_cache()
            type(self)._listed = True
            if self.package_in_cache(package):
                continue

        missing.append(package)

    if not missing:
        return []

    # Asked about once per process: a "no" stands until restart -- see _declined.
    askable = [p for p in missing if p not in self._declined]
    mode = _install_mode()
    later = []
    if askable and mode == "always" and self.install_later(askable, chunk_name):
        # The host installs them in the background and cooks the chunk again after.
        later = askable
        logger.info("Chunk %s needs %s: installing in the background",
                    chunk_name or "module", ", ".join(askable))
    elif askable and mode != "never" and (
        mode == "always" or self.should_install_packages(askable, chunk_name)
    ):
        if self.install(askable, chunk_name):
            missing = [p for p in missing if p not in askable]
    else:
        type(self)._declined.update(askable)

    if unhandled := [p for p in missing if p not in later]:
        # Say what is missing and how to get it, rather than failing later with an
        # import error from inside the chunk's module.
        quoted = " ".join(f'"{p}"' for p in unhandled)
        logger.warning(
            "Chunk %s needs %s, which is not installed. Install it with:\n"
            "    %s -m pip install -t \"%s\" %s",
            chunk_name or "module", ", ".join(unhandled), python_executable(), self.package_dir, quoted,
        )
    return missing

package_in_cache ⚓︎

package_in_cache(package)

Whether package -- a requirement, range and all -- is satisfied by the cache.

Reads the ranges chunks declare ("numpy>=2,<3"), not only "==". package_info splits on "==" alone, so a ranged requirement would come back with the whole string as its name, which no cache entry is ever keyed by -- every chunk declaring a range would miss the cache on every load, costing two pip list subprocesses each time.

Parameters:

Name Type Description Default
package

A requirement string.

required

Returns:

Type Description

True if a cached version satisfies it.

Source code in usd_chunks/chunk_registry.py
def package_in_cache(self, package):
    """Whether `package` -- a requirement, range and all -- is satisfied by the cache.

    Reads the ranges chunks declare ("numpy>=2,<3"), not only "==". package_info splits
    on "==" alone, so a ranged requirement would come back with the whole string as its
    name, which no cache entry is ever keyed by -- every chunk declaring a range would
    miss the cache on every load, costing two `pip list` subprocesses each time.

    Args:
        package: A requirement string.

    Returns:
        True if a cached version satisfies it.
    """
    if not self.initialized:
        return False

    name, specifier = split_requirement(package)
    wanted = _normalise_package(name)
    versions: set = set()
    for cached, pvs in self._cache.items():
        if _normalise_package(cached) == wanted:
            versions |= set(pvs)
    if not versions:
        return False
    return any(_satisfies(v, specifier) for v in versions)

package_info staticmethod ⚓︎

package_info(package)

Split an "==" requirement into (name, version); version is None without "==".

Parameters:

Name Type Description Default
package

A requirement string such as "numpy==2.1.0".

required

Returns:

Type Description

(name, version).

Source code in usd_chunks/chunk_registry.py
@staticmethod
def package_info(package):
    """Split an "==" requirement into (name, version); version is None without "==".

    Args:
        package: A requirement string such as "numpy==2.1.0".

    Returns:
        (name, version).
    """
    try:
        name, version = package.split("==")
    except ValueError:
        name = package
        version = None
    return name, version

should_install_packages ⚓︎

should_install_packages(packages, chunk_name='')

Whether the scene allows installing packages for chunk_name; False with no scene.

Source code in usd_chunks/chunk_registry.py
def should_install_packages(self, packages, chunk_name: str = "") -> bool:
    """Whether the scene allows installing `packages` for `chunk_name`; False with no scene."""
    reg = ChunkRegistry()
    if not reg.scene or not self.initialized:
        return False
    return reg.scene.should_install_packages(list(packages), chunk_name)

target_dir ⚓︎

target_dir()

Where install() puts packages -- for telling the user, before initialize() too.

Source code in usd_chunks/chunk_registry.py
def target_dir(self) -> str:
    """Where install() puts packages -- for telling the user, before initialize() too."""
    if getattr(self, "package_dir", None):
        return self.package_dir
    base = USD_CHUNK_DEPENDENCY_DIR()
    return os.path.join(base, f"py{sys.version_info[0]}{sys.version_info[1]}") if base else ""

update_cache ⚓︎

update_cache()

Record every installed package: what Python can import, then pip list on top.

Source code in usd_chunks/chunk_registry.py
def update_cache(self):
    """Record every installed package: what Python can import, then `pip list` on top."""
    if not self.initialized:
        return

    # What Python itself can see, everywhere on sys.path: site-packages, PYTHONPATH
    # folders, the dependency dir. Needs no pip. usdview's bundled Python ships without
    # pip, so `pip list` below finds nothing there -- and every requirement read as
    # missing, even mujoco and numpy that were on PYTHONPATH all along.
    for dist in distributions():
        if (name := dist.metadata["Name"]) and dist.version:
            self._cache[name].add(dist.version)

    # Search the current interpreters' environment
    args = [python_executable(), "-m", "pip", "list", "--format", "freeze"]

    proc = subprocess.run(
        args, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
        creationflags=_NO_WINDOW_FLAGS,
    )
    pip_packages = proc.stdout.decode().split()
    for p in pip_packages:
        pname, pversion = self.package_info(p)
        self._cache[pname].add(pversion)

    # Search the pip_dep_dir
    args = [python_executable(), "-m", "pip", "list", "--path", self.package_dir, "--format", "freeze"]

    proc = subprocess.run(
        args, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
        creationflags=_NO_WINDOW_FLAGS,
    )
    pip_packages = proc.stdout.decode().split()
    for p in pip_packages:
        pname, pversion = self.package_info(p)
        self._cache[pname].add(pversion)

ChunkEnvVar ⚓︎

ChunkEnvVar(key, default, relpath=False)

Bases: Generic[_T]

An environment variable the chunk system reads, with a typed default.

A list-typed variable is split on os.pathsep, and its entries come before the defaults.

Parameters:

Name Type Description Default
key str

The environment variable's name.

required
default _T

The value when the variable is unset or empty.

required
relpath

Resolve a string or list default relative to the repository root.

False
Source code in usd_chunks/chunk_registry.py
def __init__(self, key: str, default: _T, relpath=False):
    self.key = key
    self._default: _T = default
    self._relpath = relpath

default property ⚓︎

default

The default, resolved against the repository root when relpath is set.

__call__ ⚓︎

__call__(fallback=None, /, *args, **kwds)

The variable's current value.

A list variable returns the environment's entries first, then the defaults, keeping the first of any duplicate: env-provided dirs take precedence on duplicate module names, since the first one registered wins.

Parameters:

Name Type Description Default
fallback _T | None

Used in place of the environment value when the variable is unset.

None
*args Any

Ignored.

()
**kwds Any

Ignored.

{}

Returns:

Type Description

The value, or the default when the variable is unset or empty.

Source code in usd_chunks/chunk_registry.py
def __call__(self, fallback: _T | None = None, /, *args: Any, **kwds: Any):
    """The variable's current value.

    A list variable returns the environment's entries first, then the defaults, keeping the
    first of any duplicate: env-provided dirs take precedence on duplicate module names,
    since the first one registered wins.

    Args:
        fallback: Used in place of the environment value when the variable is unset.
        *args: Ignored.
        **kwds: Ignored.

    Returns:
        The value, or the default when the variable is unset or empty.
    """
    return self._get(self.default, fallback)

ChunkLayerManager ⚓︎

A singleton that creates, tracks and removes the layers chunks author into.

Every chunk is associated with a layer onto which the edits of its parameters are applied. The layers are created and deleted along with the chunks.

Attributes:

Name Type Description
chunk_layers dict[ChunkHash, Layer]

Maps each chunk to its layer

scene SceneSender

A handle to the scene for stage access

add_arc_layer ⚓︎

add_arc_layer(c, layer)

Hold layer as the one c made for an asset to payload or reference, or drop c's.

A Layer chunk set not to sublayer makes a layer nothing in the stage points at yet: held here it stays alive, and arc_layers lists it for an Edit Target to author into and a Payload or Reference chunk to bring in.

Parameters:

Name Type Description Default
c ChunkBase

The chunk that made the layer.

required
layer Layer | None

The layer, or None to drop the one c made.

required
Source code in usd_chunks/chunk_registry.py
def add_arc_layer(self, c: core.ChunkBase, layer: Sdf.Layer | None) -> None:
    """Hold layer as the one c made for an asset to payload or reference, or drop c's.

    A Layer chunk set not to sublayer makes a layer nothing in the stage points at yet:
    held here it stays alive, and arc_layers lists it for an Edit Target to author into
    and a Payload or Reference chunk to bring in.

    Args:
        c: The chunk that made the layer.
        layer: The layer, or None to drop the one c made.
    """
    if layer is None:
        self._arc_layers.pop(c.get_hash(), None)
    else:
        self._arc_layers[c.get_hash()] = layer

add_layer ⚓︎

add_layer(target_layer, c, index=0)

Add a layer to the stage and add a mapping from chunk c to the new layer.

Parameters:

Name Type Description Default
target_layer Layer

The target layer onto which we append the new layer

required
c ChunkUsdLayerSupport

The chunk that can modify the new layer. The new layer's identifier will be created from the chunks get_layer_identifier()

required
index int

Where in target_layer's subLayerPaths to insert the new layer

0

Returns:

Type Description
Layer

Sdf.Layer: A new layer

Source code in usd_chunks/chunk_registry.py
def add_layer(self, target_layer: Sdf.Layer, c: core.ChunkCommon, index: int = 0) -> Sdf.Layer:
    """Add a layer to the stage and add a mapping from chunk c to the new layer.

    Args:
        target_layer (Sdf.Layer): The target layer onto which we append the new layer
        c (ChunkUsdLayerSupport): The chunk that can modify the new layer. The new layer's identifier
            will be created from the chunks get_layer_identifier()
        index (int): Where in target_layer's subLayerPaths to insert the new layer

    Returns:
        Sdf.Layer: A new layer
    """
    h = c.get_hash()
    if layer := self.chunk_layers.get(h):
        return layer

    layer_id = c.get_layer_identifier()
    layer = Sdf.Layer.CreateAnonymous(layer_id)
    self.internal_layer_ids.add(layer.identifier)

    self.chunk_layers[h] = layer
    self._insert_sublayer(target_layer, index, layer.identifier)
    # An empty layer holds no result, so the chunk must cook before recook_for_view can
    # count on its layer. Covers a load, which makes every layer afresh for chunk objects
    # that may have cooked into the old ones.
    chunk_cook.mark_cook_stale(c)

    # We must make sure to delete the layer of the chunk AFTER all other notices are sent.
    # For this reason, we can't rely on callbacks. Instead we can register a finalizer, so we delete
    # the layer once the chunk is actually deleted
    weakref.finalize(c, self._chunk_deleted, h)

    return layer

add_outside_layer ⚓︎

add_outside_layer(c, layer, parent=None)

Record layer as one c sublayered into parent itself, outside its own layer -- or forget c's.

A Layer chunk placed below a file the target lists has its layer there, not in its own layer, so deleting the chunk does not take it with it: this manager takes it out of parent when it takes c's own layer away.

Parameters:

Name Type Description Default
c ChunkBase

The chunk that placed the layer.

required
layer Layer | None

The layer, or None to forget the one c placed.

required
parent Layer | None

The layer it is a sublayer of.

None
Source code in usd_chunks/chunk_registry.py
def add_outside_layer(self, c: core.ChunkBase, layer: Sdf.Layer | None, parent: Sdf.Layer | None = None) -> None:
    """Record layer as one c sublayered into parent itself, outside its own layer -- or forget c's.

    A Layer chunk placed below a file the target lists has its layer there, not in its
    own layer, so deleting the chunk does not take it with it: this manager takes it out
    of parent when it takes c's own layer away.

    Args:
        c: The chunk that placed the layer.
        layer: The layer, or None to forget the one c placed.
        parent: The layer it is a sublayer of.
    """
    if layer is None or parent is None:
        self._outside_layers.pop(c.get_hash(), None)
    else:
        self._outside_layers[c.get_hash()] = (layer, parent)

arc_layers ⚓︎

arc_layers()

(display name, layer) for every layer a chunk made to be brought in by arc.

Source code in usd_chunks/chunk_registry.py
def arc_layers(self) -> list[tuple[str, Sdf.Layer]]:
    """(display name, layer) for every layer a chunk made to be brought in by arc."""
    return [(layer.GetDisplayName(), layer) for layer in self._arc_layers.values()]

batched_placement ⚓︎

batched_placement()

Collect graph-layer sublayer edits made inside, and write each list once at the end.

Only graph layers are batched, those there already and those made meanwhile: they are this manager's own and hold no layer offsets, which a whole-list write would not keep. The layers they sit under are edited as before.

Source code in usd_chunks/chunk_registry.py
@contextlib.contextmanager
def batched_placement(self):
    """Collect graph-layer sublayer edits made inside, and write each list once at the end.

    Only graph layers are batched, those there already and those made meanwhile: they are
    this manager's own and hold no layer offsets, which a whole-list write would not
    keep. The layers they sit under are edited as before.
    """
    if self._sublayer_batch is not None:
        yield
        return
    self._sublayer_batch = {}
    self._batchable = {g.identifier for g in self.graph_layers.values()}
    try:
        yield
    finally:
        batch, self._sublayer_batch = self._sublayer_batch, None
        self._batchable = set()
        with self._internal_sublayer_edit():
            for layer, paths in batch.values():
                if list(layer.subLayerPaths) != paths:
                    layer.subLayerPaths = paths

clear_layer ⚓︎

clear_layer(layer_id)

Clear a layer's contents.

Parameters:

Name Type Description Default
layer_id str | ChunkBase

The layer identifier, or the chunk whose layer to clear

required
Source code in usd_chunks/chunk_registry.py
def clear_layer(self, layer_id: str | core.ChunkBase):
    """Clear a layer's contents.

    Args:
        layer_id (str | ChunkBase): The layer identifier, or the chunk whose layer to clear
    """
    if isinstance(layer_id, str):
        layer = Sdf.Layer.Find(layer_id)
    elif _layer_id := self.get_layer(layer_id):
        layer = Sdf.Layer.Find(_layer_id.identifier)
    else:
        return
    layer.Clear()

cook_layer_for ⚓︎

cook_layer_for(home)

The layer cooks of chunks defined in home nest under: home's cook sublayer.

layout.usda lists layout.cook.usda (chunk_layers.find_cook_sublayer), so the cook sits just under the layer that defines it: stronger layers -- lighting's, authored or cooked -- beat it, and so do home's own hand edits. The graph layers go under it in memory; neither file is written, so both may be locked.

Opt-in by the link: a home listing none is never given one here, since that would dirty a file merely by opening it -- its chunks cook into the session layer as before. Whoever owns the file adds the link (chunk_layers.add_cook_sublayer).

Parameters:

Name Type Description Default
home Layer

A file layer that defines chunks.

required

Returns:

Type Description
Layer | None

The cook layer, internal to the chunk system (is_chunk_internal_layer), or None

Layer | None

when home lists none.

Source code in usd_chunks/chunk_registry.py
def cook_layer_for(self, home: Sdf.Layer) -> Sdf.Layer | None:
    """The layer cooks of chunks defined in home nest under: home's cook sublayer.

    layout.usda lists layout.cook.usda (chunk_layers.find_cook_sublayer), so the cook
    sits just under the layer that defines it: stronger layers -- lighting's, authored or
    cooked -- beat it, and so do home's own hand edits. The graph layers go under it in
    memory; neither file is written, so both may be locked.

    Opt-in by the link: a home listing none is never given one here, since that would
    dirty a file merely by opening it -- its chunks cook into the session layer as
    before. Whoever owns the file adds the link (chunk_layers.add_cook_sublayer).

    Args:
        home: A file layer that defines chunks.

    Returns:
        The cook layer, internal to the chunk system (is_chunk_internal_layer), or None
        when home lists none.
    """
    if (cook := self._cook_layers.get(home.identifier)) is not None:
        return cook
    if (cook := chunk_layers.find_cook_sublayer(home)) is None:
        return None
    # From here on the live cooks are the result: the bake saved in the file was the
    # last one, and left in place it would compose under them -- a prim a cook no
    # longer makes would linger. Cleared in memory only; save_cook_layers writes the
    # next bake. What the file records about itself stays.
    with self._internal_sublayer_edit():
        data = dict(cook.customLayerData)
        cook.Clear()
        cook.customLayerData = data
    self.internal_layer_ids.add(cook.identifier)
    self._cook_layers[home.identifier] = cook
    return cook

deactivate_stale_bakes ⚓︎

deactivate_stale_bakes()

Hide the prims an asset's published bake makes that this stage's live cooks don't.

A shot cooks an asset's chunks into its own cook layer and never touches the asset, so the bake the asset was published with still composes underneath -- with every prim that network made then. A prim the live cook no longer makes would linger. So for each copy of an asset whose chunks have all cooked live here, every prim that only the asset's bake defines gets an over with active = false, the topmost one of a subtree only, in a layer under this stage's cook layer. It is written into the shot's bake with the rest (save_cook_layers), so a consumer without usd_chunks sees the same.

A copy whose chunks have not all cooked -- disabled, blocked outside the active view, never cooked -- keeps its bake whole: hiding it would leave the bare asset.

Incremental, for large assets. A copy that turns live is walked once in full. After that only the bake subtrees at paths whose definition changed (_after_cook) are walked again, and only the overs that change are written, so a prim that stays hidden is never recomposed.

Source code in usd_chunks/chunk_registry.py
def deactivate_stale_bakes(self) -> None:
    """Hide the prims an asset's published bake makes that this stage's live cooks don't.

    A shot cooks an asset's chunks into its own cook layer and never touches the asset,
    so the bake the asset was published with still composes underneath -- with every prim
    that network made then. A prim the live cook no longer makes would linger. So for
    each copy of an asset whose chunks have all cooked live here, every prim that only
    the asset's bake defines gets an `over` with `active = false`, the topmost one of a
    subtree only, in a layer under this stage's cook layer. It is written into the shot's
    bake with the rest (save_cook_layers), so a consumer without usd_chunks sees the same.

    A copy whose chunks have not all cooked -- disabled, blocked outside the active view,
    never cooked -- keeps its bake whole: hiding it would leave the bare asset.

    Incremental, for large assets. A copy that turns live is walked once in full. After
    that only the bake subtrees at paths whose definition changed (_after_cook) are
    walked again, and only the overs that change are written, so a prim that stays
    hidden is never recomposed.
    """
    if not (self.initialized and self.stage):
        return
    copies = self._live_copies()
    dirty, self._dirty_paths = self._dirty_paths, set()
    drop: dict[tuple, set] = {}
    walk: dict[tuple, list] = {}
    for key, state in self._copies.items():
        if key not in copies:
            drop[key] = set(state["hidden"])
    for key, (cook, bake, map_fn, live) in copies.items():
        state = self._copies.setdefault(
            key, {"cook": cook, "bake": bake, "map": map_fn, "live": False, "hidden": set()})
        hidden = state["hidden"]
        if not live:
            drop[key] = set(hidden)
        elif not state["live"]:
            drop[key] = set(hidden)
            walk[key] = [Sdf.Path.absoluteRootPath]
        else:
            drop[key], walk[key] = self._dirty_roots(hidden, dirty, map_fn, bake)
        state["live"] = live
    with self._internal_sublayer_edit():
        with Sdf.ChangeBlock():
            for key, paths in drop.items():
                if not paths:
                    continue
                state = self._copies[key]
                state["hidden"] -= paths
                if (entry := self._deactivations.get(state["cook"].identifier)) is not None:
                    for path in paths:
                        if (spec := entry[1].GetPrimAtPath(path)) is not None:
                            spec.ClearInfo("active")
                    entry[1].RemoveInertSceneDescription()
        for key in list(drop):
            if key not in copies:
                del self._copies[key]
        found = {key: self._only_in_bake(self._copies[key], roots)
                 for key, roots in walk.items() if roots}
        with Sdf.ChangeBlock():
            for key, paths in found.items():
                if not paths:
                    continue
                state = self._copies[key]
                state["hidden"] |= paths
                overs = self._overs_layer(state["cook"])
                for path in paths:
                    Sdf.CreatePrimInLayer(overs, path).active = False

defer_new ⚓︎

defer_new(c)

Leave a chunk with no layer yet without one until placement, while deferring.

A new chunk asks for its layer as it is added, only to clear it -- and a layer made then went on the stage at once, in the loading layer: an edit that recomposes the whole layer stack, per chunk. Made at placement instead, with all the others, it goes straight into its graph layer, written once.

Returns:

Name Type Description
bool bool

True if c waits for placement; False when not deferring, or c has a layer.

Source code in usd_chunks/chunk_registry.py
def defer_new(self, c: core.ChunkCommon) -> bool:
    """Leave a chunk with no layer yet without one until placement, while deferring.

    A new chunk asks for its layer as it is added, only to clear it -- and a layer made
    then went on the stage at once, in the loading layer: an edit that recomposes the
    whole layer stack, per chunk. Made at placement instead, with all the others, it goes
    straight into its graph layer, written once.

    Returns:
        bool: True if c waits for placement; False when not deferring, or c has a layer.
    """
    if self._deferred_placement is None or self.get_layer(c) is not None:
        return False
    self._deferred_placement[c.get_hash()] = weakref.ref(c)
    return True

defer_placement ⚓︎

defer_placement()

Hold chunk layers made from now on in one loading layer, until the graph is wired.

A load restores each chunk's inputs before it restores the wiring between chunks, and restoring an input asks for the chunk's layer. Placed then, every chunk looks like a graph of its own: it got a graph layer of its own on the stage, and the wiring moved it into the shared one a moment later and threw that graph layer away. Each of those sublayer edits recomposes the stage -- a fifth of opening a 70-chunk network.

The held layers stay on the stage, in one loading layer, so a held layer behaves as a placed one does. Off the stage it would not: Usd.EditContext raises for a layer outside the stage's layer stack, and chunk_notice declines to cook a chunk whose layer is not in it yet. A first version kept them off the stage, and that skipped a cook the load used to run -- the same outcome, reached by accident, that a vector's premature cook no longer has in the first place (see _set_children_blocked).

Returns:

Name Type Description
bool bool

True if this call turned deferral on. Only that caller calls

bool

place_deferred_layers; a nested caller gets False and leaves it to the outer one.

Source code in usd_chunks/chunk_registry.py
def defer_placement(self) -> bool:
    """Hold chunk layers made from now on in one loading layer, until the graph is wired.

    A load restores each chunk's inputs before it restores the wiring between chunks, and
    restoring an input asks for the chunk's layer. Placed then, every chunk looks like a
    graph of its own: it got a graph layer of its own on the stage, and the wiring moved
    it into the shared one a moment later and threw that graph layer away. Each of those
    sublayer edits recomposes the stage -- a fifth of opening a 70-chunk network.

    The held layers stay on the stage, in one loading layer, so a held layer behaves as a
    placed one does. Off the stage it would not: Usd.EditContext raises for a layer
    outside the stage's layer stack, and chunk_notice declines to cook a chunk whose
    layer is not in it yet. A first version kept them off the stage, and that skipped a
    cook the load used to run -- the same outcome, reached by accident, that a vector's
    premature cook no longer has in the first place (see _set_children_blocked).

    Returns:
        bool: True if this call turned deferral on. Only that caller calls
        place_deferred_layers; a nested caller gets False and leaves it to the outer one.
    """
    if self._deferred_placement is not None:
        return False
    self._deferred_placement = {}
    return True

end_deferral ⚓︎

end_deferral()

Stop deferring, and return the chunks whose layer is still waiting to be placed.

The loading layer comes off the stage, so the returned chunks' layers are off it until they are placed. The caller places them, in the order it wants: the order layers go onto a graph layer breaks ties between chunks at the same depth, and a load keeps the order it had before placement was deferred.

Source code in usd_chunks/chunk_registry.py
def end_deferral(self) -> list[core.ChunkCommon]:
    """Stop deferring, and return the chunks whose layer is still waiting to be placed.

    The loading layer comes off the stage, so the returned chunks' layers are off it
    until they are placed. The caller places them, in the order it wants: the order
    layers go onto a graph layer breaks ties between chunks at the same depth, and a
    load keeps the order it had before placement was deferred.
    """
    pending, self._deferred_placement = self._deferred_placement, None
    if (loading := self._loading_layer) is not None:
        self._loading_layer = None
        self.internal_layer_ids.discard(loading.identifier)
        with self._internal_sublayer_edit():
            session = self.stage.GetSessionLayer() if self.stage else None
            if session and loading.identifier in session.subLayerPaths:
                session.subLayerPaths.remove(loading.identifier)
            loading.subLayerPaths = []
    return [c for ref in (pending or {}).values() if (c := ref()) is not None]

follow_merged_graphs ⚓︎

follow_merged_graphs(chunks)

Move each chunk's layer into its graph's layer when a wire merged its graph into another.

A layer moves on its chunk's next get_or_create_layer, which a cook asks for. The chunk above a new wire doesn't cook again, so ranking calls this first, or that chunk's layer stays in its old graph layer where the ranking can't place it.

Parameters:

Name Type Description Default
chunks Iterable[ChunkCommon]

The chunks about to be ranked.

required
Source code in usd_chunks/chunk_registry.py
def follow_merged_graphs(self, chunks: Iterable[core.ChunkCommon]) -> None:
    """Move each chunk's layer into its graph's layer when a wire merged its graph into another.

    A layer moves on its chunk's next get_or_create_layer, which a cook asks for. The chunk
    above a new wire doesn't cook again, so ranking calls this first, or that chunk's layer
    stays in its old graph layer where the ranking can't place it.

    Args:
        chunks: The chunks about to be ranked.
    """
    moved = [c for c in chunks
             if (key := self._chunk_graph_key.get(c.get_hash())) is not None
             and key[0] != c.get_dag_node().get_graph_id()]
    # One at a time, not batched_placement: that writes the old graph layer's list before
    # the new one's, and a layer sublayered under the moving one (a Layer chunk's) leaves
    # the stack in between, which resets an Edit Target on it.
    for c in moved:
        self.get_or_create_layer(c)

get_chunk_target ⚓︎

get_chunk_target(c)

The real layer this chunk's content last resolved against.

That is the session layer by default, or an Edit-Target-chosen layer, as tracked by get_or_create_layer. Export tooling uses it to group a chunk's own bookkeeping prim (always in the session layer) with whichever real layer its driven content nests in.

Parameters:

Name Type Description Default
c ChunkHash

The chunk.

required

Returns:

Type Description
Layer | None

The layer, or None if the chunk has none yet.

Source code in usd_chunks/chunk_registry.py
def get_chunk_target(self, c: core.ChunkHash) -> Sdf.Layer | None:
    """The real layer this chunk's content last resolved against.

    That is the session layer by default, or an Edit-Target-chosen layer, as tracked by
    get_or_create_layer. Export tooling uses it to group a chunk's own bookkeeping prim
    (always in the session layer) with whichever real layer its driven content nests in.

    Args:
        c: The chunk.

    Returns:
        The layer, or None if the chunk has none yet.
    """
    return self._chunk_target.get(c)

get_layer ⚓︎

get_layer(c)

Get the layer associated with a chunk.

Parameters:

Name Type Description Default
c ChunkHash

The chunk whose layer to get

required

Returns:

Type Description
Layer | None

Sdf.Layer | None: The chunk's layer, or None if it has none

Source code in usd_chunks/chunk_registry.py
def get_layer(self, c: core.ChunkHash) -> Sdf.Layer | None:
    """Get the layer associated with a chunk.

    Args:
        c (ChunkHash): The chunk whose layer to get

    Returns:
        Sdf.Layer | None: The chunk's layer, or None if it has none
    """
    return self.chunk_layers.get(c)

get_layer_file_name ⚓︎

get_layer_file_name(layer)

The file name a layer is saved under: its display name without the unique suffix.

Source code in usd_chunks/chunk_registry.py
def get_layer_file_name(self, layer: Sdf.Layer):
    """The file name a layer is saved under: its display name without the unique suffix."""
    return "_".join(layer.GetDisplayName().split("_")[:-1]) + ".usda"

get_or_create_layer ⚓︎

get_or_create_layer(c)

Get a chunk's layer as get_layer() does, creating it if it does not exist yet.

Parameters:

Name Type Description Default
c ChunkCommon

The chunk whose layer to get

required

Returns:

Type Description
Layer | None

Sdf.Layer | None: The layer, or None when there is no stage or the chunk has been

Layer | None

deleted

Source code in usd_chunks/chunk_registry.py
def get_or_create_layer(self, c: core.ChunkCommon) -> Sdf.Layer | None:
    """Get a chunk's layer as get_layer() does, creating it if it does not exist yet.

    Args:
        c (ChunkCommon): The chunk whose layer to get

    Returns:
        Sdf.Layer | None: The layer, or None when there is no stage or the chunk has been
        deleted
    """
    if not self.stage:
        return

    # A deleted chunk never gets a layer back -- see _chunk_deleted for why re-creating
    # one mid-teardown is what corrupts the graph layer's sublayer list.
    if c.get_hash() in self._retired_chunks:
        return

    existing = self.chunk_layers.get(c.get_hash())
    if existing and not existing.anonymous:
        return existing

    owner = c.get_owner()
    while owner and not isinstance(owner, core.ChunkBase):
        owner = owner.get_owner()
    if isinstance(owner, core.ChunkBase) and owner.has_flag(ChunkFlags.GroupFlags.CONTEXT):
        ctx_layer = self.chunk_layers.get(owner.get_hash())
        if ctx_layer and not ctx_layer.anonymous:
            return ctx_layer

    # While a load is still wiring its chunks, the layer is made in the loading layer;
    # place_deferred_layers puts it where the finished wiring says. See defer_placement.
    if self._deferred_placement is not None:
        self._deferred_placement[c.get_hash()] = weakref.ref(c)
        if layer := self.get_layer(c):
            return layer
        # A chunk layer moving into the stack, like the placed ones below: without the
        # guard the stage sync took each hold for a real layer stack change and dropped
        # what it remembers of the scene, every chunk.
        with self._internal_sublayer_edit():
            return self.add_layer(self._holding_layer(), c)

    # Resolve where this chunk's content should nest: the user's chosen Edit Target,
    # or the session layer by default -- the same resolution chunk-prim redirect
    # opinions already follow (chunk_utils.get_edit_layer). CONTEXT chunks (Tab-like
    # containers) are structural scaffolding, not content -- they always stay pinned
    # to the session layer regardless of Edit Target, same as chunk-prim bookkeeping;
    # only a regular content-producing chunk's own driven data should move.
    if isinstance(c, core.ChunkBase) and c.has_flag(ChunkFlags.GroupFlags.CONTEXT):
        target = self.stage.GetSessionLayer()
    else:
        # Pass c as the owner so the target is resolved from c's own exec-chain position
        # (nearest upstream Edit Target), not from whichever chunk executed last.
        owner_chunk = c if isinstance(c, core.ChunkBase) else None
        target = chunk_utils.get_edit_layer(self.stage, owner_chunk)
        # No Edit Target upstream: the cook belongs with the layer that defines the chunk,
        # just under it, not in the session layer over everything -- a layout procedural
        # must not beat what lighting authors by hand.
        if target == self.stage.GetSessionLayer() and (home := self._home_layer(c)):
            target = self.cook_layer_for(home) or target
    self._chunk_target[c.get_hash()] = target
    self._placed[c.get_hash()] = weakref.ref(c)

    # Create a graph layer - these are used for organising the layers. When chunks are part of
    # the same graph AND currently resolve to the same target, they become siblings in the
    # same graph layer (a graph_layer can only ever be a sublayer of one real layer at a time,
    # so chunks split into separate groups per target they're actually authored against).
    graph_id = c.get_dag_node().get_graph_id()
    graph_key = (graph_id, target.identifier)

    # Every subLayerPaths edit below is a plain Sdf write that fires StageContentsChanged
    # like any other stage edit. Without this guard, one can synchronously re-enter this
    # same method (via ChunkStageSync._on_contents_changed -> load_chunks, the same
    # reentrancy class documented on _chunk_deleted's own guard below) for a sibling chunk
    # targeting the same graph_key, before this call's own bookkeeping/insert has settled
    # -- seen live in CI as a genuine, if intermittent, "Duplicate item ... not allowed for
    # field 'subLayers'" Sdf error on the exact same graph layer being inserted twice.
    prev_w = chunk_notice._usd_write_active
    prev_internal = chunk_notice._internal_layer_write_active
    chunk_notice._usd_write_active = True
    # Everything sublayered below is a graph layer or a chunk's own procedural layer, so
    # none of it can change which Edit Target a chunk resolves to -- see the flag's own
    # comment. Set alongside _usd_write_active because the Edit Target memo is dropped
    # ahead of that flag's bail-out, deliberately: an ordinary suppressed sublayer edit
    # still moves a real layer in or out of the stack, and this one does not.
    chunk_notice._internal_layer_write_active = True
    try:
        if not (parent := self.graph_layers.get(graph_key)):
            parent = Sdf.Layer.CreateAnonymous(f"graph_{graph_id}")
            self.internal_layer_ids.add(parent.identifier)
            self.graph_layers[graph_key] = parent
            self._graph_layer_target[graph_key] = target
            if self._sublayer_batch is not None:
                self._batchable.add(parent.identifier)
            # Insert at the top so this graph's layers win over other sublayers already on
            # the target -- e.g. an RBD bake must beat the prim's existing xformOp:transform.
            # Checked rather than unconditional: a freshly minted anon layer's identifier
            # can still collide with one already sitting in target.subLayerPaths -- e.g. a
            # stale reference left over from before a reload (anon identifiers are reused
            # once the process's underlying slot is freed), which USD's Sdf list editor
            # rejects outright as a duplicate ("Duplicate item ... not allowed for field
            # 'subLayers'") rather than silently no-opping.
            if parent.identifier not in target.subLayerPaths:
                target.subLayerPaths.insert(0, parent.identifier)
        else:
            prev_target = self._graph_layer_target.get(graph_key)
            if prev_target and prev_target.identifier != target.identifier:
                # The resolved Edit Target changed since this graph layer was created (e.g.
                # an upstream Edit Target chunk's selection changed) -- reparent rather than
                # leave it sublayered under a now-stale target.
                if parent.identifier in prev_target.subLayerPaths:
                    prev_target.subLayerPaths.remove(parent.identifier)
                self._graph_layer_target[graph_key] = target
            if parent.identifier not in target.subLayerPaths:
                target.subLayerPaths.insert(0, parent.identifier)

        def insert_index() -> int:
            # The sort_key is used to determine the order of the chunk sublayers
            count = len(self._sublayers(parent))
            index = chunk_scheduler.layer_sort_key(c.get_dag_node())
            if index > count - 1:
                index = count
            # Reverse the order
            return count - index

        layer = self.get_layer(c)
        chunk_hash = c.get_hash()

        if not layer:
            if self.is_muted(c):
                return
            layer = self.add_layer(parent, c, insert_index())
        elif self._confirmed_in.get(chunk_hash) != parent.identifier:
            # Only when this manager has not already seen the layer in this parent: every
            # read of subLayerPaths copies the whole list out of C++, a graph layer holds a
            # layer per chunk, and this runs on every get_edit_context -- so checking each
            # time made every call cost the size of the graph. Only this manager moves a
            # chunk layer between graph layers, and each move goes through here.
            if layer.identifier not in self._sublayers(parent):
                self._insert_sublayer(parent, insert_index(), layer.identifier)
        self._confirmed_in[chunk_hash] = parent.identifier

        # If a chunk connection changed after the chunk layer was created, the layer may have been
        # added to a new graph. This can be cleaned up as to not sublayer the chunk layer twice.
        # Only inspect pl for emptiness right after we've just removed its child above -- never as
        # a blanket sweep over every tracked graph layer, since a graph layer can legitimately be
        # momentarily empty while it's still being constructed (its own first leaf not yet added).
        #
        # Looks only at the graph layers' own direct sublayers, not the whole composed
        # hierarchy iter_chunk_layers() walks. A chunk leaf layer is only ever inserted as a
        # direct sublayer of a graph layer (add_layer), so those are the only pairs that could
        # ever match here -- the deeper levels of that walk are a chunk layer's own imports
        # (a Sublayer chunk's files), which are never this layer. The full walk resolves every
        # sublayer path through Sdf.Find on every call, and this runs on every single
        # get_edit_context: it was ~30% of the cost of opening a saved graph.
        #
        # And only when this chunk's graph_key differs from the one its layer was last
        # placed under: the layer can only be sitting in another graph layer if this
        # function put it there, under the key it had then. Every subLayerPaths read is a
        # C++ list copy, and this loop was two thirds of the call on a warm cache, on a
        # call made ~50 times per chunk over a scene open.
        chunk_hash = c.get_hash()
        moved = self._chunk_graph_key.get(chunk_hash) != graph_key
        self._chunk_graph_key[chunk_hash] = graph_key
        for pl in list(self.graph_layers.values()) if moved else ():
            if pl.identifier != parent.identifier and layer.identifier in self._sublayers(pl):
                self._remove_sublayer(pl, layer.identifier)
                if len(self._sublayers(pl)) == 0:
                    for gkey, g_layer in list(self.graph_layers.items()):
                        if g_layer.identifier != pl.identifier:
                            continue
                        gtarget = self._graph_layer_target.pop(gkey, None)
                        if gtarget and pl.identifier in gtarget.subLayerPaths:
                            gtarget.subLayerPaths.remove(pl.identifier)
                        self.graph_layers.pop(gkey, None)
                        self.internal_layer_ids.discard(pl.identifier)
                        # Its address can be handed to a new layer, identifier and all.
                        self._confirmed_in = {
                            h: ident for h, ident in self._confirmed_in.items()
                            if ident != pl.identifier
                        }
                        break
    finally:
        chunk_notice._usd_write_active = prev_w
        chunk_notice._internal_layer_write_active = prev_internal

    return layer

holding_deletes ⚓︎

holding_deletes()

Take the layers of chunks deleted inside this block off the stage together, at its end.

Each layer taken off on its own recomposes the whole stage, twice with the mute, so deleting every chunk of a big network one by one cost the square of its size -- most of the time closing a 900-chunk scene took. Nested blocks join the outermost.

Source code in usd_chunks/chunk_registry.py
@contextlib.contextmanager
def holding_deletes(self):
    """Take the layers of chunks deleted inside this block off the stage together, at its end.

    Each layer taken off on its own recomposes the whole stage, twice with the mute, so
    deleting every chunk of a big network one by one cost the square of its size -- most
    of the time closing a 900-chunk scene took. Nested blocks join the outermost.
    """
    if self._held_deletes is not None:
        yield
        return
    self._held_deletes = []
    try:
        yield
    finally:
        held, self._held_deletes = self._held_deletes, None
        if held:
            self._remove_layers(held)

initialize ⚓︎

initialize(stage)

Set up the manager for a stage. Call at least once before using it.

Parameters:

Name Type Description Default
stage Stage

The stage to manage

required
Source code in usd_chunks/chunk_registry.py
def initialize(self, stage: Usd.Stage):
    """Set up the manager for a stage. Call at least once before using it.

    Args:
        stage (Usd.Stage): The stage to manage
    """
    self._edit_target_listener = NoticeConnection(
        self._edit_target_changed, ChunkNoticeAlias.STAGE_EDIT_TARGET_CHANGED, globally=True
    )

    self._flags_listener = NoticeConnection(self._flags_changed, ChunkNoticeAlias.STATE_CHANGED, globally=True)

    self._deleted_listener = NoticeConnection(
        self._on_chunk_deleted_notice, ChunkNoticeAlias.CHUNK_DELETED, globally=True
    )

    self.chunk_layers: dict[core.ChunkHash, Sdf.Layer] = {}
    # Keyed by (graph_id, target_layer_identifier) -- a graph's chunks get split into a
    # separate sibling group per Edit Target they're actually authored against, since a
    # single graph_layer can only ever be a sublayer of one target at a time.
    self.graph_layers: dict[tuple, Sdf.Layer] = {}
    # Which real layer each graph_layer entry is currently sublayered under -- needed to
    # detect and reparent when the resolved Edit Target changes between calls.
    self._graph_layer_target: dict[tuple, Sdf.Layer] = {}
    # Which real layer each individual chunk's content currently resolves against (same
    # value as its graph_layer's entry above, but keyed per-chunk for easy lookup) -- lets
    # export tooling group a chunk's own bookkeeping prim with whichever layer its driven
    # content nests in (see chunks/core/export_usd.py's per-layer export mode).
    self._chunk_target: dict[core.ChunkHash, Sdf.Layer] = {}
    # The graph_key each chunk's layer was last placed under. get_or_create_layer only
    # has to look for that layer in OTHER graph layers when this has changed since --
    # and it is asked several times per chunk per cook, nearly always with nothing moved.
    self._chunk_graph_key: dict[core.ChunkHash, tuple] = {}
    # Hashes of chunks whose layer has been torn down. Never served a layer again, so a
    # notice fired by the teardown itself can't resurrect one -- see _chunk_deleted.
    self._retired_chunks: set = set()
    # Chunk hash -> identifier of the graph layer get_or_create_layer last saw its layer in.
    self._confirmed_in: dict[core.ChunkHash, str] = {}
    # Layers of chunks deleted inside holding_deletes, taken off the stage at its end.
    self._held_deletes: list[Sdf.Layer] | None = None
    # Chunks whose layer exists but waits to be placed, while defer_placement is on; None
    # when it is off.
    self._deferred_placement: dict[core.ChunkHash, weakref.ReferenceType] | None = None
    # Where those layers sit meanwhile; see defer_placement.
    self._loading_layer: Sdf.Layer | None = None
    # Graph layer identifier -> (layer, its sublayer list as edited so far), while
    # place_deferred_layers writes each graph layer once; None otherwise.
    self._sublayer_batch: dict[str, tuple[Sdf.Layer, list[str]]] | None = None
    # The graph layers that batch may write: this manager's own, which hold no offsets.
    self._batchable: set[str] = set()
    # The cook layer of each file layer that defines chunks, by that layer's identifier;
    # see cook_layer_for.
    self._cook_layers: dict[str, Sdf.Layer] = {}
    # Per cook layer, the layer of `active = false` overs that hides what an asset's
    # published bake makes and this stage's live cooks no longer do; see
    # deactivate_stale_bakes. Taken out of their cook layers before being dropped: a cook
    # layer is a file layer that outlives this, and an anonymous identifier is reused once
    # its layer is freed -- a stale entry would then name an unrelated layer.
    self._drop_deactivations()
    self._deactivations: dict[str, tuple[Sdf.Layer, Sdf.Layer]] = {}
    chunk_cook.after_cook(self._after_cook)
    # Every chunk given a layer, for save_cook_layers to find the ones a bake holds.
    self._placed: dict[core.ChunkHash, weakref.ReferenceType] = {}
    # Per chunk, a layer it made for an asset to bring in by payload or reference rather
    # than sublayer; see add_arc_layer. Held here: nothing else keeps it alive until an
    # arc composes it.
    self._arc_layers: dict[core.ChunkHash, Sdf.Layer] = {}
    # Per chunk, a layer it put in a real layer's sublayers itself, outside its own
    # layer, with that parent; see add_outside_layer. Taken out when the chunk goes.
    self._outside_layers: dict[core.ChunkHash, tuple[Sdf.Layer, Sdf.Layer]] = {}
    self._chunk_refs = weakref.WeakSet()
    # Identifiers of every layer above that this manager created, so is_chunk_internal_layer
    # can answer in one lookup. Maintained beside the two dicts rather than derived from
    # them on demand, and dropped with them, because an anonymous identifier embeds an
    # address and addresses get recycled: a leftover entry would eventually claim an
    # unrelated layer.
    self.internal_layer_ids: set = set()

    # When initialized, the stage may be None as the chunks are loaded prior to loading the stage
    if getattr(self, "stage", None) is not stage:
        skip_cook.release_cook_stage()  # kept for the stage this manager had before
    self.stage: Usd.Stage = stage

    self.initialized = True

is_muted ⚓︎

is_muted(c)

Check if a chunk's layer is muted.

A chunk layer can be muted stage-scoped (the disable path) or at the Sdf level (node graph view slots); either counts.

Parameters:

Name Type Description Default
c ChunkHash

The chunk whose layer to check

required

Returns:

Name Type Description
bool bool

True when the layer is muted, False otherwise

Source code in usd_chunks/chunk_registry.py
def is_muted(self, c: core.ChunkHash) -> bool:
    """Check if a chunk's layer is muted.

    A chunk layer can be muted stage-scoped (the disable path) or at the Sdf level (node
    graph view slots); either counts.

    Args:
        c (ChunkHash): The chunk whose layer to check

    Returns:
        bool: True when the layer is muted, False otherwise
    """
    layer = self.chunk_layers.get(c)
    if not (self.stage and layer):
        return False
    # A chunk layer can be muted stage-scoped (disable path) or Sdf-level (node graph
    # view slots); treat either as muted.
    return self.stage.IsLayerMuted(layer.identifier) or layer.IsMuted()

iter_chunk_layers ⚓︎

iter_chunk_layers()

Every tracked graph layer and chunk layer, with the layer it sits under.

Covers every graph layer regardless of which real layer (the session layer, or an Edit-Target-chosen sublayer) it is attached under.

Yields:

Type Description
tuple[Layer, Layer]

(parent, sublayer) pairs.

Source code in usd_chunks/chunk_registry.py
def iter_chunk_layers(self) -> Generator[tuple[Sdf.Layer, Sdf.Layer], None, None]:
    """Every tracked graph layer and chunk layer, with the layer it sits under.

    Covers every graph layer regardless of which real layer (the session layer, or an
    Edit-Target-chosen sublayer) it is attached under.

    Yields:
        (parent, sublayer) pairs.
    """
    # Don't pre-mark g_layer as visited here -- _iter_sublayers does that itself as
    # its first action, and would otherwise immediately bail (thinking it's already
    # walked) before ever descending into g_layer's own chunk leaf layers.
    visited: set = set()
    for gkey, g_layer in list(self.graph_layers.items()):
        target = self._graph_layer_target.get(gkey)
        if target:
            yield (target, g_layer)
        yield from self._iter_sublayers(g_layer, visited)

place_deferred_layers ⚓︎

place_deferred_layers()

Put every layer held back by defer_placement on the stage, and stop deferring.

Each graph layer's sublayer list is written once, for all its chunks: every write to a list on the stage recomposes the whole layer stack, so placing a layer per write made a network of n chunks cost n recomposes of up to n layers each.

Each chunk's Edit Target is resolved first, while every held layer is still on the stage: an Edit Target names a layer, and counts only while that layer is composed. Asked during placement, a layer a Layer chunk made was in no stack -- its loading layer gone, the graph layer it moves to written only at the end -- so a chunk not yet resolved fell back to the session layer: a group's child under an Edit Target cooked outside the layer the target exports. The answers are kept: the memo holds across this manager's own layer edits.

Source code in usd_chunks/chunk_registry.py
def place_deferred_layers(self) -> None:
    """Put every layer held back by defer_placement on the stage, and stop deferring.

    Each graph layer's sublayer list is written once, for all its chunks: every write
    to a list on the stage recomposes the whole layer stack, so placing a layer per
    write made a network of n chunks cost n recomposes of up to n layers each.

    Each chunk's Edit Target is resolved first, while every held layer is still on the
    stage: an Edit Target names a layer, and counts only while that layer is composed.
    Asked during placement, a layer a Layer chunk made was in no stack -- its loading
    layer gone, the graph layer it moves to written only at the end -- so a chunk not
    yet resolved fell back to the session layer: a group's child under an Edit Target
    cooked outside the layer the target exports. The answers are kept: the memo holds
    across this manager's own layer edits.
    """
    if self.stage and self._deferred_placement:
        for ref in self._deferred_placement.values():
            if isinstance(c := ref(), core.ChunkBase) and not c.has_flag(ChunkFlags.GroupFlags.CONTEXT):
                chunk_utils.get_edit_layer(self.stage, c)
    held = self.end_deferral()
    with self.batched_placement():
        for c in held:
            self.get_or_create_layer(c)

reset ⚓︎

reset()

Return the singleton to its pre-initialize state, dropping every layer mapping.

Used when switching stages (reload, tests) so state from the old stage's stack doesn't leak into the new one. Keeps this class the single owner of its private field list -- callers must not clear fields directly.

Source code in usd_chunks/chunk_registry.py
def reset(self):
    """Return the singleton to its pre-initialize state, dropping every layer mapping.

    Used when switching stages (reload, tests) so state from the old stage's stack doesn't
    leak into the new one. Keeps this class the single owner of its private field list --
    callers must not clear fields directly.
    """
    # The cook stage kept between cooks holds the old stage's layers, and hears them.
    skip_cook.release_cook_stage()
    self.initialized = False
    self.stage = None
    self.chunk_layers = {}
    self.graph_layers = {}
    self._graph_layer_target = {}
    self._chunk_target = {}
    self._chunk_graph_key = {}
    self._retired_chunks = set()
    self._confirmed_in = {}
    self._held_deletes = None
    self._deferred_placement = None
    self._loading_layer = None
    self._sublayer_batch = None
    self._drop_deactivations()
    self._cook_layers = {}
    self._placed = {}
    self._arc_layers = {}
    self._outside_layers = {}
    self.internal_layer_ids = set()

save_cook_layers ⚓︎

save_cook_layers()

Write every cook layer's bake to its file: what its chunks cooked, flattened.

At runtime a cook layer only lists the layers its chunks cook into, all in memory. The bake is that stack flattened (UsdUtils.FlattenLayerStack, honouring the stage's mutes) and written beside the layer that lists it, so a consumer without usd_chunks sees the result. The layer in memory keeps listing the live ones; it is exported, never saved.

Each chunk the bake holds is stamped in the file's customLayerData under usdchunks:cooks, by its prim path in the file's own namespace: its module hash, a hash of its inputs, and whether its result is partial (muted, blocked outside the active view, or never cooked). A bake whose stamps no longer match is stale.

Returns:

Type Description
list[str]

The files written.

Source code in usd_chunks/chunk_registry.py
def save_cook_layers(self) -> list[str]:
    """Write every cook layer's bake to its file: what its chunks cooked, flattened.

    At runtime a cook layer only lists the layers its chunks cook into, all in memory.
    The bake is that stack flattened (UsdUtils.FlattenLayerStack, honouring the stage's
    mutes) and written beside the layer that lists it, so a consumer without usd_chunks
    sees the result. The layer in memory keeps listing the live ones; it is exported,
    never saved.

    Each chunk the bake holds is stamped in the file's customLayerData under
    usdchunks:cooks, by its prim path in the file's own namespace: its module hash, a
    hash of its inputs, and whether its result is partial (muted, blocked outside the
    active view, or never cooked). A bake whose stamps no longer match is stale.

    Returns:
        The files written.
    """
    self.deactivate_stale_bakes()
    written = []
    for cook in self._cook_layers.values():
        if cook.anonymous or not self.stage:
            continue
        reader = Usd.Stage.Open(cook, Usd.Stage.LoadNone)
        if muted := [i for i in self.stage.GetMutedLayers() if i != cook.identifier]:
            reader.MuteAndUnmuteLayers(muted, [])
        flat = UsdUtils.FlattenLayerStack(reader)
        flat.customLayerData = {
            **dict(cook.customLayerData),
            "usdchunks:derived": True,
            "usdchunks:cooks": self._bake_stamps(cook),
        }
        flat.Export(cook.realPath)
        written.append(cook.realPath)
    return written

save_layer ⚓︎

save_layer(c, dirname)

Export one chunk's layer into a directory.

Parameters:

Name Type Description Default
c ChunkHash

The chunk.

required
dirname str | Path

The directory to write to.

required

Returns:

Type Description

The written file, or None if the chunk has no layer.

Source code in usd_chunks/chunk_registry.py
def save_layer(self, c: core.ChunkHash, dirname: str | Path):
    """Export one chunk's layer into a directory.

    Args:
        c: The chunk.
        dirname: The directory to write to.

    Returns:
        The written file, or None if the chunk has no layer.
    """
    if not (layer := self.get_layer(c)):
        return None

    name = self.get_layer_file_name(layer)
    file = f"{dirname}/{name}"
    layer.Export(file)
    return Path(file)

save_layers ⚓︎

save_layers(path)

Export every chunk layer into a directory.

Parameters:

Name Type Description Default
path str | Path

The directory to write to.

required

Returns:

Type Description
dict[str, str]

{chunk: written file}, empty if path is not a directory.

Source code in usd_chunks/chunk_registry.py
def save_layers(self, path: str | Path) -> dict[str, str]:
    """Export every chunk layer into a directory.

    Args:
        path: The directory to write to.

    Returns:
        {chunk: written file}, empty if `path` is not a directory.
    """
    p = Path(path)
    if not p.is_dir():
        logger.warning("Can't save layers. Path is not a directory %s", path)
        return {}

    layers = {}
    for key, layer in self.chunk_layers.items():
        if not layer:
            continue
        file = self.save_layer(key, path)
        if not file:
            continue
        layers[file.as_posix()] = key
    return layers

target_of_layer ⚓︎

target_of_layer(layer)

The real layer a chunk layer's content lands in (get_chunk_target, by layer).

Parameters:

Name Type Description Default
layer Layer

A chunk's own layer.

required

Returns:

Type Description
Layer | None

Its chunk's target, or None if no tracked chunk owns layer.

Source code in usd_chunks/chunk_registry.py
def target_of_layer(self, layer: Sdf.Layer) -> Sdf.Layer | None:
    """The real layer a chunk layer's content lands in (get_chunk_target, by layer).

    Args:
        layer: A chunk's own layer.

    Returns:
        Its chunk's target, or None if no tracked chunk owns `layer`.
    """
    for c, owned in self.chunk_layers.items():
        if owned is not None and owned.identifier == layer.identifier:
            return self._chunk_target.get(c)
    return None

ChunkModule ⚓︎

ChunkModule(module=None, module_name='', module_file=Path())

One chunk module file, as the ChunkRegistry manages it.

Parameters:

Name Type Description Default
module ModuleType | None

The loaded chunk module

None
module_name str

The name of the module

''
module_file str

The original, absolute, path to the python module

Path()
Source code in usd_chunks/chunk_registry.py
def __init__(
    self,
    module: types.ModuleType | None = None,
    module_name: str = "",
    module_file: Path | str = Path(),
):
    self.module = module
    self.module_name = module_name
    self.module_file = Path(module_file)

    self.loaded = False
    #: Why the last load failed (a missing package, an import error), None once it loads.
    self.load_error: str | None = None
    # self.loaded_chunks: weakref.WeakValueDictionary[str, ChunkBase] = weakref.WeakValueDictionary()
    self.func: Callable[[], core.ChunkBase | list[core.ChunkBase]] | None = None

    self._has_main = False

    # This is set by the _ConfigTransformer when parsing the module (before loading) if the module
    # defines a config() function
    self.config: core.ChunkConfig = core.ChunkConfig()
    # Call _exec_module to lazily load the module. This runs the _ConfigTransformer. If module_file is .py and
    # module is not a RuntimeModule, _exec_module will create a _ChunkConfigLazyLoader
    #
    # Whether the file needs reading again before the next load: not after this read
    # succeeds, until unload() says the file may have changed. Reading it twice parses and
    # transforms it twice, and was half of what finding a scene's chunk modules cost.
    self._needs_read = not self._exec_module()

__repr__ ⚓︎

__repr__()

The module's name and file.

Source code in usd_chunks/chunk_registry.py
def __repr__(self) -> str:
    """The module's name and file."""
    return f"ChunkModule(module_name='{self.module_name}', module_file='{self.module_file.as_posix()}')"

create ⚓︎

create()

Run the module's main function and return the chunks it made, with its config attached.

The chunks are not added to a scene; the caller does that.

Returns:

Type Description
Sequence[ChunkBase]

Sequence[ChunkBase]: The created chunks, empty if main failed or is missing.

Source code in usd_chunks/chunk_registry.py
def create(self) -> Sequence[core.ChunkBase]:
    """Run the module's main function and return the chunks it made, with its config attached.

    The chunks are not added to a scene; the caller does that.

    Returns:
        Sequence[ChunkBase]: The created chunks, empty if main failed or is missing.
    """
    if not self.loaded:
        logger.warning("Trying to create a chunk from an unloaded module. Consider running load() first")

    loaded = []

    if self.func:
        try:
            retval = self.func()
        except Exception:
            logger.exception("Failed to run main function from module %s", self.module_name)
            return []
        try:
            loaded.extend(retval)  # type: ignore
        except TypeError:
            loaded.append(retval)
    else:
        logger.warning("module %s does not have a function for creating chunks", self.module_name)
        return []

    for c in loaded:
        _seed_one_prim_field(c)

    if self.config:
        for c in loaded:
            self.config.module = self.module_name
            # The config's module name is all a chunk holds of its module: hooks are
            # looked up in the registry by it (ChunkBase.get_chunk_module), so a chunk
            # runs what its saved info:chunkType will run on reopen, and nothing else.
            c._config = self.config
            # Here, at creation, so a load restores a saved untick onto it like any
            # other param -- values are read back before the chunk joins the scene.
            if (self.config.cache_cook and not self.config.cache_always
                    and c.get_param(cook_cache.TOGGLE) is None):
                c.add_param(core.Checkbox(
                    cook_cache.TOGGLE, "Cache Result", default_value=True,
                    tooltip="Read this node's result back from the cook cache when "
                    "nothing it depends on has changed, instead of cooking it again. "
                    "USD_CHUNKS_COOK_CACHE=0 turns the cache off for every node.",
                ))

    return loaded

declared_requirements ⚓︎

declared_requirements()

Its requirements, then its optional ones (the module-level declared_requirements).

Source code in usd_chunks/chunk_registry.py
def declared_requirements(self) -> list[str]:
    """Its requirements, then its optional ones (the module-level declared_requirements)."""
    return declared_requirements(self)

export ⚓︎

export(path)

Export this ChunkModule's source as a .py file. Any other extension warns and fails.

Parameters:

Name Type Description Default
path str | Path

The path to export to. Should be .py

required
Source code in usd_chunks/chunk_registry.py
def export(self, path: str | Path):
    """Export this ChunkModule's source as a .py file. Any other extension warns and fails.

    Args:
        path (str | Path): The path to export to. Should be .py
    """
    p = Path(path)

    if not self.module:
        return

    if p.suffix != PY_MODULE_EXT:
        logger.warning("wrong file extension %s", path)
        return

    try:
        source = inspect.getsource(self.module)
    except (OSError, TypeError):
        logger.exception("Could not read the source of %s", self.module_name)
        return

    with open(p, "w", encoding="utf-8") as f:
        f.write(source)

from_chunk_definition staticmethod ⚓︎

from_chunk_definition(file, name=None)

Create a ChunkModule from a chunk module file (.py).

Parameters:

Name Type Description Default
file str | Path

The module file.

required
name str | None

The module name; defaults to the file's stem.

None

Returns:

Type Description
list[ChunkModule]

list[ChunkModule]: A list holding the created module, or empty for anything

list[ChunkModule]

that is not a .py file.

Source code in usd_chunks/chunk_registry.py
@staticmethod
def from_chunk_definition(file: str | Path, name: str | None = None) -> list[ChunkModule]:
    """Create a ChunkModule from a chunk module file (.py).

    Args:
        file: The module file.
        name: The module name; defaults to the file's stem.

    Returns:
        list[ChunkModule]: A list holding the created module, or empty for anything
        that is not a .py file.
    """
    p = Path(file)

    if name is None:
        name = p.stem

    if p.suffix != PY_MODULE_EXT:
        logger.warning("Cannot import %s: a chunk module is a .py file", p.name)
        return []

    # No deserialising to do -- loading the module is ChunkModule's own job.
    return [ChunkModule(module_name=name, module_file=file)]

get_menu_path ⚓︎

get_menu_path()

Get the menu namespace this module lives under, outermost element first.

The module's dotted path is prefixed with its category, dropping a leading path element that merely repeats that category.

Returns:

Type Description
list[str]

List[str]: The menu namespace, outermost first; the last element is the label.

Source code in usd_chunks/chunk_registry.py
def get_menu_path(self) -> list[str]:
    """Get the menu namespace this module lives under, outermost element first.

    The module's dotted path is prefixed with its category, dropping a leading path
    element that merely repeats that category.

    Returns:
        List[str]: The menu namespace, outermost first; the last element is the label.
    """
    cat = self.config.category
    if isinstance(cat, Enum):
        cat = cat.value

    import_path = self.module_name.split(".")
    if not cat:
        return import_path
    else:
        namespace = cat.split("/")
        namespace.append(import_path[-1])

    return namespace

has_config ⚓︎

has_config()

Whether the module defines a config.

Source code in usd_chunks/chunk_registry.py
def has_config(self):
    """Whether the module defines a config."""
    return bool(self.config)

has_main ⚓︎

has_main()

Whether the module defines a main function.

Source code in usd_chunks/chunk_registry.py
def has_main(self):
    """Whether the module defines a main function."""
    return self._has_main

installed_packages ⚓︎

installed_packages()

The installed version of each package the config declares, optional ones included.

A package unpacked into a folder on PYTHONPATH, as the usdview bundles do it, has no install record to read a version from; if it is imported, its version stands in.

Returns:

Type Description
dict[str, str]

{package: installed version}; a package with no version to find is left out.

Source code in usd_chunks/chunk_registry.py
def installed_packages(self) -> dict[str, str]:
    """The installed version of each package the config declares, optional ones included.

    A package unpacked into a folder on PYTHONPATH, as the usdview bundles do it, has no
    install record to read a version from; if it is imported, its __version__ stands in.

    Returns:
        {package: installed version}; a package with no version to find is left out.
    """
    packages = {}
    for requirement in declared_requirements(self):
        name, _ = split_requirement(requirement)
        if name in packages:
            continue
        installed = installed_version(name)
        if installed is None:
            imported = sys.modules.get(name) or sys.modules.get(name.replace("-", "_"))
            installed = getattr(imported, "__version__", None)
        if installed is not None:
            packages[name] = str(installed)
    return packages

load ⚓︎

load()

Load the module and create its chunks.

Runs the one-time initialization before create(). Once loaded, call create() directly to skip it.

Returns:

Type Description
Sequence[ChunkBase]

Sequence[ChunkBase]: The created chunks, empty if loading failed.

Source code in usd_chunks/chunk_registry.py
def load(self) -> Sequence[core.ChunkBase]:
    """Load the module and create its chunks.

    Runs the one-time initialization before create(). Once loaded, call create() directly
    to skip it.

    Returns:
        Sequence[ChunkBase]: The created chunks, empty if loading failed.
    """
    if not self._pre_load():
        logger.warning("Could not load module %s", self.module)
        return []

    start = timer()

    res = self.create()
    self.loaded = len(res) > 0
    end = timer()

    logger.info("loaded module: %s. Load took %s seconds", self.module_name, end - start)
    return res

optional_requirements_installed ⚓︎

optional_requirements_installed(installed=None)

Whether every optional requirement is installed.

Parameters:

Name Type Description Default
installed

installed_packages(), when the caller has it already.

None
Source code in usd_chunks/chunk_registry.py
def optional_requirements_installed(self, installed=None) -> bool:
    """Whether every optional requirement is installed.

    Args:
        installed: installed_packages(), when the caller has it already.
    """
    optional = {split_requirement(r)[0]
                for r in getattr(self.config, "optional_requirements", None) or []}
    if not optional:
        return True
    return optional <= set(self.installed_packages() if installed is None else installed)

should_load_for_filter ⚓︎

should_load_for_filter()

Check if this module passes USD_CHUNK_LOAD_FILTER.

Returns:

Name Type Description
bool bool

True if no filter is set or the module is named in it

Source code in usd_chunks/chunk_registry.py
def should_load_for_filter(self) -> bool:
    """Check if this module passes USD_CHUNK_LOAD_FILTER.

    Returns:
        bool: True if no filter is set or the module is named in it
    """
    if module_filter := USD_CHUNK_LOAD_FILTER():
        if self.module_name not in module_filter:
            return False
    return True

should_load_for_host ⚓︎

should_load_for_host(name, version)

Whether config.hosts lets the module load in host name at version.

Empty hosts means every host. Otherwise one entry must name this host, and its specifier (if any) must admit the version: "blender>=5.1" admits "5.1.0", and a host that reports no version only matches an entry without a specifier.

Parameters:

Name Type Description Default
name str

The host's name, from SceneSender.get_host().

required
version str

Its version string.

required

Returns:

Name Type Description
bool bool

True if the module may load here

Source code in usd_chunks/chunk_registry.py
def should_load_for_host(self, name: str, version: str) -> bool:
    """Whether config.hosts lets the module load in host `name` at `version`.

    Empty hosts means every host. Otherwise one entry must name this host, and its
    specifier (if any) must admit the version: "blender>=5.1" admits "5.1.0", and a
    host that reports no version only matches an entry without a specifier.

    Args:
        name: The host's name, from SceneSender.get_host().
        version: Its version string.

    Returns:
        bool: True if the module may load here
    """
    hosts = self.config.hosts if self.config else None
    if not hosts:
        return True
    for entry in hosts:
        match = _HOST_ENTRY.match(entry or "")
        if match is None or match.group(1).lower() != (name or "").lower():
            continue
        spec = match.group(2).strip()
        if not spec:
            return True
        if SpecifierSet is None or not version:
            continue
        try:
            if Version(version) in SpecifierSet(spec):
                return True
        except (InvalidSpecifier, InvalidVersion):
            logger.warning("%s: cannot compare host %s %s against %r",
                           self.module_name, name, version, entry)
    return False

should_load_for_plugin ⚓︎

should_load_for_plugin(plugin_name)

Check if the module may be used with a plugin.

An empty config.load_for_plugin means the module is safe to load everywhere.

Parameters:

Name Type Description Default
plugin_name str

The name of the plugin

required

Returns:

Name Type Description
bool bool

True if the ChunkModule can be used with the plugin plugin_name

Source code in usd_chunks/chunk_registry.py
def should_load_for_plugin(self, plugin_name: str) -> bool:
    """Check if the module may be used with a plugin.

    An empty config.load_for_plugin means the module is safe to load everywhere.

    Args:
        plugin_name (str): The name of the plugin

    Returns:
        bool: True if the ChunkModule can be used with the plugin plugin_name
    """
    if self.config.load_for_plugin and plugin_name not in self.config.load_for_plugin:
        return False
    return True

unload ⚓︎

unload()

Mark the module as not loaded.

Source code in usd_chunks/chunk_registry.py
def unload(self):
    """Mark the module as not loaded."""
    self.loaded = False
    # Reload Chunks unloads to pick up an edited file, so the next load reads it again.
    self._needs_read = True

ChunkRegistry ⚓︎

A singleton that finds and registers chunk modules.

By default chunk modules are scanned for in the USD_CHUNK_DIRS directories.

Attributes:

Name Type Description
registered_directories set[str]

A set of directories we have searched

registered_modules dict[str, ChunkModule]

A mapping between module names and modules

scene SceneSender

An instance of the SceneSender for accessing the scene

plugin str

A string identifier of the currently loaded plugin

get_chunk_module ⚓︎

get_chunk_module(module_name)

Get the ChunkModule registered under a module name.

The name is the chunk's file name without its folder or extension, e.g. A/B/C/foo.py is 'foo'. A folder-qualified name such as 'B.C.foo', as older scenes saved it, resolves to the same module.

Parameters:

Name Type Description Default
module_name str

The name of the module to find

required

Returns:

Type Description
ChunkModule | None

ChunkModule | None: The module, or None if not found

Source code in usd_chunks/chunk_registry.py
def get_chunk_module(self, module_name: str) -> ChunkModule | None:
    """Get the ChunkModule registered under a module name.

    The name is the chunk's file name without its folder or extension, e.g. A/B/C/foo.py is
    'foo'. A folder-qualified name such as 'B.C.foo', as older scenes saved it, resolves to
    the same module.

    Args:
        module_name (str): The name of the module to find

    Returns:
        ChunkModule | None: The module, or None if not found
    """
    if found := self.registered_modules.get(module_name):
        return found
    # Scenes saved while chunks were named by folder ("modelling.box", "core_usd.prune_prim")
    # still resolve: a chunk's name is now its file name alone.
    return self.registered_modules.get(module_name.rpartition(".")[2])

get_chunk_modules ⚓︎

get_chunk_modules(loaded=False, sort_result=False)

Every registered chunk module.

Parameters:

Name Type Description Default
loaded

Only the modules that are loaded.

False
sort_result

Sort by module name.

False

Returns:

Type Description
list[ChunkModule]

The modules.

Source code in usd_chunks/chunk_registry.py
def get_chunk_modules(self, loaded=False, sort_result=False) -> list[ChunkModule]:
    """Every registered chunk module.

    Args:
        loaded: Only the modules that are loaded.
        sort_result: Sort by module name.

    Returns:
        The modules.
    """
    v = list(self.registered_modules.values())
    if loaded:
        v = [x for x in v if x.loaded]
    if not sort_result:
        return v
    return sorted(v, key=lambda x: x.module_name.split(".")[-1])

get_menu_entries ⚓︎

get_menu_entries(sort_result=True)

Get every entry a chunk-creation menu should offer, described without reference to any UI.

One entry per registered module, plus one per element of that module's ChunkConfig.variants. Variant entries sit alongside their module rather than beneath it, so a declared variant reads as a chunk in its own right in both the menu tree and the search field -- which is the whole point of declaring one.

A variant label may contain "/" to nest further, e.g. "standard_surface/Standard Surface". A catalog-driven module can declare hundreds of variants, and those need grouping or the category becomes one unnavigable list; search stays flat either way.

Parameters:

Name Type Description Default
sort_result bool

Sort modules by trailing name, as get_chunk_modules does

True

Returns:

Type Description
list[MenuEntry]

List[MenuEntry]: Base and variant entries, in module order

Source code in usd_chunks/chunk_registry.py
def get_menu_entries(self, sort_result: bool = True) -> list[MenuEntry]:
    """Get every entry a chunk-creation menu should offer, described without reference to any UI.

    One entry per registered module, plus one per element of that module's
    ChunkConfig.variants. Variant entries sit alongside their module rather than
    beneath it, so a declared variant reads as a chunk in its own right in both the
    menu tree and the search field -- which is the whole point of declaring one.

    A variant label may contain "/" to nest further, e.g. "standard_surface/Standard
    Surface". A catalog-driven module can declare hundreds of variants, and those need
    grouping or the category becomes one unnavigable list; search stays flat either way.

    Args:
        sort_result (bool): Sort modules by trailing name, as get_chunk_modules does

    Returns:
        List[MenuEntry]: Base and variant entries, in module order
    """
    entries: list[MenuEntry] = []

    for mod in self.get_chunk_modules(sort_result=sort_result):
        if mod.config.hidden:
            continue  # a type that exists to be loaded, not to be picked
        namespace = mod.get_menu_path()
        tooltip = clean_description(mod.config.description)

        entries.append(MenuEntry(module=mod, namespace=tuple(namespace), tooltip=tooltip))

        parent = tuple(namespace[:-1])
        # get_variants, not .variants: a module may declare them as a callable so the
        # cost lands here, when a menu is actually being drawn, rather than at discovery
        # in every headless process.
        for label, presets in mod.config.get_variants():
            sub = tuple(p for p in str(label).split("/") if p)
            if not sub:
                continue
            entries.append(
                MenuEntry(
                    module=mod,
                    namespace=parent + sub,
                    tooltip=tooltip,
                    presets=dict(presets),
                )
            )

    if sort_result:
        # A menu nests submenus in the order it first meets them, so categories are
        # ordered here, or they come out in the order of their alphabetically-first chunk.
        # Stable, so chunks keep their name order (and variants their place) within one.
        entries.sort(key=_category_order)

    return entries

import_chunk ⚓︎

import_chunk(path)

Import a chunk module from path. The file extension of path should be .py.

Parameters:

Name Type Description Default
path str | Path

The path to the chunk module

required
Source code in usd_chunks/chunk_registry.py
def import_chunk(self, path: str | Path):
    """Import a chunk module from path. The file extension of path should be .py.

    Args:
        path (str | Path): The path to the chunk module
    """
    p = Path(path)
    if p.suffix == PY_MODULE_EXT:
        for mod in ChunkModule.from_chunk_definition(p):
            if mod.module_name not in self.registered_modules:
                self.register_module(mod)
            mod.load()

initialize ⚓︎

initialize(scene_sender, plugin)

Initialize the FileReader with the working directory.

Parameters:

Name Type Description Default
scene_sender SceneSender

A SceneSender instance for access to the scene. We hold a reference to it here in order to keep it alive

required
plugin str

A string identifying the currently used plugin

required
Source code in usd_chunks/chunk_registry.py
def initialize(self, scene_sender: SceneSender, plugin: str) -> None:
    """Initialize the FileReader with the working directory.

    Args:
        scene_sender (SceneSender): A SceneSender instance for access to the scene.
            We hold a reference to it here in order to keep it alive
        plugin (str): A string identifying the currently used plugin
    """
    self.scene = scene_sender
    self.plugin = plugin

    ChunkDependencyManager().initialize()

    self.shadowed_modules = []
    ordered_dirs = self.register_chunk_environ_directories()
    # Scan in precedence order (env dirs, then GLOBAL_USDCHUNKS). Include any
    # directories registered out-of-band before initialize at the tail, so
    # they still get scanned but never override the explicit env dirs.
    extras = [p for p in self.registered_directories if p not in ordered_dirs]
    self.register_chunk_modules(ordered_dirs + extras)

    c = len(self.get_chunk_modules(loaded=True))
    logger.info("Found %s chunks. Loaded %s", len(self.registered_modules), c)
    self._report_shadows()
    self.initialized = True

register_chunk_environ_directories ⚓︎

register_chunk_environ_directories()

Register USD_CHUNK_DIRS, then append the GLOBAL_USDCHUNKS directory.

On a duplicate module name the first-registered wins (see register_module), so the USD_CHUNK_DIRS entries -- your proprietary / on-prem chunks -- override marketplace chunks installed into the appended GLOBAL_USDCHUNKS dir.

Returns:

Type Description
list[Path]

The registered directories, in precedence order.

Source code in usd_chunks/chunk_registry.py
def register_chunk_environ_directories(self) -> list[Path]:
    """Register USD_CHUNK_DIRS, then append the GLOBAL_USDCHUNKS directory.

    On a duplicate module name the first-registered wins (see register_module), so the
    USD_CHUNK_DIRS entries -- your proprietary / on-prem chunks -- override marketplace
    chunks installed into the appended GLOBAL_USDCHUNKS dir.

    Returns:
        The registered directories, in precedence order.
    """
    # The bundled chunks are a default, not something the user asked for: of its two
    # locations (checkout, installed package) only one exists, and the other is skipped
    # quietly instead of reported as "directory not found".
    defaults = set(USD_CHUNK_DIRS.default)
    # Absolute, as of now: modules are found here and loaded on first use, and a module
    # found under a relative folder (CI's USD_CHUNK_DIRS=chunks) kept a relative file
    # path, which a chdir in between -- a test's, a script's -- left pointing nowhere.
    dirs = [os.path.abspath(d) for d in USD_CHUNK_DIRS() if d not in defaults or Path(d).exists()]
    global _warned_no_chunk_dirs
    if not dirs and not _warned_no_chunk_dirs:
        _warned_no_chunk_dirs = True
        logger.warning(
            "Set USD_CHUNK_DIRS! It is empty, so your chunk folders are not loaded. "
            "Set it to your chunk folders, like C:\\path\\to\\usd_chunks\\chunks. "
            "See docs/install.md#environment-variables."
        )
    # Append the marketplace install dir last (lowest precedence). Skip it
    # when it doesn't exist yet (nothing installed) to avoid a spurious
    # "directory not found" warning on a clean machine.
    global_dir = GLOBAL_USDCHUNKS()
    global_dir = os.path.abspath(global_dir) if global_dir else global_dir
    self._global_dir = Path(global_dir) if global_dir else None
    if global_dir and global_dir not in dirs and Path(global_dir).exists():
        dirs.append(global_dir)

    ordered: list[Path] = []
    for chunk_dir in dirs:
        self.register_directory(chunk_dir)
        p = Path(chunk_dir)
        if p.exists() and p not in ordered:
            ordered.append(p)
    return ordered

register_chunk_modules ⚓︎

register_chunk_modules(paths)

Recursively register the chunk module files (.py) under each directory.

Parameters:

Name Type Description Default
paths Iterable[Path]

The directories containing the chunk modules to register.

required
Source code in usd_chunks/chunk_registry.py
def register_chunk_modules(self, paths: Iterable[Path]):
    """Recursively register the chunk module files (.py) under each directory.

    Args:
        paths (Iterable[Path]): The directories containing the chunk modules to register.
    """
    for working_dir in paths:
        # Loop over all directories of the working_dirs directory
        for module_path in Path(working_dir).glob("**/*" + PY_MODULE_EXT):
            # A chunk's name is its file name alone, never its folder: the name is what a
            # scene saves as info:chunkType, so sorting chunks into category folders must
            # not rename them (and break every scene saved before the move).
            module_name = module_path.stem

            for mod in self._discover_module(module_path, module_name):
                self.register_module(mod)
    _CONFIG_CACHE.save()

register_directory ⚓︎

register_directory(chunk_dir)

Add a single directory to the register.

Parameters:

Name Type Description Default
chunk_dir str | Path

The directory to add; a relative one is taken from the working directory now and kept absolute (see register_chunk_environ_directories).

required
Source code in usd_chunks/chunk_registry.py
def register_directory(self, chunk_dir: str | Path):
    """Add a single directory to the register.

    Args:
        chunk_dir (str | Path): The directory to add; a relative one is taken from the
            working directory now and kept absolute (see register_chunk_environ_directories).
    """
    chunk_dir = os.path.abspath(chunk_dir)
    if (p := Path(chunk_dir)) and not p.exists():
        logger.warning("%s directory not found.", chunk_dir)
        return

    if p in self.registered_directories:
        # directory already loaded
        return

    sys.path.append(str(chunk_dir))
    self.registered_directories.add(p)

register_module ⚓︎

register_module(module)

Register a ChunkModule, keeping it alive and accessible.

Parameters:

Name Type Description Default
module ChunkModule

The module to add to the registry

required

Returns:

Type Description
ChunkModule | None

ChunkModule | None: The registered module, or None if it was skipped

Source code in usd_chunks/chunk_registry.py
def register_module(self, module: ChunkModule) -> ChunkModule | None:
    """Register a ChunkModule, keeping it alive and accessible.

    Args:
        module (ChunkModule): The module to add to the registry

    Returns:
        ChunkModule | None: The registered module, or None if it was skipped
    """
    if not module.has_main():
        logger.info("Skipping %s. Module has no main function defined", module.module_name)
        return
    if not module.has_config():
        logger.info("Skipping %s. Module has no config function defined", module.module_name)
        return
    if not module.module:
        logger.info("Skipping %s. Module has no valid python source module", module.module_name)
        return
    if not module.should_load_for_plugin(self.plugin):
        logger.info("Skipping %s. Module should not be registered in this context", module.module_name)
        return
    if not module.should_load_for_filter():
        logger.info("Skipping %s. Module is excluded in CHUNK_MODULE_FILTER", module.module_name)
        return
    get_host = getattr(self.scene, "get_host", None)  # a bare test scene may not have one
    host, host_version = get_host() if get_host is not None else ("", "")
    if not module.should_load_for_host(host, host_version):
        logger.info("Skipping %s. Module is for %s, not %s %s", module.module_name,
                    ", ".join(module.config.hosts), host or "this host", host_version)
        return

    name = module.module_name

    if name in self.registered_modules:
        winner = self.registered_modules[name]
        if Path(winner.module_file) == Path(module.module_file):
            # The same file rediscovered, not two files claiming one name -- what a
            # re-initialize() does to every chunk it already holds. Reporting those as
            # shadows drowned the genuinely ambiguous ones (a second initialize logged
            # "69 shadowed chunk name(s)", i.e. all of them).
            return winner
        self.shadowed_modules.append(
            (name, Path(winner.module_file), Path(module.module_file))
        )
        logger.debug("Skipping %s. Duplicate module: %s", module.module_file, name)
        return

    self.registered_modules[name] = module
    return self.registered_modules[name]

unload_modules ⚓︎

unload_modules()

Unload every loaded module, so the next load of each reads its file again.

The chunks already made stay as they are. Reload Chunks (chunk_stage.ChunkStageSync.reload_chunks) calls this, then builds them again.

Source code in usd_chunks/chunk_registry.py
def unload_modules(self):
    """Unload every loaded module, so the next load of each reads its file again.

    The chunks already made stay as they are. Reload Chunks
    (chunk_stage.ChunkStageSync.reload_chunks) calls this, then builds them again.
    """
    for mod in self.get_chunk_modules():
        if mod.loaded:
            mod.unload()

MenuEntry dataclass ⚓︎

MenuEntry(module, namespace, tooltip='', presets=None)

One selectable item in a chunk-creation menu, described without reference to any UI.

A module contributes one base entry, plus one more per ChunkConfig.variants element. A variant creates exactly the same chunk as its module and then presets some params, so it needs no ChunkModule of its own: the preset lands in an ordinary param value, which round-trips through save/load like any other. That is what keeps N menu entries from costing N files -- module identity, discovery and shadowing all stay 1:1 with a file, and only the menu grows.

Frontends render this list rather than deriving it, so a module that declares variants gains them in every frontend at once.

Attributes:

Name Type Description

Parameters:

Name Type Description Default
module ChunkModule
required
namespace tuple[str, ...]
required
tooltip str
''
presets dict[str, Any] | None
None

label property ⚓︎

label

The text this entry shows in a menu.

create ⚓︎

create()

Create this entry's chunk(s), applying any variant presets.

Presets are applied here rather than by the caller because they must land before the chunk is added to a container: PARAM_ADDED fires synchronously and builds widgets that read their values immediately, so a preset applied after the add would never reach the widgets it was supposed to configure.

Returns:

Name Type Description
list list

The created chunks, ready for the caller to add

Source code in usd_chunks/chunk_registry.py
def create(self) -> list:
    """Create this entry's chunk(s), applying any variant presets.

    Presets are applied here rather than by the caller because they must land before
    the chunk is added to a container: PARAM_ADDED fires synchronously and builds
    widgets that read their values immediately, so a preset applied after the add
    would never reach the widgets it was supposed to configure.

    Returns:
        list: The created chunks, ready for the caller to add
    """
    chunks = self.module.create() if self.module.loaded else self.module.load()

    # The module's values for a new chunk (ChunkConfig.new_values), then the variant's on
    # top. Only here, where a chunk is made new: a load restores what was saved instead.
    config = self.module.config
    presets = {**(getattr(config, "new_values", None) or {}), **(self.presets or {})}
    if presets:
        for c in chunks or []:
            # Values only, no cook. Setting a param runs the chunk's module, and here
            # the chunk is not in any context yet: it cooked into a layer the stage
            # had not adopted, and when the real cook came on CHUNK_LOADED a module
            # that remembered what it last authored (light.py's since-removed _last_light_type)
            # saw nothing new and returned -- a "Dome Light" entry created the chunk
            # with the right menu value and no prim. Same guard the Qt paste path
            # sets before applying copied values (_paste_guard_sets): execute()
            # still solves and notifies, so the widgets built on add see the preset,
            # but skips module.execute while _executing is set.
            prev = getattr(c, "_executing", False)
            c._executing = True
            try:
                for param_name, value in presets.items():
                    if param := c.get_param(param_name):
                        param.value = value
            finally:
                c._executing = prev

    return chunks

candidate_distributions ⚓︎

candidate_distributions(imported)

Every pip name that could plausibly supply this import.

Several distributions can provide one module -- opencv-python and opencv-python-headless both give you cv2 -- so there is no single right answer to check against. The static map covers the well-known renames without needing anything installed; the environment is consulted as well, which catches the variants the map does not know, but is never the only source or a chunk would pass or fail on which variant this particular machine happens to have.

Parameters:

Name Type Description Default
imported str

A top-level import name.

required

Returns:

Type Description
set

Normalised distribution names.

Source code in usd_chunks/chunk_registry.py
def candidate_distributions(imported: str) -> set:
    """Every pip name that could plausibly supply this import.

    Several distributions can provide one module -- opencv-python and
    opencv-python-headless both give you cv2 -- so there is no single right answer to check
    against. The static map covers the well-known renames without needing anything
    installed; the environment is consulted as well, which catches the variants the map
    does not know, but is never the only source or a chunk would pass or fail on which
    variant this particular machine happens to have.

    Args:
        imported: A top-level import name.

    Returns:
        Normalised distribution names.
    """
    candidates = {imported, IMPORT_TO_PACKAGE.get(imported, imported)}
    candidates.update(_packages_distributions().get(imported, ()))
    return {_normalise_package(name) for name in candidates}

clean_description ⚓︎

clean_description(desc)

Dedented/stripped chunk description for a tooltip; '' for the unset placeholder.

Descriptions are often triple-quoted with leading indentation; dedent + strip so the tooltip reads cleanly. Returns '' for the default placeholder so undocumented chunks show no tooltip.

Parameters:

Name Type Description Default
desc str

The raw ChunkConfig.description

required

Returns:

Name Type Description
str str

Tooltip-ready text, or '' when the chunk is undocumented

Source code in usd_chunks/chunk_registry.py
def clean_description(desc: str) -> str:
    """Dedented/stripped chunk description for a tooltip; '' for the unset placeholder.

    Descriptions are often triple-quoted with leading indentation; dedent + strip so the
    tooltip reads cleanly. Returns '' for the default placeholder so undocumented chunks
    show no tooltip.

    Args:
        desc (str): The raw ChunkConfig.description

    Returns:
        str: Tooltip-ready text, or '' when the chunk is undocumented
    """
    cleaned = textwrap.dedent(desc or "").strip()
    return "" if cleaned == _PLACEHOLDER_DESC else cleaned

conflicting_requirements ⚓︎

conflicting_requirements(modules)

Packages whose installed version fails some chunk's declared range.

Reported against the version actually installed rather than by intersecting ranges in the abstract, because that is the question that has an answer: two chunks disagreeing matters exactly when what is on the machine cannot satisfy both. It also catches the single-chunk case, where the one declared range is not what is installed.

Parameters:

Name Type Description Default
modules

The chunk modules to check.

required

Returns:

Type Description
dict

{package: [(module_name, specifier, installed), ...]} for the failures only.

Source code in usd_chunks/chunk_registry.py
def conflicting_requirements(modules) -> dict:
    """Packages whose installed version fails some chunk's declared range.

    Reported against the version actually installed rather than by intersecting ranges in
    the abstract, because that is the question that has an answer: two chunks disagreeing
    matters exactly when what is on the machine cannot satisfy both. It also catches the
    single-chunk case, where the one declared range is not what is installed.

    Args:
        modules: The chunk modules to check.

    Returns:
        {package: [(module_name, specifier, installed), ...]} for the failures only.
    """
    if SpecifierSet is None:  # pragma: no cover - packaging ships with pip
        return {}

    wanted: dict = defaultdict(list)
    for module in modules:
        for requirement in declared_requirements(module):
            name, specifier = split_requirement(requirement)
            if specifier:
                wanted[_normalise_package(name)].append((module.module_name, specifier))

    failures: dict = {}
    for package, claims in wanted.items():
        installed = installed_version(package)
        if installed is None:
            continue
        try:
            version = Version(installed)
        except InvalidVersion:
            continue
        bad = [
            (name, specifier, installed)
            for name, specifier in claims
            if version not in SpecifierSet(specifier)
        ]
        if bad:
            failures[package] = bad
    return failures

declared_requirements ⚓︎

declared_requirements(module)

Every package a module declares: its requirements, then its optional ones.

Parameters:

Name Type Description Default
module

The chunk module.

required

Returns:

Type Description
list[str]

The requirement strings.

Source code in usd_chunks/chunk_registry.py
def declared_requirements(module) -> list[str]:
    """Every package a module declares: its requirements, then its optional ones.

    Args:
        module: The chunk module.

    Returns:
        The requirement strings.
    """
    config = module.config
    return [*(config.requirements or []), *(getattr(config, "optional_requirements", None) or [])]

installed_version ⚓︎

installed_version(package)

The version actually importable right now, or None.

The only number that is true. What a chunk declared is a claim; this is what ran, and it is what a recipe's manifest has to record -- otherwise a conflict resolved by whichever install landed last is invisible after the fact.

Parameters:

Name Type Description Default
package str

A distribution name; "-" and "_" spellings are both tried.

required

Returns:

Type Description

The installed version, or None.

Source code in usd_chunks/chunk_registry.py
def installed_version(package: str):
    """The version actually importable right now, or None.

    The only number that is true. What a chunk declared is a claim; this is what ran, and it
    is what a recipe's manifest has to record -- otherwise a conflict resolved by whichever
    install landed last is invisible after the fact.

    Args:
        package: A distribution name; "-" and "_" spellings are both tried.

    Returns:
        The installed version, or None.
    """
    for candidate in (package, package.replace("-", "_"), package.replace("_", "-")):
        try:
            return distribution_version(candidate)
        except PackageNotFoundError:
            continue
    return None

is_chunk_internal_layer ⚓︎

is_chunk_internal_layer(layer)

Whether the chunk graph owns this layer.

"Internal" means a per-chunk procedural layer or a graph layer grouping them: content the chunk system Clear()s and regenerates, never something a user picks as an Edit Target or exports, and never somewhere a chunk's bookkeeping may be hand-moved.

Answered from the identifiers ChunkLayerManager records as it creates them, so it costs a set lookup. The alternative -- deriving it by walking every tracked layer's sublayers -- has to be redone against live subLayerPaths every time it is asked, which is once per reconcile pass and one pass per wire, and the layer stack grows with the graph.

Deliberately NOT stored on the layer itself, which is the obvious place and is wrong: Sdf.Layer.Clear() drops customLayerData along with everything else, and Clear() on its own layer is exactly what a procedural chunk does every time it cooks. A mark there survives until the chunk first runs and then silently disappears.

Parameters:

Name Type Description Default
layer Layer

The layer to check.

required

Returns:

Type Description
bool

True for a layer the chunk graph created.

Source code in usd_chunks/chunk_registry.py
def is_chunk_internal_layer(layer: Sdf.Layer) -> bool:
    """Whether the chunk graph owns this layer.

    "Internal" means a per-chunk procedural layer or a graph layer grouping them: content
    the chunk system Clear()s and regenerates, never something a user picks as an Edit
    Target or exports, and never somewhere a chunk's bookkeeping may be hand-moved.

    Answered from the identifiers ChunkLayerManager records as it creates them, so it costs
    a set lookup. The alternative -- deriving it by walking every tracked layer's sublayers --
    has to be redone against live subLayerPaths every time it is asked, which is once per
    reconcile pass and one pass per wire, and the layer stack grows with the graph.

    Deliberately NOT stored on the layer itself, which is the obvious place and is wrong:
    Sdf.Layer.Clear() drops customLayerData along with everything else, and Clear() on its
    own layer is exactly what a procedural chunk does every time it cooks. A mark there
    survives until the chunk first runs and then silently disappears.

    Args:
        layer: The layer to check.

    Returns:
        True for a layer the chunk graph created.
    """
    if not layer:
        return False
    return layer.identifier in ChunkLayerManager().internal_layer_ids

match_score ⚓︎

match_score(query, label, path=())

How well query matches label, or the category path it sits in; 0 if it does not.

Every word of query must match. The scoring is search_menu_entries', so the chunk menu and the node graph's search rank the same way.

Parameters:

Name Type Description Default
query str

What was typed; case and surrounding whitespace are ignored.

required
label str

The name being searched for.

required
path tuple[str, ...]

Weaker names the label also answers to, such as its category.

()

Returns:

Type Description
int

The score, higher for a closer match; 0 for an empty query or any word that misses.

Source code in usd_chunks/chunk_registry.py
def match_score(query: str, label: str, path: tuple[str, ...] = ()) -> int:
    """How well query matches label, or the category path it sits in; 0 if it does not.

    Every word of query must match. The scoring is search_menu_entries', so the chunk menu
    and the node graph's search rank the same way.

    Args:
        query: What was typed; case and surrounding whitespace are ignored.
        label: The name being searched for.
        path: Weaker names the label also answers to, such as its category.

    Returns:
        The score, higher for a closer match; 0 for an empty query or any word that misses.
    """
    total = 0
    for token in query.lower().split():
        if not (score := _token_score(token, label, path)):
            return 0
        total += score
    return total

migrate_legacy_dependencies ⚓︎

migrate_legacy_dependencies(sources, target)

Copy into target the packages in sources that this interpreter can import.

Run while this Python version's folder has no packages yet, so a user upgrading is not asked to reinstall every chunk's packages. A distribution is copied only if its wheel tags fit this interpreter and its files are intact (_intact_files); anything else is left for the usual ask-to-install. Copied, not moved: the sources may also hold packages for another Python version, whose own first run migrates them. Several versions of one distribution: the newest that qualifies wins.

Parameters:

Name Type Description Default
sources list[str]

Folders packages were pip install -t'd into.

required
target str

This Python version's dependency folder. It may already exist holding only the package cache file; anything else in it means it is in use, and nothing is copied.

required

Returns:

Type Description
list[str]

The distributions copied, as "name-version".

Source code in usd_chunks/chunk_registry.py
def migrate_legacy_dependencies(sources: list[str], target: str) -> list[str]:
    """Copy into `target` the packages in `sources` that this interpreter can import.

    Run while this Python version's folder has no packages yet, so a user upgrading
    is not asked to reinstall every chunk's packages. A distribution is copied only if its
    wheel tags fit this interpreter and its files are intact (_intact_files); anything else
    is left for the usual ask-to-install. Copied, not moved: the sources may also hold
    packages for another Python version, whose own first run migrates them. Several
    versions of one distribution: the newest that qualifies wins.

    Args:
        sources: Folders packages were `pip install -t`'d into.
        target: This Python version's dependency folder. It may already exist holding only
            the package cache file; anything else in it means it is in use, and nothing is
            copied.

    Returns:
        The distributions copied, as "name-version".
    """
    if sys_tags is None or not _no_packages_in(target):
        return []
    supported = {str(t) for t in sys_tags()}

    candidates: dict[str, list[tuple[Version, str, str]]] = defaultdict(list)
    for source in sources:
        for entry in os.listdir(source):
            if not entry.endswith(".dist-info"):
                continue
            name, _, version = entry[: -len(".dist-info")].rpartition("-")
            try:
                parsed = Version(version)
                with open(os.path.join(source, entry, "WHEEL"), encoding="utf-8") as f:
                    tags = {line.split(":", 1)[1].strip() for line in f if line.startswith("Tag:")}
            except (InvalidVersion, OSError):
                continue
            if tags & supported:
                candidates[_normalise_package(name)].append((parsed, source, entry))

    staging = f"{target}.migrating-{os.getpid()}"
    copied = []
    for versions in candidates.values():
        for _, source, dist_info in sorted(versions, reverse=True):
            files = _intact_files(source, dist_info)
            if files is None:
                continue
            for path in files:
                dest = os.path.join(staging, path)
                os.makedirs(os.path.dirname(dest), exist_ok=True)
                shutil.copy2(os.path.join(source, path), dest)
            shutil.copytree(os.path.join(source, dist_info), os.path.join(staging, dist_info), dirs_exist_ok=True)
            copied.append(dist_info[: -len(".dist-info")])
            break

    try:
        if not _no_packages_in(target):
            # Another process (an xdist worker, a second usdview) migrated first.
            return []
        if copied:
            shutil.copytree(staging, target, dirs_exist_ok=True)
        # Once, even when nothing qualified: hashing every file of a large package (vtk)
        # again on each stage reload would buy nothing. The marker makes the folder in use.
        os.makedirs(target, exist_ok=True)
        open(os.path.join(target, _MIGRATED_MARKER), "w").close()
    finally:
        shutil.rmtree(staging, ignore_errors=True)
    return copied

search_menu_entries ⚓︎

search_menu_entries(entries, query)

The entries matching query, closest first -- the chunk menu's search.

Every word of the query must match the entry's name or its category path. A name that starts with the query ranks first (shorter names ahead), then a name with a word that starts with it, then the query anywhere in the name, then the name's initials ("vmo" for vector_math_ops), then the category, then the query's letters in order with a few missing in between ("tranform"). Shared by every frontend so "trans" lists the same chunks in the same order everywhere.

Parameters:

Name Type Description Default
entries List[MenuEntry]

The entries to search, in menu order

required
query str

What was typed; case and surrounding whitespace are ignored

required

Returns:

Type Description
list[MenuEntry]

List[MenuEntry]: Matching entries, best first; all of entries, unchanged, for an

list[MenuEntry]

empty query.

Source code in usd_chunks/chunk_registry.py
def search_menu_entries(entries: list[MenuEntry], query: str) -> list[MenuEntry]:
    """The entries matching query, closest first -- the chunk menu's search.

    Every word of the query must match the entry's name or its category path. A name that
    starts with the query ranks first (shorter names ahead), then a name with a word that
    starts with it, then the query anywhere in the name, then the name's initials ("vmo"
    for vector_math_ops), then the category, then the query's letters in order with a few
    missing in between ("tranform"). Shared by every frontend so "trans" lists the same
    chunks in the same order everywhere.

    Args:
        entries (List[MenuEntry]): The entries to search, in menu order
        query (str): What was typed; case and surrounding whitespace are ignored

    Returns:
        List[MenuEntry]: Matching entries, best first; all of entries, unchanged, for an
        empty query.
    """
    if not query.split():
        return list(entries)
    scored = []
    for index, entry in enumerate(entries):
        label = entry.label
        if total := match_score(query, label, tuple(entry.namespace[:-1])):
            if entry.presets is None:
                # A chunk ahead of a variant that matches about as well: one module can
                # declare hundreds (every MaterialX node), and "ref" should reach Reference
                # before the Reflect shader.
                total += _BASE_ENTRY_BONUS
            scored.append((-total, len(label), label.lower(), index, entry))
    scored.sort(key=lambda s: s[:4])
    return [s[-1] for s in scored]

split_requirement ⚓︎

split_requirement(requirement)

A requirement string as (name, specifier), specifier "" when unconstrained.

Separate from ChunkDependencyManager.package_info, which answers a narrower question -- the exact version to cache an install under -- and only understands "==".

Parameters:

Name Type Description Default
requirement str

A requirement string.

required

Returns:

Type Description
tuple

(name, specifier).

Source code in usd_chunks/chunk_registry.py
def split_requirement(requirement: str) -> tuple:
    """A requirement string as (name, specifier), specifier "" when unconstrained.

    Separate from ChunkDependencyManager.package_info, which answers a narrower question --
    the exact version to cache an install under -- and only understands "==".

    Args:
        requirement: A requirement string.

    Returns:
        (name, specifier).
    """
    text = str(requirement).strip()
    for op in _SPECIFIER_OPS:
        if op in text:
            head, _, _ = text.partition(op)
            return head.strip(), text[len(head):].strip()
    return text, ""

top_level_imports ⚓︎

top_level_imports(module_file)

The root names a chunk module imports at module scope, read without executing it.

Walks into try/except, if and with rather than only tree.body, so a guarded import is seen. rbd_solver is exactly that case::

try:
    import mujoco as _mj
    import numpy as _np
    _HAS_MUJOCO = True
except ImportError:
    _HAS_MUJOCO = False

Reading only the literal top level would report nothing for that file, in the one chunk where a version silently changing the answer matters most -- try-guarding a heavy optional is the normal pattern for precisely the chunks that carry a simulator.

Function and class bodies are still skipped, which is the actual house rule: imports go at module top level, never inside a function. A try-guarded import obeys that rule lexically; it just is not an ast.Import node in tree.body, which is a different thing.

Parameters:

Name Type Description Default
module_file

The chunk module's file.

required

Returns:

Type Description
set

The imported root names, empty if the file cannot be read or parsed.

Source code in usd_chunks/chunk_registry.py
def top_level_imports(module_file) -> set:
    """The root names a chunk module imports at module scope, read without executing it.

    Walks into try/except, if and with rather than only `tree.body`, so a guarded import is
    seen. rbd_solver is exactly that case::

        try:
            import mujoco as _mj
            import numpy as _np
            _HAS_MUJOCO = True
        except ImportError:
            _HAS_MUJOCO = False

    Reading only the literal top level would report nothing for that file, in the one chunk
    where a version silently changing the answer matters most -- try-guarding a heavy
    optional is the normal pattern for precisely the chunks that carry a simulator.

    Function and class bodies are still skipped, which is the actual house rule: imports go
    at module top level, never inside a function. A try-guarded import obeys that rule
    lexically; it just is not an ast.Import node in tree.body, which is a different thing.

    Args:
        module_file: The chunk module's file.

    Returns:
        The imported root names, empty if the file cannot be read or parsed.
    """
    try:
        tree = ast.parse(Path(module_file).read_text(encoding="utf-8"))
    except (OSError, SyntaxError):
        return set()

    roots = set()

    def walk(nodes):
        for node in nodes:
            if isinstance(node, ast.Import):
                roots.update(a.name.split(".")[0] for a in node.names)
            elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module:
                roots.add(node.module.split(".")[0])
            elif not isinstance(node, _NOT_MODULE_SCOPE):
                walk(ast.iter_child_nodes(node))

    walk(tree.body)
    return roots

unconstrained_requirements ⚓︎

unconstrained_requirements(module)

Requirements declared with no version constraint at all.

A bare "mujoco" says nothing, so two runs a month apart can use different solvers from an identical recipe and nothing recorded explains the difference.

A range is what a chunk should declare -- what it is compatible with -- rather than an exact pin. Exact pins do not compose: two chunks pinning numpy==2 and numpy==3 have no solution, there is one sys.modules['numpy'], and resolving it would mean editing someone else's chunk. Ranges intersect, and the exact version belongs in the recipe that runs them, recorded as what was actually imported.

Parameters:

Name Type Description Default
module ChunkModule

The chunk module.

required

Returns:

Type Description
list

The unconstrained requirement strings.

Source code in usd_chunks/chunk_registry.py
def unconstrained_requirements(module: ChunkModule) -> list:
    """Requirements declared with no version constraint at all.

    A bare "mujoco" says nothing, so two runs a month apart can use different solvers from
    an identical recipe and nothing recorded explains the difference.

    A *range* is what a chunk should declare -- what it is compatible with -- rather than an
    exact pin. Exact pins do not compose: two chunks pinning numpy==2 and numpy==3 have no
    solution, there is one sys.modules['numpy'], and resolving it would mean editing someone
    else's chunk. Ranges intersect, and the exact version belongs in the recipe that runs
    them, recorded as what was actually imported.

    Args:
        module: The chunk module.

    Returns:
        The unconstrained requirement strings.
    """
    return [r for r in declared_requirements(module) if not split_requirement(r)[1]]

undeclared_imports ⚓︎

undeclared_imports(module, sibling_modules=())

Third-party imports a chunk makes without declaring them in config().

An undeclared dependency is not only an install that breaks on a clean machine -- it is a version that never reaches a run's manifest. A chunk importing numpy without saying so means a numpy upgrade can change its results while the manifest still reads clean, which is worse than having no manifest, because it looks authoritative.

Parameters:

Name Type Description Default
module ChunkModule

The chunk module.

required
sibling_modules

The other chunk modules' import names, which resolve locally and are not dependencies to declare.

()

Returns:

Type Description
list

The undeclared import names.

Source code in usd_chunks/chunk_registry.py
def undeclared_imports(module: ChunkModule, sibling_modules=()) -> list:
    """Third-party imports a chunk makes without declaring them in config().

    An undeclared dependency is not only an install that breaks on a clean machine -- it is
    a version that never reaches a run's manifest. A chunk importing numpy without saying so
    means a numpy upgrade can change its results while the manifest still reads clean, which
    is worse than having no manifest, because it looks authoritative.

    Args:
        module: The chunk module.
        sibling_modules: The other chunk modules' import names, which resolve locally and
            are not dependencies to declare.

    Returns:
        The undeclared import names.
    """
    # split_requirement, not package_info: the latter only understands "==", so a
    # requirement declared as a range would come back with the whole string as its name
    # and every import it covers would read as undeclared.
    declared = {
        _normalise_package(split_requirement(r)[0])
        for r in declared_requirements(module)
    }
    declared.update(_normalise_package(m) for m in (module.config.required_modules or []))

    host_imports = set()
    for entry in module.config.hosts or []:
        if match := _HOST_ENTRY.match(entry or ""):
            host_imports |= _HOST_IMPORTS.get(match.group(1).lower(), set())

    undeclared = []
    for root in sorted(top_level_imports(module.module_file)):
        if root in _ASSUMED_IMPORTS or root in host_imports or root in sys.stdlib_module_names:
            continue
        if root in sibling_modules:
            continue
        if not candidate_distributions(root) & declared:
            undeclared.append(root)
    return undeclared