Page 1 of 1

Error in creating a visible object for GUI In Matlab

Posted: Mon May 11, 2020 2:57 pm
by sanchanak
Hi, I am trying to add a body to the model and make it a visible object to the GUI using Matlab. I got a code snippet for doing the same in OpenSim 3.3 and trying to replicate this for OpenSim4.

Code: Select all

% Create the Right Hand Sphere
rightHandSphere = Body();
rightHandSphere.setName('RightHandSphere');
rightHandSphere.setMass(0.3);
rightHandSphere.setInertia(Inertia(0,0,0,0,0,0));

% Add Visible Object for GUI
rightHandSphere.addDisplayGeometry(strcat('sphere.vtp'));
rightHandSphere.getDisplayer().setScaleFactors([0.1, 0.1, 0.1]);
I have modified the code to

Code: Select all

rightHandSphere = Body();
rightHandSphere.setName('RightHandSphere');
rightHandSphere.setMass(0.3);
rightHandSphere.setInertia(Inertia(0,0,0,0,0,0));
% Add Visible Object for GUI
rightHandSphere.attachGeometry(Mesh(strcat('sphere.vtp')));
rightHandSphere.upd_scale_factors().set_scale_factors([0.1, 0.1, 0.1]);
By doing this I get an error saying

Mesh sphere.vtp not connected to model..ignoring
Undefined function 'upd_scale_factors' for input arguments of type 'org.opensim.modeling.Body
'

Please let me know how to specify the .vtp/.stl/.obj file and scale factor for a newly defined body to be displayed as a visible object in the GUI.

Regards,
Sanchana

Re: Error in creating a visible object for GUI In Matlab

Posted: Mon May 11, 2020 4:48 pm
by aymanh
Hello,

I would recommend you check the API examples in version 4.0+, described here
https://simtk-confluence.stanford.edu/d ... pleScripts

In particular check the script OpenSimCreateTugOfWarModel.m included with the distribution since it creates bodies and attaches geometry to them. The API has changed significantly from 3.3 to 4.0+. Specifically in your case, the Mesh should be a part of the model and added as a component to the model and wired up before it can be used.

Hope this helps,
-Ayman

Re: Error in creating a visible object for GUI In Matlab

Posted: Mon Mar 01, 2021 10:46 am
by mn13rt
Hi,

I have come across the same error.
Mesh SimpHip.STL not connected to model..ignoring
Based on the previous response I looked at OpenSimCreateTugOfWarModel.m and it looks like it only flags the above error when using a mesh. In the visualiser, the mech does show up but I still get the error.

OpenSimCreateTugOfWarModel.m Code

Code: Select all

% Instantiate a Body with mass, inertia, and a display geometry
block = Body();
block.setName('Block');
block.setMass(20);
block.setMassCenter(zeroVec3);
block.setInertia(Inertia(0.133,0.133,0.133,0,0,0));
% Add display geometry for the block
block.attachGeometry(Brick(Vec3(0.05)));
My Code

Code: Select all

pelvisExo = Body();
pelvisExo.setName('Pelvis');
pelvisExo.setMass(1);
pelvisExo.setMassCenter(Vec3(0));
pelvisExo.setInertia(Inertia(1,1,1,0,0,0));
% Add geometry for display
pelvisExo.attachGeometry((Mesh('SimpHip.STL')));

I tried the following to add the mesh to the model as a separate component based on this statement.
the Mesh should be a part of the model and added as a component to the model and wired up before it can be used.

Code: Select all

meshTest = PhysicalOffsetFrame('meshTest',Pelvis,Transform(Vec3(0.05,0.075,0)));
meshTest.attachGeometry(Mesh('SimpHip.STL'));
LowerExo.addComponent(meshTest);
It did not have the desired effect. While I have been hunting through the API documentation please may you elaborate on what you mean in the sentence quoted above? Any hints would be welcomed!

Re: Error in creating a visible object for GUI In Matlab

Posted: Mon Mar 01, 2021 11:11 am
by aymanh
Hello,

The reference to visible object (on the thread title) is misleading since 'visible object' and 'display geometry' are all obsolete as of version 4.0+.

It is important that after you finish all model changes (e.g. addComponent, attachGeometry etc.) that you call

Code: Select all

%% Finalize connections so that sockets connectees are correctly saved
model.finalizeConnections();
It is hard to troubleshoot a code snippet from a longer script, if you can reproduce the issue in a small self contained script and post it that would be easier for us and other users to reproduce and to help.

Best regards,
-Ayman

Re: Error in creating a visible object for GUI In Matlab

Posted: Tue Mar 02, 2021 11:08 am
by mn13rt
Hi,

Thank you for your quick response. I have produced a piece of sample code that isolates the issue (below). Still getting the error:
Mesh Tube.STL not connected to model..ignoring

From debugging it appears that the error occurs when model.addBody(tube) is used. From my previous post in this thread, there is only an issue when the Mesh() function is used. In the visualiser, the "tube" does show up as expected and moves as expected (it just drops). Given that the geometry is purely for a visual representation and it is showing up in the visualiser, can the error just be ignored or does it mean there is still something wrong with the code?

Code: Select all

% Test.m

% Clear Workspace
clear;
close all;
clc;

% Import the OpenSim libraries. 
import org.opensim.modeling.*


%% Instantiate an Leg OpenSim Model
model = Model();
model.setName('Test');
model.setUseVisualizer(true); % Initiate visualiser - not necessary for data output

% Get a reference to the ground object
ground = model.getGround();

% Define the acceleration of gravity
model.setGravity(Vec3(0, -9.80665, 0));
                        
%% Create Tube
tube = Body();
tube.setName('Tube');
tube.setMass(1);
tube.setInertia(Inertia(1));

% Create form 
shape = Mesh('Tube.STL'); % Import STL mesh
shape.setColor(Vec3(1)); % green(?)
tube.attachGeometry(shape); % Add tube geometry to body

% Add Body to the Model
model.addBody(tube);

% Create free joint to set ball initial position
tubeHang    = FreeJoint('freeHang',...  % Joint Name
                                ground,...             % Parent Frame
                                Vec3(0,0.51,0),...   % Translation in Parent Frame
                                Vec3(0,0,0),...% Orientation in Parent Frame
                                tube,...           % Child Frame
                                Vec3(0,0,0),...    % Translation in Child Frame
                                Vec3(0,0,0));   % Orientation in Child Frame

% Add Joint to the Model
model.addJoint(tubeHang);

model.finalizeConnections();

% %% Initialize the System (checks model consistency).
state = model.initSystem();
% 
% % Simulate the model.
osimSimulate(model, state, 2.0);
Best wishes,
Rory