3
$\begingroup$

How to copy all object properties to another object with Python?

Could you suggest the simplest way.

Desired process

$\endgroup$
5
$\begingroup$

Custom Properties on object level are contained in Object.keys(), but there's also a helper property _RNA_UI and some API-defined properties such as cycles_visibility.

Therefore, it's better to test for _RNA_UI and get all actual Custom Properties there:

import bpy

ob_sel = bpy.context.selected_editable_objects
ob_act = bpy.context.object

try:
    props = ob_act["_RNA_UI"]
except KeyError:
    pass
else:
    for ob in ob_sel:
        if ob == ob_act:
            continue
        for p in props.keys():
            print(p, ob_act[p])
            ob[p] = ob_act[p]

Note that properties will added or overwritten, but not removed if the target has some the source does not have. You could change that by deleting all Custom Properties of the target before copying the properties from the source object.

$\endgroup$
6
  • $\begingroup$ I got exception with "props = ob_act["_RNA_UI"]" I got it working only like this pastebin.com/Di0r5JrY $\endgroup$ – mifth Feb 1 '15 at 17:09
  • $\begingroup$ My bad, try/except needs to catch KeyError, not AttributeError to make it work as intended. If you iterate over Object.keys(), keep in mind it may copy more properties than you may want. $\endgroup$ – CodeManX Feb 1 '15 at 22:28
  • $\begingroup$ the problem is that i always get exception or None with "props = ob_act["_RNA_UI"]". Right now i found the only way to copy all stuff to iterate over Object.keys(). $\endgroup$ – mifth Feb 2 '15 at 11:45
  • $\begingroup$ Please upload a sample .blend, it appears to work just fine on my side. $\endgroup$ – CodeManX Feb 2 '15 at 11:56
  • $\begingroup$ Here is the test dl.dropboxusercontent.com/u/26887202/blender/test_RNAUI.blend Create an object and run the script - you will get exception. $\endgroup$ – mifth Feb 3 '15 at 12:05
1
$\begingroup$

I'll add to the other answer to expand a little bit.

For some reason the other method won't copy the subtype of the origin property. This can be interesting if you are working with a Color or Euler subtypes. Example :

enter image description here

Also there is now a is_library_overridable flag that is not copied over with the script.

enter image description here

We just need to add a few lines to the origin script :

import bpy

ob_sel = bpy.context.selected_editable_objects
ob_act = bpy.context.object


for ob in ob_sel:
    if ob == ob_act:
        continue
    for p in ob_act.keys():
        if p == '_RNA_UI':
            # Make sure the dictionary is initialized (Mainly for empty objects) :
            if p not in ob.keys():
                ob[p] = {}
            for sub_p in ob_act[p].keys():
                # This will copy over the subtype field :
                ob[p][sub_p] = ob_act[p][sub_p].to_dict()
        else:
            # This is a "standard" property (not under '_RNA_UI') - for some reason:
            ob[p] = ob_act[p]
    for p in ob_act.keys():
        if p == '_RNA_UI':
            continue
        # Copy over the overridable flag :
        ob.property_overridable_library_set(f'["{p}"]', ob_act.is_property_overridable_library(f'["{p}"]'))

This has been made into a handy add-on by user Scorpion81 (and the help of batFINGER) on this thread (Needs a bit of tweaking though to actually copy over the subtype and overridable flag) : https://blenderartists.org/t/addon-copy-custom-properties/591562/13

$\endgroup$

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.