3
$\begingroup$

Let's say I have a vertex group layer, that I'd like to identify through the object's lifespan.

In order to identify such bpy object:

  • I cannot rely on the name because the users might change it
  • I cannot rely on the data contained by the vertex group either, for the same reason.
  • I cannot add custom properties because by.types.VertexGroup does not support custom properties
  • I cannot use id(my_vglayer) neither hash(my_vglayer) because how blender handles undo any memory address is therefore un-reliable.
  • I cannot store my_vglayer as a global, this might cause crashes, due to changing memory address as explained above.

What am I supposed to do to recognize such a bpy object then? It is unfortunate because id() or hash() was supposed to be stable identifiers working on any kind of python objects...

$\endgroup$
4
  • 1
    $\begingroup$ Does stackoverflow.com/a/63794345/3466081 work? Although your hopes may dim with this old (2015) posting: blenderartists.org/t/… $\endgroup$
    – james_t
    Aug 20, 2022 at 16:40
  • $\begingroup$ unfortunately, it couldn't work, we'd need to be able to identify the mesh object through it's lifespam, that means also if it's geometry or name change. in the case of mesh object it's quite easy tho we can just add a custom identifier property $\endgroup$
    – Fox
    Aug 20, 2022 at 17:50
  • 1
    $\begingroup$ Here's another from devtalk "devtalk.blender.org/t/universal-unique-id-per-object/363/4". Rather ridiculous that there's no UUID technique! $\endgroup$
    – james_t
    Aug 20, 2022 at 19:00
  • 1
    $\begingroup$ "just use the name" they said haha $\endgroup$
    – Fox
    Aug 20, 2022 at 20:56

2 Answers 2

1
+300
$\begingroup$

Check out this repo https://github.com/strike-digital/non_id_props by Strike_Digital It allows to store reference to non-ID properties similar to blender's PointerProperty. Under the hood it uses blender's msgbus, handlers and python's getter-setter on name of the property to track the it. I'm guessing They are also using this in their add-ons also.

This is the demo given in the repository

import bpy
from .non_id_props import NonIDProperty


def my_vertex_group_update(self, context):
    print(f"My vertex group is called '{self.my_vertex_group.name if self.my_vertex_group else self.my_vertex_group}'")


class MyPropertyGroup(bpy.types.PropertyGroup):

    # Make sure to assign with an '=' rather than a ':' like other Blender properties
    my_vertex_group = NonIDProperty(
        name="my_vertex_group",  # THIS MUST BE THE SAME AS THE NAME OF THE VARIABLE
        # (Someone please tell me if theres a better way :)
        subtype="vertex_groups",  # This is the path to the non id property, from its parent ID property
        # e.g. object.vertex_groups
        set=my_vertex_group_update,  # The get, set, and update functions work in the same way as default Blender
    )

    # Note that the subtype can only be of a subtype of the ID property this group is registered to
    # (In this case, bpy.types.Object)
    my_modifier = NonIDProperty(
        name="my_modifier",
        subtype="modifiers",
    )

    my_constraint = NonIDProperty(
        name="my_constraint",
        subtype="constraints",
    )


classes = [MyPropertyGroup]


def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.non_id_prop_demo = bpy.props.PointerProperty(type=MyPropertyGroup)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

    del bpy.types.Scene.non_id_prop_demo

I would like to invite you to BPY discord server: https://discord.gg/dRvw2yNHGv where this was shared by them. It is a community focused on blender add-on development and Blender's python api

$\endgroup$
1
  • $\begingroup$ I was hoping for a more low-level solution, but well it seems that there is no other solutions $\endgroup$
    – Fox
    Aug 24, 2022 at 21:45
0
$\begingroup$

I presented a homemade implementation of integer uuid for objects types in this topic Unique and invariable ID (within the session) for an object?

$\endgroup$

You must log in to answer this question.

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