- 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.
- Retrieving the active context instance is simple:
>>> import anchorpoint >>> ctx = anchorpoint.Context.instance()
- Access an 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.File if the action is running on a file selection, Type.Folder when running on a folder selection.
A dictionary that passes the inputs from the YAML file to the script. The key to the dictionary is the name of the input.
The absolute path to the current file or folder. For a multi selection this is the file / folder the context menu was invoked on.
Project only, the path of the file / folder relative to the project root
The project root, if any
The directory where the action’s YAML lives
The parent folder of path
File only, the name of the file without the path and without the extension
The suffix of the file (e.g. “blend”)
The username of the current user, e.g. “John Doe”
The list of selected files (absolute paths)
The list of selected folders (absolute paths)
The icon as set in the YAML file
The icon color as set in the YAML file
- 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.
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
>>> 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
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
>>> 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.
>>> 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.
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
- Run a function with arguments
>>> ctx = anchorpoint.Context.instance() >>> def my_func(path): >>> print("async: " + path) >>> >>> ctx.run_async(my_func, ctx.path)