- This topic has 2 replies, 3 voices, and was last updated 9 years ago by .
Viewing 3 posts - 1 through 3 (of 3 total)
Viewing 3 posts - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.
Home › Forum › SOFA › Programming with SOFA › [SOLVED] sofa+python
Hi,
I have some issues combining sofa and python.
I have a python script which adds new objects (named A1, A2,…) in my scene with a keyboard event.
A need to dynamically link a Data from these new objects (for instance, Vec3 positions) with an object already in the scene (named B).
I don’t know how to do it.
I tried to retrieve a vector of positions from B as a numpy array and add the new positions from Ai to it. But it did not work.
Has someone an idea ?
lionel
Hello Lionel,
The position vector is copied when accessed from python, so you need to copy it back for the changes to happen. When using numpy arrays, you also need to convert it to a python list first using .tolist(). Here is a short example:
import numpy as np
def createScene(node):
# create some dofs
dofs = node.createObject('MechanicalObject', template = 'Vec3')
# positions are copied
pos = dofs.position
# modification
pos[0][0] = 1
# debug
print 'before:', dofs.position
# commit changes
dofs.position = pos
# debug
print 'after:', dofs.position
# numpy array
pos = np.zeros( (10, 3) )
dofs.position = pos.tolist()
print 'numpy:', dofs.position
Hope this helps.
Best,
max.
edit: we really need a preview system for posts + unescaped code characters
Thank you for your reply Max.
This helped (I confirmed it !)
Cheers,
Hugo
WARNING
The forum has been moved to GitHub Discussions.
Old topics and replies are kept here as an archive.