Cycling through an array of style transfer models

I’m trying to make a simple tap-to-change effect for viewing a handful of style transfer models. I built this off of the official style transfer template and stripped out all of the irrelevant code, just to make it easier to understand.

For some reason, when it loops back to the first model in the array, it won’t show that one on screen. It’s forever stuck showing the last model in the array.

I tried stopping the models from running, but then they would get stuck in the Idle state and wouldn’t respond when I tried running them again. This approach sets the component’s enabled to false

Here’s the script. Maybe there’s something blatantly wrong with it :man_shrugging:

//@input Component.MLComponent[] mlComponents
// match your model output name
//@input string outputName

//@input Component.MaterialMeshVisual outputImage
//@ui {"widget":"separator"}
//@input bool advanced
//@input SceneObject loader {"showIf" : "advanced"}

var currentIndex = -1

var mlComponents, 
  frameProcessed = false

function init() {

function nextModel(){

  if(currentIndex >= 0) {
    mlComponent.enabled = false
  currentIndex = currentIndex % script.mlComponents.length
  print('currentIndex: ' + currentIndex)

  mlComponent = script.mlComponents[currentIndex]
  mlComponent.enabled = true
  mlComponent.onLoadingFinished = onMLLoaded
  if(mlComponent.state === MachineLearning.ModelState.Idle) print('Idle')
  if(mlComponent.state === MachineLearning.ModelState.Loading ) print('Loading')
  if(mlComponent.state === MachineLearning.ModelState.Running ) print('Running')
  if(mlComponent.state === MachineLearning.ModelState.NotReady) print('NotReady')
  if(mlComponent.state === MachineLearning.ModelState.NotReady){[])


function onMLLoaded() {
  frameProcessed = false
  mlComponent.onRunningFinished = onMLFinishedProcessingFirstFrame // wrapFunction(mlComponent.onRunningFinished, onMLFinishedProcessingFirstFrame)
  if(mlComponent.state !== MachineLearning.ModelState.Running){
    mlComponent.runScheduled(true, MachineLearning.FrameTiming.Update, MachineLearning.FrameTiming.OnRender)    

function onMLFinishedProcessingFirstFrame() {
  if (!frameProcessed) {
    if (script.loader) {
      script.loader.enabled = false
    frameProcessed = true

function setOutputTexture(fromOutput) {
  if (fromOutput) {
    script.outputImage.mainPass.baseTex = mlComponent.getOutput(script.outputName).texture
  } else {
    script.outputImage.mainPass.baseTex = script.defaultTexture

1 Like

instead of changing the model u can just apply all the model to different screen image or so and u can toggle the enable.