import(path)

The import() function loads and evaluates OSL code from another file, making its contents available to the current script. This function supports importing both local files and package files.

// Import a local OSL file
data = import("utilities.osl")

// Import a file without the .osl extension (it will be added automatically)
helpers = import("helpers")

Syntax

import(filename)

Parameters

  • filename: String - The name or path of the file to import. If the filename doesn't contain a '.', the '.osl' extension will be added automatically.

Return Value

Returns the exported data from the imported file, which can be any valid OSL value (object, array, function, etc.).

Description

The import() function loads and executes OSL code from an external file, allowing you to:

  • Organize your code into modules

  • Reuse code across multiple scripts

  • Share functionality between different parts of your application

  • Load dynamically selected scripts at runtime

The function supports two main import paths:

  1. Local files in the current workspace

  2. Package files from the packages directory

Examples

Basic Usage

Importing Without Extension

Importing from Packages

Conditional Importing

Using Imported Data

Notes

  • Imported files are executed in their own scope

  • If a file doesn't exist or can't be loaded, an empty object is returned

  • The import path is resolved relative to the current script's location

  • For package imports, the file is loaded from the user's packages directory

  • The function handles path resolution automatically, using the parseFilePath function internally

Last updated

Was this helpful?