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
//@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
script.createEvent("TapEvent").bind(nextModel)
var mlComponents,
mlComponent,
frameProcessed = false
function init() {
nextModel()
}
function nextModel(){
if(currentIndex >= 0) {
mlComponent.enabled = false
}
currentIndex++
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){
mlComponent.build([])
}else{
onMLLoaded()
}
}
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)
}else{
setOutputTexture(true)
}
}
function onMLFinishedProcessingFirstFrame() {
if (!frameProcessed) {
setOutputTexture(true)
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
}
}
init()