---
title: "Useful python snippets"
description: "Documentation and guidance for building tools and integrations with the Disguise platform."
url: https://developer.disguise.one/python-api/useful-strings/
---

This page contains useful Python code snippets for common Designer tasks. For more comprehensive guides, see the [Python API Guides](#python-api-guides) section below.

## Add Video Layer at current track time

```python
guisystem.track.addNewLayer(VariableVideoModule, trackTime(), 60, 'Video');
```

_See the [Track & Sequencing Guide](/python-api/guides/track-and-sequencing) for more details on working with layers._

## Split track at current track time

```python
guisystem.track.splitSectionAtBeat(trackTime());
```

(note that the above two examples use the [Execution Utility Functions](/python-api/environment#execution-utility-functions) shorthand)

_See the [Track & Sequencing Guide](/python-api/guides/track-and-sequencing) for more details on working with tracks._

## Add a projector & set it’s resolution to 4K

```python
projector = resourceManager.loadOrCreate('objects/projector/myprojector.apx', Projector)
projector.resolution = Vec2(3840, 2160)
```

_See the [Stage Guide](/python-api/guides/stage) for more details on working with stage objects and the [Resources Guide](/python-api/guides/resources) for resource management patterns._

## Add and configure an OSC device

```python
# Create OSC device using resource manager
osc = resourceManager.loadOrCreate('objects/OscDevice/PluginMonitor.apx', OscDevice)

# Configure OSC device
osc.sendIPAddress = '127.0.0.1'  # IP where the plugin is running
osc.sendPort = {port}  # Port where our plugin is listening
osc.receivePort = {port} + 1  # Use next port for Designer to listen

# Add the device to devices array if not already there
device_exists = False
for device in state.devices.devices:
    if device.path == osc.path:
        device_exists = True
        break
if not device_exists:
    state.devices.devices.append(osc)
    print("Added OSC device to state.devices")

# Start the device if not already started
if not osc.started:
    osc.start()
```

_See the [Devices Guide](/python-api/guides/devices) for more details on working with the Device Manager and different device types._

## List all resources of a type

```python
resourceManager.allResources(OscDevice)
```

_See the [Resources Guide](/python-api/guides/resources) for comprehensive resource management patterns._

## Set ExpressionVariable to a value

```python
# Find our device
existing_devices = resourceManager.allResources(ExpressionVariablesDevice)
for device in existing_devices:
    if device.path == 'objects/ExpressionVariablesDevice/PluginVariable.apx':
        for var in device.container.variables:
            if var.name == 'plugin_value':
                var.defaultFloat = {value}
                print("Updated plugin_value to %f" % {value})
                break
```

_See the [Expressions Guide](/python-api/guides/expressions) for more details on working with expression variables._

## Create a 5x5 grid of LED screens with rainbow keyframes

```python
def createOddEvenLEDScreenGrid(nXScreens, nYScreens, screen_size, screen_gap):
    
    screen_offset = screen_size + 2*screen_gap
    
    x_offset = (nXScreens/2)*screen_offset - screen_offset/2
    y_offset = screen_size/2
    
    mapping_even_path = 'objects/directprojection/even_screens_direct.apx'
    mapping_even = resourceManager.loadOrCreate(mapping_even_path, DirectProjection)
    
    mapping_odd_path = 'objects/directprojection/odd_screens_direct.apx'
    mapping_odd = resourceManager.loadOrCreate(mapping_odd_path, DirectProjection)
    
    for i in range(nYScreens):
        for j in range (nXScreens):
            new_resource = resourceManager.loadOrCreate(
                'objects/ledscreen/ledscreen_{idx}.apx'.format(
                    idx=i*nYScreens + j ), LedScreen)

            new_resource.scale = Vec(screen_size, screen_size, 0)
            new_resource.offset = Vec(-x_offset + screen_offset*j, y_offset + screen_offset*i, 0)
            new_resource.resolution = Vec(1920, 1080)
            
            if i % 2 == 0:
                mapping_even.screens.append(new_resource)
            else:
                mapping_odd.screens.append(new_resource)
            
    return [mapping_even_path, mapping_odd_path]

def createVideoLayer(layer_name, start_time_seconds, length_seconds):
    return guisystem.track.addNewLayer(VariableVideoModule, start_time_seconds, length_seconds, layer_name) 

def makeSineWaveKeyFrame(layer, field, initial_value, offset_value):
    import math
    layer.fields[field].disableSequencing = False
    for i in range(1, 60):
        layer.fields[field].sequence.setFloat(i, offset_value + initial_value*math.sin(math.pi*((5 - (i % 10) )/5.0)))
    
if __name__ == '__main__':
    
    mapping_paths = createOddEvenLEDScreenGrid(5, 5, 5, 0.5)
    
    layer_1 = createVideoLayer('layer_1', 0, 60)
    layer_1.fields[10].sequence.setString(0, 'objects/videoclip/sample/george.jpg.apx')
    layer_1.fields[2].sequence.setString(0, mapping_paths[0])
    
    layer_2 = createVideoLayer('layer_2', 0, 60)
    layer_2.fields[10].sequence.setString(0, 'objects/videoclip/sample/ada.jpg.apx')
    layer_2.fields[2].sequence.setString(0, mapping_paths[1])
    
    makeSineWaveKeyFrame(layer_1, 20, 360, 0) # Hue
    makeSineWaveKeyFrame(layer_1, 36, 1, 1) # Size
    makeSineWaveKeyFrame(layer_1, 41, 90, 0) # Rotation
    
    makeSineWaveKeyFrame(layer_2, 20, -360, 0) # Hue
    makeSineWaveKeyFrame(layer_2, 36, 1, 1) # Size
    makeSineWaveKeyFrame(layer_2, 41, -90, 0) # Rotation
```

_This example demonstrates creating stage objects, managing resources, and working with layers and sequences. See the [Stage Guide](/python-api/guides/stage), [Resources Guide](/python-api/guides/resources), and [Track & Sequencing Guide](/python-api/guides/track-and-sequencing) for more details._

## Locate all feed rectangles

```python
import json

feed_scenes = resourceManager.allResources(FeedScene)
output = []

for scene in feed_scenes:
    scene_info = {
        "Scene UID": str(scene.uid),
        "FeedRects": []
    }
    
    if hasattr(scene, "feedRects"):
        for rect in scene.feedRects:
            rect_info = {
                "FeedRect UID": str(rect.uid),
                "Local Position and Size": str(rect.rect) if hasattr(rect, "rect") else "Not found",
                "Screen Position and Size": str(rect.screenRect) if hasattr(rect, "screenRect") else "Not found",
                "Output Rectangle": str(rect.outputRect) if hasattr(rect, "outputRect") else "Not found"
            }

            # Reference Display Extraction
            if hasattr(rect, "referenceDisplay"):
                ref_disp = rect.referenceDisplay
                ref_info = {
                    "Reference Display": str(ref_disp),
                    "uid": str(ref_disp.uid) if hasattr(ref_disp, "uid") else "Unknown",
                    "path": str(ref_disp.path) if hasattr(ref_disp, "path") else "Unknown"
                }
                if hasattr(ref_disp, "parent"):
                    parent = ref_disp.parent
                    ref_info["Parent UID"] = str(parent.uid) if hasattr(parent, "uid") else "Unknown"
                    ref_info["Parent Path"] = str(parent.path) if hasattr(parent, "path") else "Unknown"
                rect_info["Reference Display Info"] = ref_info

            # Display Target Extraction
            if hasattr(rect, "displayTarget"):
                disp_target = rect.displayTarget
                disp_info = {
                    "Display Target": str(disp_target),
                    "uid": str(disp_target.uid) if hasattr(disp_target, "uid") else "Unknown"
                }
                rect_info["Display Target Info"] = disp_info

            scene_info["FeedRects"].append(rect_info)

    output.append(scene_info)

print(json.dumps(output, indent=4))
```

_See the [Feed Operations Guide](/python-api/guides/feed) for comprehensive information on working with feeds and feed rectangles._

* * *

## Python API Guides

For more comprehensive guides on working with the Designer Python API, check out these resources:

-   **[Audio](/python-api/guides/audio)** - Managing audio tracks, files, and volume control
-   **[Calibration](/python-api/guides/calibration)** - Projector calibration workflows (OmniCal, QuickCal, Manual)
-   **[D3Net](/python-api/guides/d3net)** - Configuring D3Net machines, roles, and director/understudy modes
-   **[Devices](/python-api/guides/devices)** - Working with the Device Manager and device types
-   **[Expressions](/python-api/guides/expressions)** - Expression syntax and usage
-   **[Feed Operations](/python-api/guides/feed)** - Managing feeds, rectangles, and output configuration
-   **[Resources](/python-api/guides/resources)** - Resource management patterns and best practices
-   **[Stage](/python-api/guides/stage)** - Working with stage objects, positioning, and DMX lighting
-   **[Track & Sequencing](/python-api/guides/track-and-sequencing)** - Layers, cues, tags, and keyframe sequences
-   **[Transports](/python-api/guides/transports)** - Transport control, timecode, and event transports
-   **[Utility Functions](/python-api/guides/utility)** - Common utility types and functions
-   **[Video Input](/python-api/guides/video-input)** - Working with video input resources

For detailed API reference documentation, see the [API Documentation](/python-api/docs).
