Stan Davis avatar image
Stan Davis asked Stan Davis commented

Change Plane's Texture Image via Custom Code

I am trying to determine how to change a Plane's texture image using the imageobject attribute via code, but that approach could be wrong from the start.

This code is called via PF a few seconds into the sim run...

Object myplane;

myplane = Model.find("Plane7");

msg("", myplane.attrs.imageobject.value);

myplane.attrs.imageobject.value = "C:\\Users\\xxxxxx\\Documents\\FlexSim 2021 Projects\\Graphics\\green.png";

msg("", myplane.attrs.imageobject.value);

myplane.applyProperties();   // does not work

The plane's image on the screen does not change.

Although the 2nd msg shows that the imageobject has changed, the plane's texture field still shows the original image path...


However, the tree did change...


When I reset the model, the plane goes blank..


What am I doing wrong??


FlexSim 21.2.4
1638473089405.png (3.1 KiB)
1638473163090.png (36.7 KiB)
1638473261278.png (31.5 KiB)
· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Joerg Vogel avatar image Joerg Vogel commented ·
I think this image object belongs to media files, too. You need to replicate the procedure to load a file from the view structure of exchanging an image file under general object pane.
0 Likes 0 ·
Stan Davis avatar image Stan Davis Joerg Vogel commented ·
thanks Joerg.
0 Likes 0 ·
Connor A avatar image Connor A commented ·

Hi @Stan Davis, was one of Phil BoBo's or Joerg Vogel's answers helpful? If so, please click the "Accept" button at the bottom of the one that best answers your question. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always unaccept and comment back to reopen your question.

0 Likes 0 ·

1 Answer

Phil BoBo avatar image
Phil BoBo answered Stan Davis commented

The imageobject attribute specifies a path to a file that should be loaded when 3D media is loaded.

When you modify the value using the UI, it calls autoloadallmedia(object) to load any specified media files for that object into the loaded media list, including the specified texture file path on the imageobject attribute.

If you want to programmatically set the texture and load it, then you can set that attribute and call autoloadallmedia(object) in your code.

The texture that is drawn in the 3D view is based on the imageindexobject attribute. Each 3D shape, texture, or sound in the media list has an index that is used to reference it. This index changes depending on the order that the media is loaded. Use the following code to set the imageindexobject attribute to the texture index for the file specified on the imageobject attribute.

myObj.attrs.imageindexobject.value = gettextureindex(myObj.attrs.imageobject.value);

If you want to dynamically change a texture while the model is running, you should load that texture into the media list first, and then just change the imageindexobject attribute to point at the texture you want to draw at any given time. You don't want to call autoloadmedia() while the model is running.

You can load the media into the media list using the Preloaded Shapes and Images window from main menu option View > Media Files.

You can then change to one of those loaded media files using similar code as above. For example:

myObj.attrs.imageindexobject.value = gettextureindex("path\\to\\mytexture.png");

For the gettextureindex() command, use the path shown in the Preloaded Shapes and Images window.

Here's an example of these commands:

Object myplane = Model.find("Plane7");
myplane.attrs.imageobject.value = "modules\\ProcessFlow\\bitmaps\\image.png";
myplane.attrs.imageindexobject.value = gettextureindex(myplane.attrs.imageobject.value);
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Stan Davis avatar image Stan Davis commented ·
understood. thanks - Stan
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.