0
$\begingroup$

my goal is to be able to detect when a child object is renamed in a way to display actual name and associated length value in every moment. I first have a list of all name of object children. If an object is renamed, I want to replace the ancient name in my list by the new one. However, it is not working when I rename several children.. Maybe there are others way to do it, or something I don't see in my code ? Thank you.

           for child in obj.children :
                if child.name not in bpy.types.Object.allnameList :
                    # A rename happened : Find the name to replace :
                    for w in range(len(bpy.types.Object.allnameList)):
                        if bpy.types.Object.allnameList[w] != child.name  and (bpy.types.Object.allnameList[w] not in obj.children) :
                            bpy.types.Object.allnameList[w] = child.name
$\endgroup$
3
$\begingroup$

Pointer property

Use a simple collection property with a name and a pointer to an object

import bpy

from bpy.props import CollectionProperty, PointerProperty

class ObjCollection(bpy.types.PropertyGroup):
    object = PointerProperty(type=bpy.types.Object)

def register():    
    bpy.utils.register_class(ObjCollection)
    bpy.types.Object.foo = CollectionProperty(type=ObjCollection)

if __name__ == "__main__":
    register()

I've added the foo collection to every object... now off to the python console to test, clear the objects list

>>> C.object.foo.clear()

Load up every object in scene to context objects "foo" collection

>>> for o in C.scene.objects:
...     item = C.object.foo.add()
...     item.name = o.name
...     item.object = o
...     

Rename every object in scene to "Bar"

>>> for o in C.scene.objects:
...     o.name = "Bar"
...     

Iterate over context object foo

>>> for o in C.object.foo:
...     o.name, o.object.name
...     
('Cube', 'Bar.001')
('Lamp', 'Bar')
('Camera', 'Bar.002')

The object formerly known as "Cube" is now known as "Bar.001" etc.

If the object has been since removed then

C.object.foo["Lamp"].object is None

We know that the lamp, "Bar" formerly named "Lamp" has been removed.

Please note: have defined the properties on classes eg bpy.types.Object and then deal with instances eg context.object. In your question code it appears you have monkey patched a list onto the class bpy.types.Object (the one and only...) instead of, as shown in above, the instances of that type (many)

$\endgroup$
  • $\begingroup$ Thank you a lot for your answer, it works well in the python console. However, I have no Idea why, but when I want to bring it in my addon, the 'bpy.utils.register_class(ObjCollection)' line creates errors 'type object has no attribute X' which is habitually not there, X is an attribute I also add to bpy.types.Object in my register lines . $\endgroup$ – art2611 Jul 31 '19 at 8:35
  • $\begingroup$ Added minimal register code. The answer gives an example of how to detect name changes using a pointer property. Any attempt to answer why it doesn't register in your addon is a pure guessing game on my part. I will reiterate that monkey-patching a list onto a class, (eg bpy.types.Foo.xxx = []). as displayed in question code, is not (IMO) the way to go. $\endgroup$ – batFINGER Aug 1 '19 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.