## Generate shape from a path via script (Path Shaping parameters with code)

Typically: "How do I... ", "How can I... " questions
Posts: 4
Joined: 18 Nov 2018, 13:46

### Generate shape from a path via script (Path Shaping parameters with code)

Hello
i have actually a problem with generating a path and make a shape (Wire in my case) from it via a child script.
I can use the Lua API functions to create a path and also insert control points and have a path easily. But i can't find a function to:
1. generate shape from this path and also
2. set Scaling factor (diameter of the wire generated by path)
This can be done manually with Path Shaping Parameters windows, but i can't generate shape and control the path shaping--> scaling factor via child script commands.

I would be so thankful for any suggestions.
Regards

fferri
Posts: 408
Joined: 09 Sep 2013, 19:28

### Re: Generate shape from a path via script (Path Shaping parameters with code)

Hi,

You can create any (mesh) shape you want in Lua with sim.createMeshShape.

With a bit of 3D geometry and trigonometric functions, you can certainly create a mesh for a wire of arbitrary radius.

Beware that this mesh will be non-convex, thus it will slow down your simulation if you want it to be respondable. Consider approximating it with pure cylinder shapes if you need it to be respondable.

Posts: 4
Joined: 18 Nov 2018, 13:46

### Re: Generate shape from a path via script (Path Shaping parameters with code)

fferri wrote:
11 Jun 2019, 09:12
Hi,

You can create any (mesh) shape you want in Lua with sim.createMeshShape.

With a bit of 3D geometry and trigonometric functions, you can certainly create a mesh for a wire of arbitrary radius.

Beware that this mesh will be non-convex, thus it will slow down your simulation if you want it to be respondable. Consider approximating it with pure cylinder shapes if you need it to be respondable.
Hello
Thank you so much. The reason that i want to generate shape from path is the Bezier control points. Actually i have a robot which is supposed to train with different type of wires. For that i need a function that takes in some points (ex. 10 bezier points) and a radius, then returns a shape (wire).
I assume with your idea i can only give the function vertices and indices as arrays.
I would be so thankful if you could help me understand the vertices and indices of sim.createMeshShape better. Actually i can make a single triangle with that, but how can i generate a mesh for something circular or a cylinder with triangles?

Code: Select all

 sim.createMeshShape(0,0,{0,0,0,0,0.1,0,0,0.1,0.1},{0,1,2})

Would you please refer me to a simple code or some keywords for 3D functions?
Thanks
Best

fferri
Posts: 408
Joined: 09 Sep 2013, 19:28

### Re: Generate shape from a path via script (Path Shaping parameters with code)

The vertices array contains x,y,z coordinates of vertices sequentially.
The indices array contains 0-based indices of points, which taken 3 by 3, defines triangles.
Simple example: to create a quad you would define vertices {x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4} and indices {0,1,2,0,2,3}.

To create an open cylinder from p to q with caps aligned to unit vectors s and t, you can do the following:

Code: Select all

    local v={}
local idx={}
local p={0.0,0.0,0.0}
local q={0.0,0.0,0.5}
local s={1,0,0}
local t={0,1,0}
local res=10
for j=1,res do
local a=math.pi*2*(j-1)/res
table.insert(idx,math.mod(j-1,res)*2)
table.insert(idx,math.mod(j,res)*2+1)
table.insert(idx,math.mod(j-1,res)*2+1)
table.insert(idx,math.mod(j-1,res)*2)
table.insert(idx,math.mod(j,res)*2)
table.insert(idx,math.mod(j,res)*2+1)
end
sim.createMeshShape(0,0,v,idx)
Now you can expand this to create multiple open cylinders aligned along a path.
Use sim.getPositionOnPath to get the position in an arbitrary point on the path.
Collect all the vertices in v, and all the indices in idx, then use sim.createMeshShape(0,0,v,idx) at the very last.

Posts: 4
Joined: 18 Nov 2018, 13:46

### Re: Generate shape from a path via script (Path Shaping parameters with code)

fferri wrote:
12 Jun 2019, 22:02
The vertices array contains x,y,z coordinates of vertices sequentially.
The indices array contains 0-based indices of points, which taken 3 by 3, defines triangles.
Simple example: to create a quad you would define vertices {x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4} and indices {0,1,2,0,2,3}.

To create an open cylinder from p to q with caps aligned to unit vectors s and t, you can do the following:
Thank you so much, i got the idea now.
Best,