27

How does the program decide what indices to assign to vertices? If you create a primitive, are the indices generated randomly, or by some base primitive setting? If you extrude new vertices, will they be the next number in sequence?

What happens when you join two objects together that have the same indices used? Which points get reassigned, and by what logic? Can anything be done to control which get re-assigned?

Are there any ways to recalculate, re-assign, or copy vertex indices?

Ascalon
  • 6,579
  • 8
  • 55
  • 121

4 Answers4

29

How does the program decide what indices to assign to vertices?

  • new vertexes are appended to the end of the vertex list
  • duplicating vertexes adds them also to the end and their order is maintained
  • .. list too long..

It would be very time consuming to analyze every mesh operation/modifier and how it handles vertex indices and make a list for you (it will be like the above though in most cases if not in all). Instead you can reverse engineer this yourself:

Type bpy.app.debug = True into the Python Console

This will enable super top secret mode with next option available in Properties panel:

enter image description here

Now you can see the indices and how they change for what you are interested in:

enter image description here

Can anything be done to control which get re-assigned?

Nope, blender handles it internally.

Are there any ways to recalculate, re-assign, or copy vertex indices?

Sure, with python you can do anything to meshes, you can reorder the vertices. Just by building a new mesh with different vertex order from old one and then switching them.

Jaroslav Jerryno Novotny
  • 51,077
  • 7
  • 129
  • 218
24

Jerryno did a great job of explaining mesh indexes in his answer, so I will focus on:

Are there any ways to recalculate, re-assign, or copy vertex indices?

Yes.

First let me clarify that there are more then just vertex indexes, faces and edges have indexes as well, but for the rest of this answer I will refer to just vertex indexes.

The simplest way to reorder the vertex indexes is with the Sort Mesh Elements tool, accessible from the 3D view header Mesh > Sort Elements. All the option in the dropdown menu run the same tool, and can be changed like any other operator by pressing F6 or in the Operator Properties section of the tool shelf.

menu item

If you want to do anything beyond reordering the indexes, that will require python.

David
  • 49,291
  • 38
  • 159
  • 317
  • ...so, you select the mesh that has the "wrong" order, and a similar mesh with the target order, and do Edit (vertices) -> Mesh -> Sort Elements -> Selected? – hatinacat2000 May 01 '20 at 23:36
  • no it does not copy some other mesh's vertices' ids. To quote the manual "Selected Move all selected elements to the beginning (or end, if Reverse enabled), without affecting their relative orders. Warning: This option will also affect unselected elements’ indices!" AKA not what you want it to do. "Selected" simply renumbers the selected vertices so that they are at the beginning (or end with reverse enabled) of the indices. – David May 03 '20 at 02:07
15

You can also sort mesh elements with Python, using the bmesh module:

import bpy
import bmesh
import random

ob = bpy.context.object
assert ob.type == "MESH"
me = ob.data

bm = bmesh.from_edit_mesh(me)
new_order = list(range(len(bm.verts)))
random.shuffle(new_order)

for i, v in zip(new_order, bm.verts):
    v.index = i

print("shuffled indices:")
for v in bm.verts:
    print(v.index)

print("update index()")    
bm.verts.index_update()

print("indices returned to original order:")
for v in bm.verts:
    print(v.index)

print("shuffling again, followed by a sort:")
for i, v in zip(new_order, bm.verts):
    v.index = i
bm.verts.sort()

for v in bm.verts:
    print(v.index)

bmesh.update_edit_mesh(me)

It's important to call sort() after assigning new indices to actually apply the new order. After that, indices will be ordered 0..n, but if you check the indices drawn in viewport, then you'll see that they changed.

CodeManX
  • 29,298
  • 3
  • 89
  • 128
  • Will this re-order to 0..1..2..3..4..5..n incrementally? Some game engine don't accept face indices and just programatically generate it incrementally on run-time. – majidarif May 17 '17 at 17:51
  • Not sure what you mean by incrementally... If you export from Blender after sorting, the indices should be in that order. I don't think that holes (e.g. 0, 2, 3, ...) are allowed in Blender. – CodeManX May 18 '17 at 15:47
  • I asked a question about that here. Can you check? :) https://blender.stackexchange.com/questions/79805/re-arranging-vertices – majidarif May 18 '17 at 16:21
  • @CoDEmanX Its a nice script that shuffles the verices. What do i need to change to sort the list from smallest to biggest? i tried sorted(new_order) but did not worked – DGRL Dec 30 '19 at 11:17
  • @DGRL Smallest to biggest what? – CodeManX Dec 31 '19 at 12:40
  • @CoDEmanX from 0 to last number – DGRL Dec 31 '19 at 12:45
  • @DGRL I don't get the point. The order of bm.verts is 0 to last number by default. If you call bm.verts.sort(), nothing will change. The idea is that you assign a new order by setting the .index attribute accordingly for all the vertices, then call .sort() to apply that desired order. The mesh will then have the vertices reordered, and the indices will be 0 to last number again. – CodeManX Jan 13 '20 at 14:07
  • Great tip with bm.verts.sort(), thanks. – MACHIN3 Mar 08 '23 at 17:45
6

For whoever will find this helpful, here is how to display vertex indices in 'Blender 2.80':

  1. In Preferences - Interface - Display, check 'Developer Extras'.

    enter image description here

  2. In the 3D View - Overlays, there should now be a 'Developer' section, check its "Indices" box - see screenshot below:

    enter image description here

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
Animik
  • 593
  • 7
  • 16