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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
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
339
340
341
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
@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
347
348
349
350
351
352
353
354
355
356
357
@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)