Context#

class anchorpoint.Context#

The context object is your link between Anchorpoint and the world of actions. It provides you with important information such as the list of selected files or folders, for example. Furthermore, there is a lot of information from the active YAML file, such as defined inputs that the user has entered.

In addition, the context offers a variety of helpful functions that simplify working with actions in Anchorpoint.

You never create the context yourself, instead you can always retrieve it via the instance() function.

Examples

Retrieving the active context instance is simple:
>>> import anchorpoint
>>> ctx = anchorpoint.Context.instance()
Access an input “my_input” from a YAML file by providing the key to the inputs dictionary like so:
>>> ctx.inputs["my_input"]
42
Iterate over all selected files:
>>> for f in ctx.selected_files:
>>>     print(f)
type#

Type.File if the action is running on a file selection, Type.Folder when running on a folder selection.

Type

Type

inputs#

A dictionary that passes the inputs from the YAML file to the script. The key to the dictionary is the name of the input.

Type

dict

path#

The absolute path to the current file or folder. For a multi selection this is the file / folder the context menu was invoked on.

Type

str

relative_path#

Project only, the path of the file / folder relative to the project root

Type

str

project_path#

The project path, if any

Type

str

yaml_dir#

The directory where the action’s YAML lives

Type

str

folder#

The parent folder of path

Type

str

filename#

File only, the name of the file without the path and without the extension

Type

str

suffix#

The suffix of the file (e.g. “blend”)

Type

str

username#

The username of the current user, e.g. “John Doe”

Type

str

email#

The email of the current user

Type

str

workspace_id#

The unique id of the workspace the action is executed in

Type

str

selected_files#

The list of selected files (absolute paths)

Type

list

selected_folders#

The list of selected folders (absolute paths)

Type

list

icon#

The icon as set in the YAML file

Type

str

icon_color#

The icon color as set in the YAML file

Type

str

create_project(path: str, name: str, workspace_id: Optional[str] = None) object#

Create a new project at path. Will create a new folder if necessary.

Parameters
  • path (str) – The path to the project

  • name (str) – The name of the project

  • workspace_id (Optional[str]) – The workspace id - or None to use the user workspace

Example

>>> ctx = anchorpoint.Context.instance()
>>> ctx.create_project("C:/Projects/AP-Commercial", "Anchorpoint Commercial")
install(package: str, upgrade: bool = False) None#

Utility function to pip install a python module to the global python module directory. To not slow down your scripts make sure to only call this function when the module import fails

Parameters
  • package (str) – The name of the package to install (e.g. “Pillow”)

  • upgrade (bool) – Set to True to upgrade the package to the latest version

Example

>>> ctx = anchorpoint.Context.instance()
>>> try:
>>>     from PIL import Image
>>> except:
>>>     ctx.install("Pillow")
static instance() anchorpoint.Context#

Returns the active instance of the anchorpoint context.

Example

>>> ctx = anchorpoint.Context.instance()
run_async(func: function, *args, **kwargs) None#

Utility function to run a function asynchronously so that it does not block the Anchorpoint UI. Always use this for long running processes such as downloading files or running a rendering job. Note: Make sure to capture the Anchorpoint context object on the main thread.

Parameters
  • func (Callable[[*args, **kwargs], None]) – The function to run async

  • args (**kwargs) – The optional arguments for the function

  • args – The optional keyword arguments for the function

Example

Run a function with arguments
>>> ctx = anchorpoint.Context.instance()
>>> def my_func(path):
>>>     print("async: " + path)
>>>
>>> ctx.run_async(my_func, ctx.path)