3
$\begingroup$

I'm trying to add objects to previously created groups from a Python script.

What do I do?

$\endgroup$
3
$\begingroup$

It is actually pretty simple:

import bpy

grp = bpy.data.groups.get('bacon')

obj = bpy.data.objects['Cube']
grp.objects.link(obj)

This avoid the hassle of setting up the context for bpy.ops operators.

$\endgroup$
0
$\begingroup$

You can do as follows:

  1. Make the desired object active
  2. Assign it to an existing group

    bpy.context.scene.objects.active = bpy.data.objects['Your Object']
    bpy.ops.object.group_link(group='Your Group')
    
$\endgroup$
  • 1
    $\begingroup$ It's better to not use operators in Python scripts, if possible. See Mutant Bob's answer to see how it can also be done. $\endgroup$ – dr. Sybren Nov 25 '17 at 17:11

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.