What is the best way to read the mesh data currently being edited?

Hi!

Longtime lurker, first time poster. :slight_smile:

I’m working on a plugin for a client that sends mesh data over the network to another application. The idea is to be able to visualize the currently edited geometry in another system.

I’ve got a prototype mostly working as desired, but am seeing some issues with performance that I want to resolve.

I suspect that a part of the issue might be that the solution I’ve arrived at for evaluating the currently active object’s mesh is inefficient.

def GetBmesh(context):
    if context.object.mode == 'EDIT':
        return bmesh.from_edit_mesh(context.object.data)
    else:
        _bmesh = bmesh.new()
        _bmesh.from_mesh(context.object.data)
        return _bmesh

I decided to use the bmesh type as a container though which I access the vertex and face data of the object. is there an easier way of quickly and reliably getting the current mesh data?

thanks!

In edit mode? No. you could push your edit mode changed to the object data but that isn’t going to save you any time. If you’re in object mode you can access the mesh data directly and bypass bmesh entirely.

Hi, thanks for the reply.

I’m hoping to eventually set it up to only send changes. Based on what your saying it sounds like I’ll want to maintain an independent copy of the data that I’m serializing to send over the network, and then compare that to data I get from C.object.data (whilst not in edit mode) and data from bmesh.from_edit_mesh() when I am.

Is there a bpy-accessible buffer someplace that stores recently edited mesh data? Or do I need to figure out what’s changed between send/update cycles myself by comparing sampled meshes?

Thanks again

in edit mode, that’s exactly what bmesh is. when you call from_edit_mesh() you’re pulling a reference to the bmesh object that already exists (the one that every other operator uses), you’re not actually creating one at all unless you use from_mesh(). The performance impact of calling from_edit_mesh() should be negligible. Have you profiled your code to verify this is what’s causing your issues?

1 Like

I have not. I’ll do some research on how to do that. Does blender provide any profiling tools that could help me here, or do I just use profile/cProfile?

Though at present I’m not just trying to resolve an issue, but also sort out the data design to provide features like sending only updates, instead of entire meshes.

EDIT: I used cProfile to actually test the operator I’m working on and, yeah my issue was totally not related to efficient access from blender. Instead it was, ironically, due to a badly executed optimization. Solving it resulted in a roughly 10x improvement in performance.

Blender doesn’t provide anything for profiling python, for most things I just time things manually using deltas, sometimes timeit(), for things that need robust profiling I use statistical profiling (eg: pyinstrument)

Neat. I sort of figured. FWIW I got a lot of milage out of cProfile as well.

Thanks for all the tips.