Animate 3D figure imported from stl
Show older comments
Hi,
I need to modify some code to work for my project.
I can't get the animation to work correctly. it looks like that.

robot looks like that

here is whole folder
I can't upload it direcly because of limit.
There's start.m file with correct order
%%%MAKE FIGURE WITH OBJECT (ROBOT ARM)
% Rysuj czlony
P2=C2.Vertices';
P3=C3.Vertices';
% ustawianie czlonow
P2=F12(0)*[P2; ones(1, size(P2, 2))];
%
C2R=C2;
%
C2R.Vertices=P2(1:3,1:size(P2, 2))';
%
P3=F12(0)*F23(0)*[P3;ones(1,size(P3, 2))];
%
C3R=C3;
C3R.Vertices=P3(1:3,1:size(P3, 2))';
%% rysowanie
figure(1);
axis equal;
grid on;
hold on;
czlon11=trisurf(C11.Vertices-1, C11.Faces(:,1), C11.Faces(:,2), C11.Faces(:,3), 'FaceColor',C11.FaceColor, 'EdgeColor','none');
lightangle(-45, 25);
lighting('flat');
czlon12=trisurf(C12.Vertices-1, C12.Faces(:,1), C12.Faces(:,2), C12.Faces(:,3), 'FaceColor',C12.FaceColor, 'EdgeColor','none');
lightangle(-45, 25);
lighting('flat');
czlon2=trisurf(C2.Vertices-1, C2.Faces(:,1), C2.Faces(:,2), C2.Faces(:,3), 'FaceColor',C2.FaceColor, 'EdgeColor','none');
lightangle(45, 65);
lighting('flat');
czlon3=trisurf(C3.Vertices-1, C3.Faces(:,1), C3.Faces(:,2), C3.Faces(:,3), 'FaceColor',C3.FaceColor, 'EdgeColor','none');
lightangle(45, 65);
lighting('flat');
clear P2 P3 C2R C3R;
fi1=[0:1:180, 180:-1:0];
fi2=[0:0.5:90, 90:-0.5:0];
fi3=[0:-0.5:-90, -90:0.5:0];
%%%%%%%%%%%Generate movie
% parametry
lbpow=2;
%przeliczenie fi na radjany
Fi1=fi1*pi/180;
Fi2=fi2*pi/180;
Fi3=fi3*pi/180;
[m,n]=size(fi1);
%pozycja zerowa
P11=C11.Vertices';
P12=C12.Vertices';
P2=C2.Vertices';
P3=C3.Vertices';
%%
% transformacja
for i=1:n
aP11=F01(Fi1(i))*[P11;ones(1,size(P11, 2))];
aP12=F01(Fi1(i))*[P12;ones(1,size(P12, 2))];
aP2=F01(Fi1(i))*F12(Fi2(i))*[P2;ones(1,size(P2, 2))];
aP3=F01(Fi1(i))*F12(Fi2(i))*F23(Fi3(i))*[P3;ones(1,size(P3, 2))];
% przesunięcie
set(czlon11,'Vertices',aP11(1:3,1:size(aP11, 2))');
set(czlon12,'Vertices',aP12(1:3,1:size(aP12, 2))');
set(czlon2,'Vertices',aP2(1:3,1:size(aP2, 2))');
set(czlon3,'Vertices',aP3(1:3,1:size(aP3, 2))');
film(i)=getframe;
end
%%
movie(film,lbpow)
clear P11 P12 P2 P3 Fi1 Fi2 Fi3
6 Comments
file = websave('matrixes','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1595496/matrixes.mat');
load('matrixes.mat');
figure
trisurf(C11.Vertices-1, C11.Faces(:,1), C11.Faces(:,2), C11.Faces(:,3), 'FaceColor',C11.FaceColor, 'EdgeColor','none')
hold on
trisurf(C12.Vertices-1, C12.Faces(:,1), C12.Faces(:,2), C12.Faces(:,3), 'FaceColor',C12.FaceColor, 'EdgeColor','none')
trisurf(C2.Vertices-1, C2.Faces(:,1), C2.Faces(:,2), C2.Faces(:,3), 'FaceColor',C2.FaceColor, 'EdgeColor','none')
trisurf(C3.Vertices-1, C3.Faces(:,1), C3.Faces(:,2), C3.Faces(:,3), 'FaceColor',C3.FaceColor, 'EdgeColor','none')
lightangle(45, 65)
lighting('flat')
% material('metal')
hold off
axis('equal')
view(-45, 20)
Ax = gca;
Ax.Visible = 0;
sgtitle('Assembled Segments')
Now that I know that this is a robot, the Robotics System Toolbox has some functions that can make articulating and moving this shape easier, however they are not trivial to work with. The applyTransform function may be the most useful, however there are others as well that could work.
meshC11 = extendedObjectMesh(C11.Faces,C11.Vertices-1);
meshC12 = extendedObjectMesh(C12.Faces,C12.Vertices-1);
meshC2 = extendedObjectMesh(C2.Faces,C2.Vertices-1);
meshC3 = extendedObjectMesh(C3.Faces,C3.Vertices-1);
And then plot them —
figure
show(meshC11);
show(meshC12);
show(meshC2);
show(meshC3);
sgtitle('Assembled Segments')
I cannot figure out how to change the colours to be what you originally wanted them to be. The show function has a ‘Children’ field that is a patch pbject, however that shows the element as a patch object with the correct colour, and not a trisurf object with a uniform surface colour. Those with more experience with these problems and functions than I am can likely help you with that aspect of it.
Applying the transforms correctly should let you change the orientation of the elements of the model and by doing this repetitively, animate it. I do not have the Robotics System Toolbox, so it may have its own animation capabilities, for example in Robot Simulation. If it does not, MATLAB has a number of them as described in the Animation documentation that you can use.
.
Star Strider
on 21 Jan 2024
I am not certain how you are attempting to animate it.
These worked for me to create the meshes:
meshC11 = extendedObjectMesh(C11.Faces,C11.Vertices-1);
meshC12 = extendedObjectMesh(C12.Faces,C12.Vertices-1);
meshC2 = extendedObjectMesh(C2.Faces,C2.Vertices-1);
meshC3 = extendedObjectMesh(C3.Faces,C3.Vertices-1);
figure
show(meshC11);
show(meshC12);
show(meshC2);
show(meshC3);
sgtitle('Assembled Segments')
I have no experience with the Robotics System Toolbox, (I do not have it) so I cannot experiment with any of its functions other than here, and have never animated a robot. I would start with the extendedObjectMesh results and go from there. The trisurf results are likely not going to work with that, other than by using the makehgtform function that I have used a few times, however not enough to be familiar with — much less proficient in — its use.
We likely have to wait until someone proficient in using the Robotics System Toolbox sees this and contributes something of value that will help you. Unfortunately, that is not me. (I am following this with that in mind.)
.
Star Strider
on 21 Jan 2024
So just to clarify, whoever has the skills to do the animation needs to use those files (that I assume are in the offsite location) rather than writing new code? (Animating robots is far outside my areas of expertise.)
TheIOzikI
on 21 Jan 2024
Answers (1)
Aman
on 5 Feb 2024
Hi Thel,
As per my understanding, you are trying to simulate a scenario for bot arm movement but are facing issues while creating animation for the same.
The issue that you are facing is due to the transformation that you are trying to do by the following piece of code, which is not transforming the vertices correctly, due to which you are seeing such an absurd figure.
% transformacja
for i=1:n
aP1=F01(d1(i))*[P1;ones(1,size(P1, 2))];
aP21=F01(d1(i))*F12(Fi2(i))*[P21;ones(1,size(P21, 2))];
aP22=F01(d1(i))*F12(Fi2(i))*[P22;ones(1,size(P22, 2))];
aP3=F01(d1(i))*F12(Fi2(i))*F23(Fi3(i))*[P3;ones(1,size(P3, 2))];
aP4=F01(d1(i))*F12(Fi2(i))*F23(Fi3(i))*F34(Fi4(i))*[P4;ones(1,size(P4, 2))];
% przesunięcie
set(czlon1,'Vertices',aP1(1:3,1:size(aP1, 2))');
set(czlon21,'Vertices',aP21(1:3,1:size(aP21, 2))');
set(czlon22,'Vertices',aP22(1:3,1:size(aP22, 2))');
set(czlon3,'Vertices',aP3(1:3,1:size(aP3, 2))');
set(czlon4,'Vertices',aP4(1:3,1:size(aP4, 2))');
film(i)=getframe;
end
A better approach for doing the same would be to use the "applyTranform" method, which applies forward transformation to the mesh, and once you have transformed vertices with you, you can call the "show" method in a loop to display the animation. Please refer to the following documentation, where the animation has been achieved using the described approach.
You can use the below matrices for rotation along the x, y, and z axes, respectively.
Rx = [1 0 0 0;
0 cos(theta_rad) -sin(theta_rad) 0;
0 sin(theta_rad) cos(theta_rad) 0;
0 0 0 1];
Ry = [cos(theta_rad) 0 sin(theta_rad) 0;
0 1 0 0;
-sin(theta_rad) 0 cos(theta_rad) 0;
0 0 0 1];
Rz = [cos(theta_rad) -sin(theta_rad) 0 0;
sin(theta_rad) cos(theta_rad) 0 0;
0 0 1 0;
0 0 0 1];
Hope it helps!
Categories
Find more on Robotics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

