Some questions to emptys and collections

Hi all,

i recieve via UDP socket a dictionary stream.
Each dictionary[key] = value is a number(key) and a x,y tuble for a location.
can someone give me some tips to do the following.

.create a empty for each key with key as name
.put this empty in a specific collection by collection name
.delete this empty by key name.

for name, loc in dictionary.items():
    empty = bpy.data.objects.new(name, None)
    empty.location = loc
    bpy.data.collections["Collection Name"].objects.link(empty)

bpy.data.objects.remove(key_name, do_unlink=True)

1 Like

many Thanks,

do you know why the key 1 seams not to work.
It creates all time 1.001 but 1 isn already created.

import bpy
from collections import OrderedDict 


D = OrderedDict([(1, [1, 8]),(2,[2,10])])
for name,loc in D.items():
    empty = bpy.data.objects.new(str(name),None)
    loc.append(0)
    empty.location = loc
    bpy.data.collections['Collection 1'].objects.link(empty)

Capture

Probably you tried to link an empty to nonexistent collection, so it was created, but not added to the scene. To clean it open orphan data in outliner and press purge
or run

for ob in bpy.data.objects:
    if not ob.users:
        bpy.data.objects.remove(ob, do_unlink=False)

And I made a mistake. To remove an empty by a key name use

name = "1"
ob = bpy.data.objects.get(name)
if ob is not None:
    bpy.data.objects.remove(ob, do_unlink=True)
1 Like