4
$\begingroup$

Periodically, addons leave different types of custom properties in objects. How do I delete ALL custom properties in the entire scene?

I don't know Python, I can only copy and paste values into Pie Menu Editor)

I found something ( bpy.ops.wm.properties_remove() ) to use in the button from Pie Menu Editor, but... I do not know how to set some properties with a brute force of names.

$\endgroup$
2
  • $\begingroup$ If the addon is still enabled, you won't be able to. Do you mean you want to clean objects after you removed an addon ? $\endgroup$
    – Gorgious
    Commented Jun 2, 2022 at 6:26
  • $\begingroup$ I don't care if any addon uses custom properties or not. I need to do the cleaning of objects from junk. Clean Up from the File menu doesn't clean everything I need. I have to do it manually. $\endgroup$
    – gkhwvjxtc
    Commented Jun 2, 2022 at 6:30

3 Answers 3

6
$\begingroup$
import bpy


class GU_OT_property_remove_all_in_file(bpy.types.Operator):
    bl_idname = "property.remove_all_in_file"
    bl_label = "Remove ALL Custom Properties from the file"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        for attr in dir(bpy.data):
            if "bpy_prop_collection" in str(type(getattr(bpy.data, attr))):
                for obj in getattr(bpy.data, attr):
                    for custom_prop_name in list(obj.keys()):
                        del obj[custom_prop_name]
        return {"FINISHED"}


def draw_op(self, context):
    self.layout.operator("property.remove_all_in_file")


if __name__ == "__main__":
    bpy.utils.register_class(GU_OT_property_remove_all_in_file)
    bpy.types.VIEW3D_MT_object.append(draw_op)

Then search for "Remove all custom properties from the file" using the search tool (F3 by default) or go to Object > Remove ALL Custom Properties from the file.

You can also directly run this in the script editor

import bpy

for attr in dir(bpy.data):
    if "bpy_prop_collection" in str(type(getattr(bpy.data, attr))):
        for obj in getattr(bpy.data, attr):
            for custom_prop_name in list(obj.keys()):
                del obj[custom_prop_name]

If you want to remove properties with a specific name from all items in the file, use :

import bpy

for attr in dir(bpy.data):
    if "bpy_prop_collection" in str(type(getattr(bpy.data, attr))):
        for obj in getattr(bpy.data, attr):
            for custom_prop_name in list(obj.keys()):
                # Use either of these depending on your use case :
                if custom_prop_name == "my_prop_name":
                # if custom_prop_name.startswith("my_prop"):
                # if custom_prop_name.endswith("prop_name"):
                # if "prop" in custom_prop_name:
                    del obj[custom_prop_name]
$\endgroup$
5
  • $\begingroup$ Oh yes, thank you! I didn't know where to insert this code, so I inserted it into the buttons of another plugin . Perhaps, if it's not difficult for you, you will explain how to use this file. It is not installed as an addon. $\endgroup$
    – gkhwvjxtc
    Commented Jun 2, 2022 at 7:27
  • $\begingroup$ @gkhwvjxtc Sure ! You need to follow these steps stackoverflow.com/a/11604587 $\endgroup$
    – Gorgious
    Commented Jun 2, 2022 at 8:00
  • $\begingroup$ understood, thank you. Does this mean you need to manually insert the script every time before using it? $\endgroup$
    – gkhwvjxtc
    Commented Jun 2, 2022 at 8:08
  • $\begingroup$ Yes, in this form. You can also turn it into a regular addon and have it always enabled by following this howto docs.blender.org/manual/en/latest/advanced/scripting/… $\endgroup$
    – Gorgious
    Commented Jun 2, 2022 at 8:48
  • $\begingroup$ Thank you very much! $\endgroup$
    – gkhwvjxtc
    Commented Jun 2, 2022 at 8:50
3
$\begingroup$

You can use this script to clear all custom data properties on all objects returned from bpy.data.objects

import bpy
import pprint

def clear_all_custom_data_properties():
        """Clear custom data properties"""
        for data in bpy.data.objects:
            data.id_properties_clear()
        pprint.pprint("[OPERATION][STATUS] DONE")
            
            
# call this with caution            
# clear_all_custom_data_properties()
$\endgroup$
0
$\begingroup$

In case you don't see property.remove_all_in_file enable Developer Extras under Preferences -> Interface credit goes to https://stackoverflow.com/a/64491055/9615233

$\endgroup$

You must log in to answer this question.

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