Unique index property on some objects


(Ange_blond) #1

Here is my issue : I would like to add a unique Int property on an object.

This property needs to be unique even when duplicated. So according to https://blender.stackexchange.com/questions/27361/duplicating-object-and-default-value-of-property and https://blenderartists.org/forum/showthread.php?328009-Unique-object-id I did this :


# Getter for anchor duplicate
def get_global_index(self):
#    print("getting value")
    done = False
    # get current index
    index = self.global_index #Recursive ?!
    # chek if already used (in case of a duplicate) and try a new one while necessary
    while not done:
        anchors = [obj for obj in bpy.context.scene.objects if obj.type == 'EMPTY' and obj.empty_draw_type == 'PLAIN_AXES']
        # already referenced
        if any(obj.global_index == self.global_index for obj in anchors):
            # found to try another index
            print("Trying another index")
            index = context.scene.global_index
            context.scene.global_index+=1
        else:
            done = True
            print("FOUND index")


    return index


def register():
    bpy.types.Object.list_index = bpy.props.IntProperty(name = "Index for my_list", default = 0)
    #Each anchor needs a unique index
    bpy.types.Scene.global_index = bpy.props.IntProperty(name = "Global index for Anchors", default = 0)
    #So use scene index to store the increment
    bpy.types.Object.global_index = bpy.props.IntProperty(name = "Object index for Anchors", default = 0, get=get_global_index)


BUT, The getter seems to loop infinitely as soon as I use the property. I just want the getter to check in the current objects of the specified king to check if the index is not already used (when Duplicated) because I need to have each one of them having a unique index.

Do you have any suggestion how to get this unique property ?

Thanks !


(Ange_blond) #2

Think I fixed it !


# Getter for anchor duplicate
def get_global_index(self):




    # get current index
    index = self.hidden_index


    # chek if already used (in case of a duplicate) and try a new one while necessary
    # Do not forget to remove self from result !    
    anchors = [obj for obj in bpy.context.scene.objects if obj.type == 'EMPTY' and obj.empty_draw_type == 'PLAIN_AXES' and obj != self]


    while any(obj.hidden_index == index for obj in anchors):
        # found to try another index
        index +=1




    self.hidden_index = index;
    return self.hidden_index


def register():
    bpy.types.Object.global_index = bpy.props.IntProperty(name = "Object index for Anchors", default = 0, get=get_global_index)
    bpy.types.Object.hidden_index = bpy.props.IntProperty(default = 0, options={'HIDDEN'})