Context
The Context API provides essential information about the current execution environment when an action is triggered in Anchorpoint. It gives you access to the selected files, folders, user information, project details, and input parameters defined in your action's YAML configuration.
Usage
The context is obtained using the get_context() function from the anchorpoint module:
import anchorpoint
ctx = anchorpoint.get_context()
Context Class
Properties
File and Folder Information
path(str): The absolute path to the file or folder where the action was triggered. For file actions: path to the selected file. For folder actions: path to the selected folder.folder(str): The absolute path to the parent folder. Always represents the directory containing the selected item.filename(str): The name of the selected file (without path). Empty string for folder actions.suffix(str): The file extension including the dot (e.g.,.py,.txt). Empty string for folders or files without extensions.selected_files(List[str]): List of absolute paths to all selected files. Useful for batch operations on multiple files.type(class: Type): The type of object that triggered the action (file, folder, task, etc.).
User and Project Information
username(str): The username of the current Anchorpoint user.user_id(str): Unique identifier of the current user.workspace_id(str): Unique identifier of the current workspace.project_id(str): Unique identifier of the current project. Empty string if not in a project context.project_path(str): The absolute file path to the root folder of the project.
Task Context
task_id(str): Unique identifier of the task if action was triggered from a task. Empty string if not triggered from a task.task_list_id(str): Unique identifier of the task list containing the task. Empty string if not triggered from a task.
Action Configuration
inputs(Dict[str, Any]): Dictionary containing input values defined in the action's YAML file inputs section. Access custom parameters passed to your action.icon(str): The icon path specified in the action's YAML configuration.module_path(str): Path to the directory containing the action's files.
Browser State
browser_path(str): Current path shown in the Anchorpoint browser. May differ frompathwhen action is triggered on specific items.browser_workspace_id(str): Workspace ID of the current browser view.browser_project_id(str): Project ID of the current browser view.
Methods
Asynchronous triggering of functions
-
run_async(func, *args, **kwargs)Executes a function asynchronously with progress indication. Automatically shows progress dialog and handles UI updates. Use for long-running operations to keep UI responsive.Arguments
func: The function to execute asynchronously*args: Positional arguments to pass to the function**kwargs: Keyword arguments to pass to the function
Returns: The return value of the executed function
Examples
Basic File Information
import anchorpoint
ctx = anchorpoint.get_context()
print(f"Selected file: {ctx.filename}")
print(f"File path: {ctx.path}")
print(f"Parent folder: {ctx.folder}")
print(f"File extension: {ctx.suffix}")
Prints several properties of the current context, when the action was triggered from a file context menu.
Working with Multiple Files
import anchorpoint
import os
ctx = anchorpoint.get_context()
# Process all selected files
for file_path in ctx.selected_files:
filename = os.path.basename(file_path)
print(f"Processing: {filename}")
# Your file processing logic here
Iterating over files, that have been selected in the Anchorpoint browser and the action has been triggered from the file context menu.
Using Input Parameters
In your YAML file:
inputs:
quality:
name: "Quality"
type: "dropdown"
options: ["Low", "Medium", "High"]
default: "Medium"
compress:
name: "Compress Output"
type: "checkbox"
default: true
In your Python script:
import anchorpoint
ctx = anchorpoint.get_context()
# Access the input values
quality = ctx.inputs.get("quality", "Medium")
compress = ctx.inputs.get("compress", True)
print(f"Quality setting: {quality}")
print(f"Compression enabled: {compress}")
Reads values that have been added to the YAML file using the inputs key.
Async Operations
import anchorpoint
import time
def long_running_task(file_path, output_path):
# Create a simple infinite spinning progress indicator
progress = anchorpoint.Progress("My Action", "Writing file...")
# Simulate a time-consuming operation
time.sleep(5)
# Your actual processing logic here
with open(output_path, 'w') as f:
f.write(f"Processed: {file_path}")
# Explicitly finish the progress
progress.finish()
return f"Successfully processed {file_path}"
ctx = anchorpoint.get_context()
# Run the task asynchronously with automatic progress dialog
result = ctx.run_async(long_running_task, ctx.path, ctx.path + ".processed")
Triggers a background process that does not block the UI. Usually used with a progress indicator.
Project Context
import anchorpoint
import apsync
ctx = anchorpoint.get_context()
api = anchorpoint.get_api()
# Check if we're in a project
if ctx.project_id:
project = api.get_project()
if project:
print(f"Working in project: {project.name}")
print(f"Project path: {project.path}")
else:
print("Not in a project context")
# Access workspace information
print(f"Workspace ID: {ctx.workspace_id}")
print(f"Current user: {ctx.username}")
Reads information about the current project, workspace and user.