Script to turn selected objects into active rigid bodies (how to )

i am trying to create a script that would turn all selected objecteds into physics objects (for coliding etc).
But it wont work, i am out of clues how it should be made i thought it would be like below… but i get errors.
Any ideas on how this should be done ?


import bpy

selected_objects = bpy.context.selected_objects
obNameList = []


for item in selected_objects:
    obNameList.append(item.name) #append each name into list
    bpy.context.rigid_body.type = 'ACTIVE'
    bpy.context.space_data.context = 'PHYSICS'
    bpy.ops.rigidbody.object_add(item.name)
print(obNameList)

# LIST COMPREHENSION STYLE
print([item.name for item in selected_objects])

Hi

There is an op that will set all the selected objects to rigid bodies at once.


import bpy


context = bpy.context
scene = context.scene


selected_objects = context.selected_objects
obNameList = [ob.name for ob in selected_objects]


# operator sets all selected obs to rb
bpy.ops.rigidbody.objects_add(type='ACTIVE')
    
    
print(obNameList)


# LIST COMPREHENSION STYLE
print([item.name for item in selected_objects])

otherwise to do it in a loop you will need to set the object to active (in context) with


for item in selected_objects:
    scene.objects.active = item
    bpy.ops.rigidbody.object_add(type='ACTIVE')

wow that’s clear logic when you look at it, what reference did you use to write it ?.
I am finding myself a lot of guesing based on a few scripts that i find on the internet.
I would like to have some background on this, some reference

Razor,

the biggest issue I found with blender python scripting was getting my head around the context - operator paradigm. Once it “clicks” it will become quite simple to do just about anything.

The references, techniques I use are:

The API reference (most pages have useful examples)
Template scripts and addons;
Autocomplete in the python console.
Verbose print statements.

and lastly, just like you have done, have a go, and if you get stuck post a question.

And how can I set the rigid_body.kinematic to “Animated” now? Using the tooltip command does not seem to work.

>>> my_plane=bpy.data.objects[myname]
>>> my_plane.rigid_body.kinematic=True
Traceback (most recent call last):
File “<blender_console>”, line 1, in <module>
AttributeError: ‘NoneType’ object has no attribute ‘kinematic’

you need to enable rigid body dynamics for your object first:
bpy.ops.rigidbody.objects_add(type=‘ACTIVE’)