This replaces the somewhat hackish and usafe code used previously. Also fixes a potential bug, where the newly created `local_id` was dereferenced before checking for it to be non-null. Pull Request: https://projects.blender.org/blender/blender/pulls/108328