0
$\begingroup$

say I have some deterministic expensive function calc_foo, that operates on a bpy.objects.Mesh.

How can I detect if a given bpy.objects.Mesh object is identical-to (unmodified-from) a previously processed mesh object.

$\endgroup$
2
  • $\begingroup$ I've never used it, but you could try unit_test_compare See 2nd answer here blender.stackexchange.com/questions/28793/… $\endgroup$
    – Psyonic
    Feb 12, 2023 at 3:56
  • $\begingroup$ @Psyonic thanks for a recommendation, unit_test_compare appears to be quite sophisticated, i think it crawls a huge variety of mesh properties (see github.com/blender/blender/blob/…) however, it primarily asserts equality, and I need to determine exactly what is different so these differences can be used as primary keys. Its very interesting to look at blender function, i thought it was much shallower than it is before I found it. $\endgroup$ Mar 3, 2023 at 6:53

1 Answer 1

0
$\begingroup$

I'm not sure if this is reliable, but for now i'm leveraging python's hash() of strings and tuples to impose an order-based hash of a mesh

def MESH_PROP_cachekey_get(self):
    return str(hash((
        bl_info['name'],
        bl_info['version'],
        tuple(tuple(i.co) for i in self.vertices),
        tuple((i.vertices[0], i.vertices[1]) for i in self.edges),
        tuple((i.vertex_index, i.edge_index) for i in self.loops),
        tuple(tuple(i.loop_indices) for i in self.polygons),
    )))

def register():
    bpy.types.Mesh.cachekey = bpy.props.StringProperty(get=MESH_PROP_cachekey_get)

must be str, because the int is too large for a blender Int

this seems to be a reasonable way to hash a mesh to me, I can then journal this in bpy

if obj.data.cachekey != obj.get('last_cachekey', None):
    print('mesh is dirty: %s' % obj)
obj['last_cachekey'] = obj.data.cachekey

or

db = dict()
if obj.get('last_cachekey', None) is not None and obj.data.cachekey not in db:
    print('mesh not indexed: %s' % obj)

also, r.i.p to performance doing integer comparisons as strings

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .