2
$\begingroup$

I am trying to replicate the functionality of Object Parent using the dropper select tool in Python (an image is attached for what exactly am I trying to replicate).:

The Object Parent Select Tool

For this purpose, I am using the PointerProperty to get a pointer to the object. Most of the functionalities work as expected, similar to the implementation above. However, if I delete the object (either using the keyboard shortcuts or the python method), it disappears from the 3D view but it is still present in bpy.data.objects. Moreover, the PointerProperty does not clear the object field as is the case with the original object parent field. I have tested this in Blender 2.79 and 2.81 with similar results. Any suggestions would be appreciated. This is the code I am using.

import bpy
bl_info = {
    "name": "Pointer Property Test",
    "author": "Mr. Potato",
    "version": (0, 1),
    "blender": (2, 79, 0)
}


class PT_test_constraint(bpy.types.Panel):
    """Add Rigid Body Properties"""
    bl_label = "TEST PROPERTIES"
    bl_idname = "addon.test_constraint"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context= "physics"

    bpy.types.Object.test_constraint_parent = bpy.props.PointerProperty(name="Parent", type=bpy.types.Object)

    def draw(self, context):

        layout = self.layout
        col = layout.column()
        col.prop_search(context.object, "test_constraint_parent", context.scene, "objects")                


def register():
    from bpy.utils import register_class
    register_class(PT_test_constraint)


def unregister():
    from bpy.utils import unregister_class
    unregister_class(PT_test_constraint)


if __name__ == "__main__":
    register()
    #unregister()
$\endgroup$
3
  • 1
    $\begingroup$ Because the object has a user (the object it's a property of) it is only unlinked from the scene. If you bpy.data.objects.remove(ob) the object it will also be gone from the pointer prop. Might find this useful blender.stackexchange.com/a/101301/15543. Also recommend declaring properties in the register method, rather than in panel class. $\endgroup$
    – batFINGER
    Jan 13, 2020 at 18:24
  • $\begingroup$ Thanks for the answer. Based on your suggestion and the link that you posted, what I think is appropriate is to check if the parent_object (selected by the PointerProperty) has less than 2 users (as if it has less than 2 users, it would mean that someone deleted it from the scene graph using the keyboard delete key). If so, I should call bpy.data.objects.remove(parent_object) in the Labels poll() method so that it is safely removed from the PointerProperty and bpy.data.objects. Does this seem reasonable? $\endgroup$
    – General
    Jan 13, 2020 at 20:05
  • $\begingroup$ Not sure about that, worth a go I suppose. Speculating a bit here, parent is a special case that is quite likely dealt with by the depsgraph when the object is unlinked from scene or deleted. A getter / setter approach on the property could be the go, setting to None if the pointer is not in the scene. $\endgroup$
    – batFINGER
    Jan 15, 2020 at 1:46

1 Answer 1

2
$\begingroup$

Based on the suggestion by @batFinger, I realized that if I delete the object using the keyboard shortcut or using the Python console, it is unlinked from the scene and its reference count decreases by 1. Since the pointer property still has a reference to the object, it is not removed from bpy.data.objects. Therefore, I check in my Panel draw() function (which updates automatically) if the desired object is present in the scene as follows:

if context.scene.objects.get(desired_object.name) == None:
# That means that the object has been deleted from the
# scene graph, therefore remove it explicitly
    bpy.data.objects.remove(desired_obj)

This lets me reproduce the functionality inheret to the Blender when dealing with PointerProperty Objects as mentioned in my question.

$\endgroup$
3
  • 1
    $\begingroup$ The draw method is not really the place for this... suprized it works. Usually trying to alter data in draw produces an error. $\endgroup$
    – batFINGER
    Jan 24, 2020 at 17:31
  • 2
    $\begingroup$ I agree, but so far it seems to work in Blender 2.79 and 2.81. Does deleting an object generates an event? and if so, can a method be bound to that event. $\endgroup$
    – General
    Jan 24, 2020 at 19:12
  • $\begingroup$ You may want to use an depsgraph_update_post app handler. $\endgroup$
    – Ray
    Nov 21 at 23:53

You must log in to answer this question.

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