Programmatically add Actors to sim3d world in Simulink
33 views (last 30 days)
Show older comments
Alyssa
on 30 Aug 2024
Commented: iyanuoluwa Adeoye
on 23 Nov 2024 at 2:20
Hi everyone,
My language might be a bit non-technical, but I will try to explain myself clearly. I have a project in which I am trying to simulate a drone in a 3D environment. I would like to build that environment programmatically in Matlab, but run the simulation in Simulink (because there are various nice function blocks for camera sims, etc in Simulink). I would like to be able to Add and Remove Actors from the environment using Matlab functions or based on some struct/cell array/object list in Matlab. This is proving to be quite a challenge.
Unfortunately, it doesn't seem that I can create a 3D world object outside of Simulink that interfaces nicely with the aforementioned blocks (they require the inclusion of a '3D Scene Configuration' block in the same model, and I think it is this block that creates the sim3d world object).
I am now hoping that I can somehow get the name or handle of the world created by this scene config block and pass it to a Matlab function block, in which I can add the 3D actors. However, I have no idea how to find the handle or name. I've tried combing through the Model Explorer. Is there some version of "findObj" that works in Simulink?
Does anyone have a better suggestion? I'm sure this is a problem faced by many before me, and it is possible that my approach is not the best way.
0 Comments
Accepted Answer
Nishan Nekoo
on 30 Aug 2024
Hi Alyssa,
There are a few different approaches you can take here and I think this example could be very relevant for you: https://www.mathworks.com/help/sl3d/automate-virtual-assembly-line.html
" I would like to build that environment programmatically in Matlab, but run the simulation in Simulink"
The example above uses the Simulation 3D Actor block (labelled "Prepare World") to load in a .m file that contains several 3D Actors. This is how an environment built programmatically in MATLAB can be loaded into Simulink. Note that the .m file should not contain any notion of a sim3d.World. An alternative is to create a .mat file instead. This can be done by creating a world programmatically in MATLAB then while the simulation is running, right click in the Simulation 3D Viewer window and click "Save as MAT file".
"I would like to be able to Add and Remove Actors from the environment using Matlab functions or based on some struct/cell array/object list in Matlab"
If you use a .mat file instead of a .m file in the first step above, you can use the Simulation 3D Actor block and the "Inputs" tab to create input ports for any of the properties of the Actors that you added programmatically. This includes the "Hidden" property which you can use to effectively add or remove an actor from the environment.
I think this should help you get started, but please feel free to respond if you have any additional questions.
Nishan
9 Comments
Nishan Nekoo
on 22 Nov 2024 at 23:09
Hi @iyanuoluwa Adeoye, glad you managed to figure it out and apologies if my guidance was a little unclear before :)
Please don't hesitate to post a question if you run into anything else, we'd love to hear from you.
Nishan
iyanuoluwa Adeoye
on 23 Nov 2024 at 2:20
As a followup, and out of curiousity incase i need it, is there a way to programmatically interact with the simulink 3d world environment without the need for a callback to acquire an actors handle first, so currently using the OnClick or onHit the callbacks provides the handle of an actor within the 3d world and using that actor you can access all other actors using the parent/children properties, but if i wanted to access the actors without using a callback the commands i have tried so far have not worked, i have tried the following;
sim3d.internal.RootObject.Children.Sim3dActor10.Children.Heatgun;
sim3d.World.Actors.Sim3dActor10.Children.Heatgun;
a screenshot of my rootobject is below;
as seen in the screen shot, when i hover my cursor over the RootObject hyperlink, it show sim3d.Internal.RootObject which i assume is the toplevel, and from there tracing the Children and Actor relationships i would think i should be able to communicate with the actors from the matlab command prompt with this command(note: i traced the children/actor relationships to create the line of code): sim3d.internal.RootObject.Children.Sim3dActor10.Children.Heatgun;
but i get the following error: Unrecognized method, property, or field 'sim3d' for class 'sim3d.internal.RootObject'.
thanks for the help.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!