Change Plane's Texture Image via Custom Code

Change Plane's Texture Image via Custom Code

stan.e.davis
Advocate Advocate
260 Views
5 Replies
Message 1 of 6

Change Plane's Texture Image via Custom Code

stan.e.davis
Advocate
Advocate

[ FlexSim 21.2.4 ]

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...


1638473089405.png

However, the tree did change...

1638473163090.png



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

1638473261278.png


What am I doing wrong??


Thanks

0 Likes
Accepted solutions (1)
261 Views
5 Replies
Replies (5)
Message 2 of 6

joerg_vogel_HsH
Mentor
Mentor
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
Message 3 of 6

philboboADSK
Autodesk
Autodesk
Accepted solution

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";
//autoloadallmedia(myplane);
myplane.attrs.imageindexobject.value = gettextureindex(myplane.attrs.imageobject.value);


Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 4 of 6

connor_a
Not applicable

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
Message 5 of 6

stan.e.davis
Advocate
Advocate
understood. thanks - Stan
0 Likes
Message 6 of 6

stan.e.davis
Advocate
Advocate
thanks Joerg.
0 Likes