> For the complete documentation index, see [llms.txt](https://stage-precision.gitbook.io/grid/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stage-precision.gitbook.io/grid/scripting/project-scripts.md).

# Project Scripts

### Creating a Project Script

A Project Script is added as an Object in the Project Tree.

<div align="left"><figure><img src="/files/tNpQ7FUMpgrsMcG8ZV9A" alt="" width="274"><figcaption></figcaption></figure></div>

### Opening the Script Editor

Select the **Script Object** in the Project Tree to access its settings in the Inspector.

Open the **Script** tab in the Inspector to view the script content.

From there, click the **Script Editor** button in the upper-right corner of the script panel to open the full Script Editor.

<div align="left"><figure><img src="/files/SRxkFoMDBNBW4sARxBFc" alt="" width="325"><figcaption></figcaption></figure></div>

The Script Editor is where the Python code of the Project Script can be edited.

<div align="left"><figure><img src="/files/xQgq4WEikjV3NXxlPvmn" alt="" width="436"><figcaption></figcaption></figure></div>

A minimal Project Script looks like this:

```python
def trigger(ob, param=None):
    print("Script executed")
```

The `trigger()` function is called whenever the Project Script is executed.

### The `ob` Argument

The `ob` argument is a handle to the **Project Script Object itself**.

For example:

```python
def trigger(ob, param=None):
    print(ob)
```

For most Project Scripts, `ob` is not required and can simply be left unused.

It does not represent the Object or Workflow that triggered the script.

## Running a Project Script

Project Scripts can be executed from several locations in Grid Studio, depending on the current workflow.

### Run from the Script Editor

A Project Script can be executed directly from the Script Editor.

Open the run menu in the upper-right corner of the editor and select:

**run trigger**

<div align="left"><figure><img src="/files/v9NPj5JIYqct5BadV3Z3" alt="" width="493"><figcaption></figcaption></figure></div>

This executes the script's `trigger()` function.

This is especially useful while developing or testing a script.

### Run from the Inspector

A Project Script can also be executed directly from its Inspector.

Select the Script Object in the Project Tree, open the **Script** tab and click the **Run** button in the script panel.

<div align="left"><figure><img src="/files/rvVTujnbL7ZUpbZHUHtV" alt="" width="324"><figcaption></figcaption></figure></div>

This executes the same `trigger()` function without opening the full Script Editor.

### Run from the Project Tree

Project Scripts expose Actions directly in the Project Tree context menu.

Secondary-click the Script Object and open:

**Actions**

Available actions include:

* **run Script**
* **run Script with Delay**
* **run Script with Parameter**

<div align="left"><figure><img src="/files/8CIPQGSNiNilDHnZmVDS" alt="" width="437"><figcaption></figcaption></figure></div>

**run Script** executes the script immediately.

**run Script with Delay** executes the script after a specified delay.

**run Script with Parameter** executes the script while passing custom Parameter values to the `param` argument of `trigger()`.

### Run from a Workflow

Project Scripts can also be executed from a Workflow.

Drag the Script Object directly from the **Project Tree** into the Workflow.

<div align="left"><figure><img src="/files/uvCYARF8bz31Y7iXPFKo" alt="" width="563"><figcaption></figcaption></figure></div>

Grid Studio creates a Workflow Action for the Script Object, allowing the script to be triggered as part of the Workflow execution.

This is useful when reusable Python logic should be called from Events, conditions or other Workflow Actions.

{% hint style="info" %}
Project Scripts can be reused from multiple Workflows without duplicating the Python code. The script remains stored centrally in the Project Tree.
{% endhint %}

### Script Parameters

Project Scripts can define custom Parameters that can be passed to the script when it is executed.

Select the Script Object and open the **Parameter** tab in the Inspector.

Click the **+** button to add a new Parameter.

Available Parameter types include:

* Boolean
* Color
* Float
* Integer
* Point
* String
* Vector

Parameters can be renamed to make their purpose easier to identify.

To rename a Parameter, secondary-click it and select **Rename**.

<div align="left"><figure><img src="/files/VVU0JsK6n8JLkJqRxK0N" alt="" width="486"><figcaption></figcaption></figure></div>

#### Accessing Parameters in the Script

Parameters passed to the Project Script are available through the `param` argument of the `trigger()` function.

```python
def trigger(ob, param=None):
    print(param)
```

When multiple values are passed, they can be accessed individually by index:

```python
def trigger(ob, param=None):
    print(param[0])
    print(param[1])
```

{% hint style="info" %}
Script Parameters are only passed to `param` when the Project Script is executed using **run Script with Parameter**. When the script is executed without Parameters, `param` remains `None`.
{% endhint %}

### Accessing Project Data

Project Scripts have access to the same `sp` namespace used throughout the Grid Studio scripting environment.

Objects and Parameters can therefore be read or changed directly:

```python
def trigger(ob, param=None):
    position = sp.Camera1.coordinates.position.value
    print(position)
```

Parameters can also be written:

```python
def trigger(ob, param=None):
    sp.Camera1.coordinates.position.value = [0, 1, 2]
```

Script Aliases can be used when the Project Script should not depend directly on a specific Object ID:

```python
def trigger(ob, param=None):
    sp.alias.camera.coordinates.position.value = [0, 1, 2]
```

### When to Use Project Scripts

Project Scripts are useful when Python logic should be stored centrally and executed on demand.

Typical use cases include:

* Reusing the same logic from multiple Workflows
* Performing calculations or data processing
* Reading or updating multiple Parameters
* Executing a sequence of Parameter changes
* Passing values from a Workflow into reusable Python logic

For logic that should operate directly inside a Workflow and process its dynamic Workflow data, use a **Workflow Script Action** instead.
