Replying to my own message here is something that seems to work to populate a vtkPolyData from numpy arrays, comments welcome:<div><br></div><div><div> def createPolyData(self, verts, tris):</div><div> """Create and return a vtkPolyData.</div>
<div> </div><div> verts is a (N, 3) numpy array of float vertices</div><div><br></div><div> tris is a (N, 1) numpy array of int64 representing the triangles</div><div> (cells) we create from the verts above. The array contains </div>
<div> groups of 4 integers of the form: 3 A B C</div><div> Where 3 is the number of points in the cell and A B C are indexes</div><div> into the verts array.</div><div> """</div>
<div> </div><div> # save, we share memory with the numpy arrays</div><div> # so they can't get deleted</div><div> self.verts = verts </div><div> self.tris = tris</div><div> </div>
<div> poly = vtk.vtkPolyData()</div><div> </div><div> points = vtk.vtkPoints()</div><div> points.SetData(numpy_to_vtk(verts)) </div><div> poly.SetPoints(points)</div><div> </div>
<div> cells = vtk.vtkCellArray()</div><div> cells.SetCells(len(tris) / 4, numpy_to_vtkIdTypeArray(tris)) </div><div> poly.SetPolys(cells)</div><div> </div><div> return poly</div>
<div>
<br></div><div><br></div><br><div>-Philip</div><br>
<br><br><div class="gmail_quote">On Sat, Oct 29, 2011 at 4:48 PM, Philip Winston <span dir="ltr"><<a href="mailto:pwinston@gmail.com">pwinston@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div>Are there any examples showing how to populate a vtkPolyData with verts/triangles from numpy arrays? </div><div><br></div><div>I have a solution right now where I loop through by hand adding points and triangles, but it is slow.</div>
<div><br></div><div>I see there is a numpy_to_vtk utility function, but I'm not clear how to setup my numpy arrays and what vtkPolyData functions I need to call. I'm imagining I want one Nx3 array with a vert per row (X Y Z). And a second Nx3 array of vert indexes with one triangle per row (v1 v2 v3)? But I can create whatever numpy arrays are needed, I just need help slurping them into a vtkPolyData.<br>
<br></div><div>Thanks!</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>-Philip</div><br>
</font></span></blockquote></div><br></div>