1
$\begingroup$

I have problems getting my head around it:

I have two arrays:

verts = np.array([1, 1, 0,
                  2, 1, 0, 
                  3, 1, 0, 
                  1, 2, 0, 
                  2, 2, 0, 
                  3, 2, 0, 
                  1, 3, 0, 
                  2, 3, 0, 
                  3, 3, 0])

faces = np.array([0, 1, 4, 3,
                  1, 2, 5, 4, 
                  3, 4, 7, 6, 
                  4, 5, 8, 7])

The mesh (me) is prepared with the 9 verts from array "verts" in it already. Now i want to add the faces but the explanations i found were scarce at best.

So, how do i add faces using (an explanation would be great):

    me.loops.add() #should be 4 loops per (4-sided) face, right?
    me.loops.foreach_set("vertex_index",) #no idea...

    me.polygons.add(faces.size / 4) #4 Faces in this case
    me.polygons.foreach_set("loop_start",) #no idea... 
    me.polygons.foreach_set("loop_total",) #not sure... number of loops per face in a list: [4, 4, 4, 4] in this case?
    #me.polygons.foreach_set("vertices", ) #needed at all? List of vert coordinates if they were no verts in mesh already?

I would be very happy if anybody could shed some light on it for me.

EDIT After some investigation of "from_pydata" code in "bpy_types.py" as suggested by batFINGER i am a bit further but still not fully able to create the correct mesh.
I now have most of it right i think but because i have shared vertices for some of the loops and edges i am now pretty sure I need to use:

me.loops.foreach_set("vertex_index", vert_idx)

i thought i would need to add two verts per loop (start and end), so:

vert_idx = (0, 1, 
            1, 4, 
            4, 3, 
            3, 0, 
            1, 2, 
            2, 5, 
            5, 4, 
            4, 1, 
            3, 4, 
            4, 7, 
            7, 6, 
            6, 3, 
            4, 5, 
            5, 8, 
            8, 7, 
            7, 4)

or "faces" but both leads to a crash, like everything else i tried.

$\endgroup$
  • $\begingroup$ Related You really want to use Mesh.from_pydata(...) or a bmesh approach. Consider foreach get and set more a way to get or set property data to / from a flat list, rather than as a way to create meshes. $\endgroup$ – batFINGER Feb 18 '18 at 17:41
  • $\begingroup$ "You really want to use Mesh.from_pydata(...)" I do not. $\endgroup$ – CoD Feb 18 '18 at 17:44
  • $\begingroup$ Fair enough, change "you really want to use" to "I strongly suggest using" $\endgroup$ – batFINGER Feb 18 '18 at 17:51
  • $\begingroup$ Sounds better ;)... i need as much speed as possible creating large meshes (data created with numpy) and "foreach_set" method should be much faster (but i will not find out if i cannot get it running) ... $\endgroup$ – CoD Feb 18 '18 at 18:06
  • $\begingroup$ The code for from_pydata is in bpy_types.py as mentioned here $\endgroup$ – batFINGER Feb 18 '18 at 18:58
0
$\begingroup$

Got it, what was missing was not:

me.loops.foreach_set("vertex_index", vert_idx)

but correct:

me.polygons.foreach_set("vertices", ...

i tried it before and it crashed all the time because i forgot to ravel() the numpy array i used.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.