Skip to content

ReferenceLayer

src.utils.adobe.ReferenceLayer

Bases: ArtLayer

A static ArtLayer whose properties such as width or height are not going to change. Most often used as a reference to position or size other layers.

Source code in src\utils\adobe.py
class ReferenceLayer(ArtLayer):
    """A static ArtLayer whose properties such as width or height are not going to change. Most often
    used as a reference to position or size other layers."""

    def __init__(self, parent: Any = None, app: PhotoshopHandler = None):
        self._global_app = app if app else PhotoshopHandler()
        super().__init__(parent=parent)

    """
    * API Methods
    """

    def duplicate(self, relativeObject=None, insertionLocation=None):
        """Duplicates the layer and returns it as a `ReferenceLayer` object."""
        return ReferenceLayer(self.app.duplicate(relativeObject, insertionLocation))

    """
    * Cached Conversions
    """

    @cache
    def sID(self, index: str) -> int:
        """Caching handler for stringIDToTypeID on the global application object.

        Args:
            index: String ID to convert to Type ID.

        Returns:
            Type ID converted from string ID.
        """
        return self._global_app.stringIDToTypeID(index)

    """
    * Layer Properties
    """

    @cached_property
    def id(self) -> int:
        """int: This layer's ID (cached)."""
        return self.app.id

    @cached_property
    def action_getter(self) -> ActionReference:
        """Gets action descriptor info object for this layer.

        Returns:
            Action descriptor info object about the layer.
        """
        ref = ActionReference()
        ref.putIdentifier(self.sID('layer'), self.id)
        return self._global_app.executeActionGet(ref)

    """
    * Layer Bounds
    """

    @cached_property
    def bounds(self) -> LayerBounds:
        """LayerBounds: Bounds of the layer (left, top, right, bottom)."""
        return self.app.bounds

    @cached_property
    def bounds_no_effects(self) -> LayerBounds:
        """LayerBounds: Bounds of the layer (left, top, right, bottom) without layer effects applied."""
        with suppress(Exception):
            d = self.action_getter
            try:
                # Try getting bounds no effects
                bounds = d.getObjectValue(self.sID('boundsNoEffects'))
            except PS_EXCEPTIONS:
                # Try getting bounds
                bounds = d.getObjectValue(self.sID('bounds'))
            return (
                bounds.getInteger(self.sID('left')),
                bounds.getInteger(self.sID('top')),
                bounds.getInteger(self.sID('right')),
                bounds.getInteger(self.sID('bottom')))
        # Fallback to layer object bounds property
        return self.bounds

    """
    * Layer Dimensions
    """

    @cached_property
    def dims(self) -> type[LayerDimensions]:
        """LayerDimensions: Returns dimensions of the layer (cached), including:
            - bounds (left, right, top, bottom)
            - height
            - width
            - center_x
            - center_y
        """
        return self.get_dimensions_from_bounds(self.bounds)

    @cached_property
    def dims_no_effects(self) -> type[LayerDimensions]:
        """LayerDimensions: Returns dimensions of the layer (cached) without layer effects applied, including:
            - bounds (left, right, top, bottom)
            - height
            - width
            - center_x
            - center_y
        """
        return self.get_dimensions_from_bounds(self.bounds_no_effects)

    """
    * Utility Methods
    """

    @staticmethod
    def get_dimensions_from_bounds(bounds) -> type[LayerDimensions]:
        """Compute width and height based on a set of bounds given.

        Args:
            bounds: List of bounds given.

        Returns:
            Dict containing height, width, and positioning locations.
        """
        width = int(bounds[2] - bounds[0])
        height = int(bounds[3] - bounds[1])
        return LayerDimensions(
            width=width,
            height=height,
            center_x=round((width / 2) + bounds[0]),
            center_y=round((height / 2) + bounds[1]),
            left=int(bounds[0]), right=int(bounds[2]),
            top=int(bounds[1]), bottom=int(bounds[3]))

Attributes

action_getter: ActionReference

Gets action descriptor info object for this layer.

Returns:

Type Description
ActionReference

Action descriptor info object about the layer.

bounds: LayerBounds

bounds_no_effects: LayerBounds

dims: type[LayerDimensions]

  • bounds (left, right, top, bottom)
  • height
  • width
  • center_x
  • center_y

dims_no_effects: type[LayerDimensions]

  • bounds (left, right, top, bottom)
  • height
  • width
  • center_x
  • center_y

id: int

Functions

duplicate(relativeObject=None, insertionLocation=None)

Duplicates the layer and returns it as a ReferenceLayer object.

Source code in src\utils\adobe.py
def duplicate(self, relativeObject=None, insertionLocation=None):
    """Duplicates the layer and returns it as a `ReferenceLayer` object."""
    return ReferenceLayer(self.app.duplicate(relativeObject, insertionLocation))

get_dimensions_from_bounds(bounds) -> type[LayerDimensions]

Compute width and height based on a set of bounds given.

Parameters:

Name Type Description Default
bounds

List of bounds given.

required

Returns:

Type Description
type[LayerDimensions]

Dict containing height, width, and positioning locations.

Source code in src\utils\adobe.py
@staticmethod
def get_dimensions_from_bounds(bounds) -> type[LayerDimensions]:
    """Compute width and height based on a set of bounds given.

    Args:
        bounds: List of bounds given.

    Returns:
        Dict containing height, width, and positioning locations.
    """
    width = int(bounds[2] - bounds[0])
    height = int(bounds[3] - bounds[1])
    return LayerDimensions(
        width=width,
        height=height,
        center_x=round((width / 2) + bounds[0]),
        center_y=round((height / 2) + bounds[1]),
        left=int(bounds[0]), right=int(bounds[2]),
        top=int(bounds[1]), bottom=int(bounds[3]))

sID(index: str) -> int

Caching handler for stringIDToTypeID on the global application object.

Parameters:

Name Type Description Default
index str

String ID to convert to Type ID.

required

Returns:

Type Description
int

Type ID converted from string ID.

Source code in src\utils\adobe.py
@cache
def sID(self, index: str) -> int:
    """Caching handler for stringIDToTypeID on the global application object.

    Args:
        index: String ID to convert to Type ID.

    Returns:
        Type ID converted from string ID.
    """
    return self._global_app.stringIDToTypeID(index)