Model

Functions and messages for interacting with model components.

animation

hash model animation

The current animation set on the component. The type of the property is hash.

EXAMPLES

How to read the current animation from a model component:

function init(self)
  -- Get the current animation on component "model"
  local animation = go.get("#model", "animation")
  if animation == hash("run_left") then
    -- Running left. Do something...
  end
end


cursor

number model cursor

The normalized animation cursor. The type of the property is number.

Please note that model events may not fire as expected when the cursor is manipulated directly.

EXAMPLES

How to get the normalized cursor value:

function init(self)
  -- Get the cursor value on component "model"
  cursor = go.get("#model", "cursor")
end

How to animate the cursor from 0.0 to 1.0 using linear easing for 2.0 seconds:

function init(self)
  -- Get the current value on component "model"
  go.set("#model", "cursor", 0.0)
  -- Animate the cursor value
  go.animate("#model", "cursor", go.PLAYBACK_LOOP_FORWARD, 1.0, go.EASING_LINEAR, 2)
end


model.cancel()

cancel all animation on a model

Cancels all animation on a model component.

PARAMETERS

url -

string | hash | url the model for which to cancel the animation


model.get_go()

retrieve the game object corresponding to a model skeleton bone

Gets the id of the game object that corresponds to a model skeleton bone. The returned game object can be used for parenting and transform queries. This function has complexity O(n), where n is the number of bones in the model skeleton. Game objects corresponding to a model skeleton bone can not be individually deleted.

PARAMETERS

url -

string | hash | url the model to query

bone_id -

string | hash id of the corresponding bone

RETURN

id -

hash id of the game object

EXAMPLES

The following examples assumes that the model component has id "model".

How to parent the game object of the calling script to the "right_hand" bone of the model in a player game object:

function init(self)
    local parent = model.get_go("player#model", "right_hand")
    msg.post(".", "set_parent", {parent_id = parent})
end


model.play_anim()

play an animation on a model

Plays an animation on a model component with specified playback mode and parameters.

An optional completion callback function can be provided that will be called when the animation has completed playing. If no function is provided, a model_animation_done message is sent to the script that started the animation.

The callback is not called (or message sent) if the animation is cancelled with model.cancel. The callback is called (or message sent) only for animations that play with the following playback modes:

PARAMETERS

url -

string | hash | url the model for which to play the animation

anim_id -

string | hash id of the animation to play

playback -

constant playback mode of the animation

[play_properties] -

table optional table with properties

Play properties table:

blend_duration
number Duration of a linear blend between the current and new animation.
offset
number The normalized initial value of the animation cursor when the animation starts playing.
playback_rate
number The rate with which the animation will be played. Must be positive.

[complete_function] -

function(self, message_id, message, sender)) function to call when the animation has completed.

self
object The current object.
message_id
hash The name of the completion message, "model_animation_done".
message
table Information about the completion:
sender
url The invoker of the callback: the model component.

EXAMPLES

The following examples assumes that the model has id "model".

How to play the "jump" animation followed by the "run" animation:

local function anim_done(self, message_id, message, sender)
  if message_id == hash("model_animation_done") then
    if message.animation_id == hash("jump") then
      -- open animation done, chain with "run"
      local properties = { blend_duration = 0.2 }
      model.play_anim(url, "run", go.PLAYBACK_LOOP_FORWARD, properties, anim_done)
    end
  end
end

function init(self)
    local url = msg.url("#model")
    local play_properties = { blend_duration = 0.1 }
    -- first blend during 0.1 sec into the jump, then during 0.2 s into the run animation
    model.play_anim(url, "jump", go.PLAYBACK_ONCE_FORWARD, play_properties, anim_done)
end


model.reset_constant()

reset a shader constant for a model

Resets a shader constant for a model component. The constant must be defined in the material assigned to the model. Resetting a constant through this function implies that the value defined in the material will be used. Which model to reset a constant for is identified by the URL.

PARAMETERS

url -

string | hash | url the model that should have a constant reset.

constant -

string | hash name of the constant.

EXAMPLES

The following examples assumes that the model has id "model" and that the default-material in builtins is used, which defines the constant "tint". If you assign a custom material to the model, you can reset the constants defined there in the same manner.

How to reset the tinting of a model:

function init(self)
    model.reset_constant("#model", "tint")
end


model.set_constant()

set a shader constant for a model

Sets a shader constant for a model component. The constant must be defined in the material assigned to the model. Setting a constant through this function will override the value set for that constant in the material. The value will be overridden until model.reset_constant is called. Which model to set a constant for is identified by the URL.

PARAMETERS

url -

string | hash | url the model that should have a constant set

constant -

string | hash name of the constant

value -

vector4 value of the constant

EXAMPLES

The following examples assumes that the model has id "model" and that the default-material in builtins is used, which defines the constant "tint". If you assign a custom material to the model, you can set the constants defined there in the same manner.

How to tint a model to red:

function init(self)
    model.set_constant("#model", "tint", vmath.vector4(1, 0, 0, 1))
end


model_animation_done

reports the completion of a Model animation

This message is sent when a Model animation has finished playing back to the script that started the animation.

No message is sent if a completion callback function was supplied when the animation was started. No message is sent if the animation is cancelled with model.cancel(). This message is sent only for animations that play with the following playback modes:

PARAMETERS

animation_id -

hash the id of the completed animation

playback -

constant the playback mode of the completed animation

EXAMPLES

function on_message(self, message_id, message, sender)
  if message_id == hash("model_animation_done") then
    if message.animation_id == hash("run") and message.playback == go.PLAYBACK_ONCE_FORWARD then
      -- The animation "run" has finished running forward.
    end
  end
end


playback_rate

number model playback_rate

The animation playback rate. A multiplier to the animation playback rate. The type of the property is number.

EXAMPLES

How to set the playback_rate on component "model" to play at double the current speed:

function init(self)
  -- Get the current value on component "model"
  playback_rate = go.get("#model", "playback_rate")
  -- Set the playback_rate to double the previous value.
  go.set("#model", "playback_rate", playback_rate * 2)
end

The playback_rate is a non-negative number, a negative value will be clamped to 0.


texture0

hash model texture0

READ ONLY Returns the texture path hash of the model. Used for getting/setting resource data

EXAMPLES

See resource.set_texture for an example on how to set the texture of an atlas.