0
$\begingroup$

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 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 and I don't understand what I'm doing wrong. 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.

Thanks !

$\endgroup$
2
  • 2
    $\begingroup$ In your getter for Object.global_index you have if any(obj.global_index == self.global_index for obj in anchors): Note that obj.global_index uses the getter, hence the infinite recursion. $\endgroup$
    – batFINGER
    Commented May 15, 2017 at 16:19
  • $\begingroup$ I see, thanks. So do you have any suggestion on how to manage this problem ? The only idea I have right now is to reference any index in a separate list, or to increment it on each get (but will increase very quickly ?) $\endgroup$
    – Ange_blond
    Commented May 16, 2017 at 7:58

1 Answer 1

0
$\begingroup$

So, based on this Duplicating object and default value of property I think I found a way.

I use a second identical property to avoid having to get from the getter

Here it is :

# 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'})

Since it the getter, I just need to return the new value. Using an other property without getter but with the same value, I can get the copied value from the getter.

$\endgroup$

You must log in to answer this question.

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