7
$\begingroup$

I want to do something like this with a python script and selecting the vertex in the middle:

I want this

I tried with many ideas as:

import bpy, bmesh


obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)


verts = [v for v in bm.verts if (v.select==True and not v.hide)]

for v in verts:
    for f in v.link_faces:

        if (f.index%2) != 0 :
            f.select = True
            obj.active_material_index = 3
            bpy.ops.object.material_slot_assign()
            f.select = False
        else:
            f.select = True
            obj.active_material_index = 4
            bpy.ops.object.material_slot_assign()
            f.select = False




bmesh.update_edit_mesh(me, True)

but this don't have good results.

wrong result

I'm tried with face.index, bool and other without good result.

$\endgroup$
9
$\begingroup$

I almost always look for hacks when doing simple tests like these (half of the time the function you spend hours on, Blender has the functionality tucked away in some menu) so if your use case is similar meshes I would probably use bpy.ops.mesh.select_nth(offset=1) to get the selections in a checkered pattern.

import bpy

# get the object
ob = bpy.context.active_object

# get the mesh
mesh = ob.data

# create a list with all faces
face_list = [face for face in mesh.polygons]

# select all faces for offset operator
for face in face_list:
    face.select = True

# toggle to edit mode
bpy.ops.object.mode_set(mode='EDIT')

# make sure face select mode is enabled
bpy.context.tool_settings.mesh_select_mode = [False, False, True]

# select every second face
bpy.ops.mesh.select_nth(offset=1)

# use second material slot
ob.active_material_index = 1

# assign the material
bpy.ops.object.material_slot_assign()

# toggle to object mode
bpy.ops.object.mode_set(mode='OBJECT')

To get the inner vertex, select the outer edges in edge select and then switch to vertex select and invert the selection. It's far from elegant but should work for the example you are using

$\endgroup$
  • $\begingroup$ Selecting the inner vertex is not required for applying the material. As far as I understand the question he also wants to select it, but that sounds like a new question to find an algorithm for this issue. $\endgroup$ – p2or Mar 26 '15 at 10:06
  • $\begingroup$ @poor Yh, but it's small enough so I just added it. $\endgroup$ – iKlsR Mar 26 '15 at 10:07
4
$\begingroup$

You have to use the link_loop_radial on the central vertex to loop through face in counter-clockwise direction( credit for this to this BA post )
no need to select the vertices ,also replaced the face selection part

import bpy, bmesh

obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)

#only the center vertex is part of more than 2 faces
verts = [v for v in bm.verts if (len(v.link_loops)>2)]

for v in verts:
   l = v.link_loops[0]
   for i in range(0,len(v.link_loops)) :   
        next_face = l.face.index
        l = l.link_loop_prev.link_loop_radial_next
        if (i%2) != 0 :
            bm.faces[next_face].material_index = 0
        else:
            bm.faces[next_face].material_index = 1

bmesh.update_edit_mesh(me, True)
$\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.