AnyScript and Python

Hello!

I am trying to automate some code for my model using Python.

The idea is that I am trying to find the optimal attachment points of certain ligaments/contact surfaces.

I have tried using this tutorial http://localhost:8892/notebooks/02_Generating_macros.ipynb and it works perfect. Only for variables composed of a value. But it doesn't work for variables composed of vectors or matrices.

So for instance, let's say I have a ligament that has positions {x,y,z}. What I am trying to do is vary x,y,z with +- 0.03 let's say, run the simulation and see the level of muscle activation, or the internal rotation of the said bones, etc..

I have tried in Python to just run one simulation, without varying anything, using the following command:

matrix = np.array([[-38.80857,125.3048,22.31599],[-34.80857, 122.3048, 22.31599]])*0.001

which is basically:

[[-0.03880857 0.1253048 0.02231599]
[-0.03480857 0.1223048 0.02231599]]

but when I try to run the macro, it always fails.

I have also tried without using the np.array:

matrix = ([-38.80857,125.3048,22.31599],[-34.80857, 122.3048, 22.31599])

which gives:

([-38.80857, 125.3048, 22.31599], [-34.80857, 122.3048, 22.31599])

And I was thinking it should work, but it doesn't...

I am thinking I am seeing this wrong and it is not just a matrix, since in AnyBody the position of the ligaments are:

{ {-38.80857, 125.3048, 22.31599},
{-34.80857, 122.3048, 22.31599}
}*0.001;

Could you give me any suggestions please?

Thanks in advance!

Best,
Diana

Hi Diana

You better show my what error you get from AnyBody.

I tried to recreate your problem but it seems to work correctly for me.

Take a look here. Maybe that can help.

Best wishes,
Morten

Hi Morten,

I was declaring the matrix in a wrong way. I have seen thanks to the link you provided, so thank you!

I have declared the matrix as shown below, and it works for editable values.
matrix = [
np.array([[-0.038,0.12,0.02],[-0.03, 0.12, 0.02]]),
]

But the error I am getting now is this one:

The values I am trying to edit (the ligament attachment points) are apparently non editable :thinking:

But if I go to the value in the AnyBodyScript and click on the object description, I see this:

From the image above I understand the object is edditable and the SetValue is Enabled, so I should be able to write new values, or am I understanding this wrong?

Thanks in advance!

Best,
Diana

Hi Dianna

You can force variable to not become constants by adding the DesignVar() functions in AnyBody.

AnyMatrix MyVar = DesignVar({{1,0,0},{0,1,0}});
1 Like

Thank you Morten! Now it works as intended!

This topic was automatically closed 125 days after the last reply. New replies are no longer allowed.