0
$\begingroup$

I am using Blender 2.82a, and am trying to visualize vertex indices of cloth (but I don't think my issue here is specific to cloth). There is an easy way to do this as shown here and here. I make a cloth via subdividing a plane 20 times, to get 484 vertices. These, I can visualize easily, but I'm running into a case where my cloth ends up with more vertices due to certain modifiers, but I can't visualize these extra vertices.

I am using cloth that gets continually updated (by armatures or hooks moving the cloth). The updates involve using the camera image and a pixel on that image. Then, I get the corresponding vertices of the cloth that are closest to that target camera image pixel, and those are what I move in simulation. Here's an example of a similar question.

To do this, it appears that I need to update the scene to continually get the updated cloth position. As described here, I am using Blender 2.8's way of doing this, in the following code. However, when doing this, it seems like the number of cloth vertices dramatically increases due to cloth modifiers. For example, the "solidify" modifier will double the number of vertices, and adding the "subsurf" modifier will further increase the count, depending on the granularity. Unfortunately, it seems like these extra vertices do not appear in the GUI.

Here is a minimal working example you can run, where I try to visualize the vertex indices of cloth_up (not cloth) in the check_vertices method:

import bpy
import numpy as np
DEG_TO_RAD = np.pi / 180.

def _to_rad(data):
    return (DEG_TO_RAD * data[0], DEG_TO_RAD * data[1], DEG_TO_RAD * data[2])

def clear_scene():
    """Clear existing objects in scene."""
    for block in bpy.data.meshes:
        if block.users == 0:
            bpy.data.meshes.remove(block)
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()

def add_camera(location, rotation):
    """Can adjust camera positions and orientations. Rotations in degrees."""
    bpy.ops.object.camera_add(location=location, rotation=_to_rad(rotation))
    bpy.context.scene.camera = bpy.context.object

def make_plane():
    """Underlying plane. Use collision to support cloth."""
    bpy.ops.mesh.primitive_plane_add(location=(0, 0, 0))
    bpy.ops.transform.resize(value=(6.0, 6.0, 6.0))
    bpy.ops.object.modifier_add(type='COLLISION')

def make_cloth():
    """Make cloth over the plane."""
    bpy.ops.mesh.primitive_plane_add(location=(0, 0, 0.2))
    bpy.ops.object.editmode_toggle()
    bpy.ops.mesh.subdivide(number_cuts=20)
    bpy.ops.object.editmode_toggle()
    bpy.ops.object.modifier_add(type='CLOTH')
    bpy.context.object.modifiers["Cloth"].settings.quality = 10
    bpy.context.object.modifiers["Cloth"].collision_settings.collision_quality = 5

    # Will double the number of vertices.
    bpy.ops.object.modifier_add(type='SOLIDIFY')
    bpy.context.object.modifiers["Solidify"].thickness = 0.03

    # Will increase vertices by a factor of about 3.8x, and then adding the
    bpy.ops.object.modifier_add(type='SUBSURF')
    bpy.context.object.modifiers["Subdivision"].levels = 3
    bpy.context.object.modifiers["Subdivision"].render_levels = 3

    return bpy.context.object

def check_vertices(cloth):
    """Check cloth vertices."""
    scene = bpy.context.scene
    assert scene.render.resolution_percentage == 100, scene.render.resolution_percentage
    render_size = (int(scene.render.resolution_x), int(scene.render.resolution_y))

    # I believe we need these two lines to get cloth that's been updated in a scene.
    depsgraph = bpy.context.evaluated_depsgraph_get()
    cloth_up = cloth.evaluated_get(depsgraph)
    v_cloth    = [cloth.matrix_world @ v.co    for v in list(cloth.data.vertices)]
    v_cloth_up = [cloth_up.matrix_world @ v.co for v in list(cloth_up.data.vertices)]
    print(len(v_cloth), len(v_cloth_up))   # returns 484 61826

    # TODO: How to visualize the vertices of v_cloth_up in the GUI?

if __name__ == '__main__':
    clear_scene()
    make_plane()
    cloth = make_cloth()
    add_camera(location=(0, 0, 5), rotation=(0, 0, 0))
    check_vertices(cloth)

If the file is called test-vertices.py then you can run blender -P test-vertices.py, and then do the following steps: (1) click the cloth, (2) go to edit mode, (3) go to viewport overlays and click "indices", then in the screenshot below,

enter image description here

However, if you notice, it will still only display the 484 vertices from the initial cloth. Is there a way to visualize the other vertex indices that are created as a result of the cloth modifiers? Obviously it would be a bit unreadable with over 60K vertices from these modifiers, but I'd run some experiments with smaller vertex counts.

$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.