0
$\begingroup$

blender 2.9 I have a custom object properties that need to be copied to ~1000 objects. I have done some research and using the script editor I am able to assign IDs to all of the objects and get a list of the properties I need to assign, however, I am a novice to python and I am stuck there.

Of course if there is an better way (addon?) to copy custom properties to multiple objects

Any help would be appreciated

Here is the script I used to assign the ids:

import bpy

objs = bpy.context.view_layer.objects.selected

numb = 1000

for obj in objs:
    obj["ID"] = str(numb)
    numb = numb + 1
    
for obj in bpy.context.view_layer.objects.selected:
    obj.select_set(state = True)

These are the custom properties I need to add:

Shake_Influence - 5.0
Shake_locX - 1.0
Shake_locY - 1.0
Shake_locZ - 1.0
Shake_rotX - 1.0
Shake_rotY - 1.0
Shake_rotz - 1.0
$\endgroup$
2
  • $\begingroup$ To clarify: you have an object with all the shake custom props listed and want to copy to 1000 odd other objects? $\endgroup$ – batFINGER May 21 at 14:23
  • $\begingroup$ Exactly batFINGER $\endgroup$ – joeejo May 25 at 16:29
0
$\begingroup$

There is already a solution to copy custom-properties to and fro objects?

https://blender.stackexchange.com/a/24004/123064

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: I am not sure if you are trying to create custom-properties additionally from a script or just trying to transfer them.

$\endgroup$
0
$\begingroup$

If not defined via UI.

The answer from https://blender.stackexchange.com/a/24004/123064 used in other answer will work for properties defined by the UI, or with the "_RNA_UI" key set. It is used for adding extra details to the property like min, max description, etc

It will not pick up on properties defined using

ob["foo"] = 3

Using ob.items() gives us a list of key, value pairs. The key being the name of the prop.

Test script,

import bpy

context = bpy.context
    
ob = context.object
# all props with name starting with "Shake_" on context ob
props = [(k, v) for k, v in ob.items() if k.startswith("Shake_")]

# all selected apart from ob
obs = context.selected_objects # selected
# obs = context.scene.objects[:] # all in scene
# obs = context.collection.objects[:] # all in context collection...
if ob in obs:
    obs.remove(ob)
# set from key value pairs
for o in obs:
    for k, v in props:
        o[k] = v
    

Related.

How to edit a custom property in a python script?

How can I save a dict in a scene propertyGroup?

how to change the value of various custom properties at the same time?

Get all custom properties of an object

When should custom-properties be used instead of 'bpy.props'?

Update a custom data field created with an addon

$\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.