> ## Documentation Index
> Fetch the complete documentation index at: https://rive.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# ViewModel

A [ViewModel](/scripting/api-reference/interfaces/view-model) provides access to named properties used for data binding
between your application and a Rive file.

## Fields

### `name`

The name of the view model.

(Coming soon)

## Methods

### `getNumber`

<div class="signature">
  ```lua theme={null}
  getNumber(name: string) -> Property<number>?
  ```
</div>

Returns the numeric property with the given name, or nil if not found.

```lua theme={null}
local vmi = context:viewModel()
if vmi then
  local score = vmi:getNumber('score')
  if score then
    print(score.value)
  end
end
```

### `getTrigger`

<div class="signature">
  ```lua theme={null}
  getTrigger(name: string) -> PropertyTrigger?
  ```
</div>

Looks up a trigger property by name.
Returns a [PropertyTrigger](/scripting/api-reference/data-value/property-trigger).

```lua theme={null}
local vmi = context:viewModel()
if vmi then
  local myTrigger = vmi:getTrigger('myTrigger')
  if myTrigger then
    myTrigger:fire()
  end
end
```

### `getString`

<div class="signature">
  ```lua theme={null}
  getString(name: string) -> Property<string>?
  ```
</div>

Looks up a string property by name.
Returns a [DataValueString](/scripting/api-reference/data-value/data-value-string).

```lua theme={null}
local vmi = context:viewModel()
if vmi then
  local heading = vmi:getString('heading')
  if heading then
    print(heading.value)
  end
end
```

### `getBoolean`

<div class="signature">
  ```lua theme={null}
  getBoolean(name: string) -> Property<boolean>?
  ```
</div>

Looks up a boolean property by name.
Returns a [DataValueBoolean](/scripting/api-reference/data-value/data-value-boolean).

```lua theme={null}
local vmi = context:viewModel()
if vmi then
  local darkMode = vmi:getBoolean('darkMode')
  if darkMode then
    print(darkMode.value)
  end
end
```

### `getColor`

<div class="signature">
  ```lua theme={null}
  getColor(name: string) -> Property<Color>?
  ```
</div>

Looks up a color property by name.
Returns a [DataValueColor](/scripting/api-reference/data-value/data-value-color).

```lua theme={null}
local vmi = context:viewModel()
if vmi then
  local primaryColor = vmi:getColor('primaryColor')
  if primaryColor then
    primaryColor.value = Color.rgba(255, 0, 0, 155)
  end
end
```

### `getList`

<div class="signature">
  ```lua theme={null}
  getList(name: string) -> PropertyList?
  ```
</div>

Looks up a List property by name.
Returns a [PropertyList](/scripting/api-reference/interfaces/property-list).

```lua theme={null}
local vmi = context:viewModel()
if vmi then
  local enemies = vmi:getList('enemies')
  if enemies then
    enemies:pop()
  end
end
```

### `getViewModel`

<div class="signature">
  ```lua theme={null}
  getViewModel(name: string) -> PropertyViewModel?
  ```
</div>

Looks up a view model property by name. Returns a [PropertyViewModel](/scripting/api-reference/interfaces/property-view-model).

```lua highlight={3} theme={null}
local vmi = context:viewModel()
if vmi then
    local nestedVM = vmi:getViewModel('MyVM')
end
```

### `getEnum`

<div class="signature">
  ```lua theme={null}
  getEnum(name: string) -> PropertyEnum?
  ```
</div>

Looks up an enum by name.
Returns a [PropertyEnum](/scripting/api-reference/interfaces/property-enum).

```lua highlight={3} theme={null}
local vmi = context:viewModel()
if vmi then
    local textAlignment = vmi:getEnum('textAlignment')
end
```

### `instance`

<div class="signature">
  ```lua theme={null}
  instance(instanceName: string?) -> ViewModel
  ```
</div>

Creates a new instance of the ViewModel.
Pass instanceName to specify an existing instance from the file to use as template

```lua highlight={4} theme={null}
function init(self: VMNameNode, context: Context): boolean
  local vm = context:viewModel()
  if vm then
    local newInstance = vm:instance()
    local age = newInstance:getNumber('age')
    if age then
      age.value = 99
    end
  end

  return true
end
```

### `getIndex`

<div class="signature">
  ```lua theme={null}
  getIndex() -> number
  ```
</div>

Returns the index.
Returns -1 if view model does not have an index available.

### `getImage`

<div class="signature">
  ```lua theme={null}
  getImage(name: string) -> Property<Image>?
  ```
</div>

Looks up an image by name.
Returns a `Property<Image>`.

```lua highlight={3} theme={null}
local vmi = context:viewModel()
if vmi then
    local image1 = vmi:getImage('avatar')
end
```
