4

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.

gkhwvjxtc
  • 103
  • 7
  • 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 ? – Gorgious Jun 02 '22 at 06:26
  • 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. – gkhwvjxtc Jun 02 '22 at 06:30

3 Answers3

6
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]

Gorgious
  • 30,723
  • 2
  • 44
  • 101
  • 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. – gkhwvjxtc Jun 02 '22 at 07:27
  • @gkhwvjxtc Sure ! You need to follow these steps https://stackoverflow.com/a/11604587 – Gorgious Jun 02 '22 at 08:00
  • understood, thank you. Does this mean you need to manually insert the script every time before using it? – gkhwvjxtc Jun 02 '22 at 08:08
  • Yes, in this form. You can also turn it into a regular addon and have it always enabled by following this howto https://docs.blender.org/manual/en/latest/advanced/scripting/addon_tutorial.html – Gorgious Jun 02 '22 at 08:48
  • Thank you very much! – gkhwvjxtc Jun 02 '22 at 08:50
3

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()

Duarte Farrajota Ramos
  • 59,425
  • 39
  • 130
  • 187
0

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

robbie
  • 1