How to change plane 'materials' property (texture) from my script

I need to change a plane ‘materials’ property from the script. At the moment I am doing it in a very complex way - by creating several planes positioned exactly at the same location and having different texture each. Then I trigger ‘visible’ property of each plane through the scenes (I have a separate scene created for each case). The relevant part of the code is below. It works but it is unnecessarily complex and expanding this to several areas that I need to ‘trigger’ will increase the complexity of the code and resources required. It would be much easier if I could change the texture (basically link another .png image already existing in the media library) from the script.

var LookingIn = 0;

var myFunction = function() {

// if Kitchen 99 is shown switch to LivingRoom and reset
if (LookingIn == 3) {
    symbol.controllers.display.elements.ShowLivingRoom.activate();

// LookingIn = 0;
Z.everyOff(myFunction);
}

// if Kitchen66% is shown switch to Kitchen99
if (LookingIn == 2 ) {
    symbol.controllers.display.elements.ShowKitchen99.activate();
    LookingIn = 3;
}

// if Kitchen33% is shown switch to Kitchen 66
if (LookingIn == 1) {
    // Switch to Kitchen66 and increase LookingIn
    symbol.controllers.display.elements.ShowKitchen66.activate();
    LookingIn = 2;
}

}

parent.on(“intersectionenter”, (e) => {
// Runs when intersectionenter occurs on the parent node
// The argument e contains useful info about this event:
// https://docs.zap.works/studio/scripting/reference/object/events/intersectionenter/

// Show Kitchen 33%
symbol.controllers.display.elements.ShowKitchen33.activate();
// Activate every 1000ms myFunction
LookingIn = 1;
Z.every(1000,myFunction);

});

parent.on(“intersectionleave”, () => {
// Runs when intersectionleave occurs on the parent node
// https://docs.zap.works/studio/scripting/reference/object/events/intersectionleave/

// differentiate if intersection is left after living room is shown or not
// Reset all when intersection was left and return back to Kitchen00
if (LookingIn != 3) {
    symbol.controllers.display.elements.ShowKitchen.activate();
    Z.everyOff(myFunction);
}
LookingIn = 0;

});

1 Like

Hi marko!

Yup that’s definitely possible. There are a few ways to change material. The easiest is to use the object’s skin(...) function:

Plane0.skin(symbol.mediaFiles.example_jpg);

You can drag the media file from the library into your script to get that ‘symbol.mediaFiles...’ bit.

The docs article for the skin(...) function is over here:
https://docs.zap.works/studio/scripting/reference/object/functions/skin-setter/

Does that help?

Cheers,
Connell

2 Likes

This is excellent. Thank you very much.

Thx for the info… works well… Now!!! i’m trying to bring it a notch more automated by creating and array with all my images names in it, then I would call it depending on the button (target) pressed…

Plane0.skin(symbol.mediaFiles.imgArray[0]); returns an error…

Also can I declare my variables in the root and access them through different scripts?

thx

Marc.

Hi Marc,

You can use the export keyword before variables and functions to be able to access them from other scripts.

Simply drag the script you’ve ‘exported’ a variable/function from into the script you want to access it from, creating a variable of the script, and you can then access exported variables and functions using the following syntax:

scriptVariableName.exportedVariableName

As for your error, symbol.mediaFiles is a dictionary of the media files and it’s best not to modify that. You’re better off storing the files in an array of image textures and accessing them from there.

Hope this helps :slight_smile:

Thanks,
Mark

Thank you Mark, I will give it a try…

Cheers!

Marc.