1
$\begingroup$

I have a group. The first object is a mesh and the second is an armature with multiple actions that controls the mesh. How can I duplicate this group with python in a for x in range(number) statement and be able to set the action on each armature that is created. This way, each duplicate group will have it's own animation.

$\endgroup$

1 Answer 1

2
$\begingroup$

Make a copy of all objects in group "Group" and assign to a number of new groups, "NewGroup[.nnn]" If the object in the new group has a parent or an armature modifier object in "Group" then it is assigned to the respective copy in the new group.

import bpy
scene = bpy.context.scene
number = 5 # make 5 group copies
group = bpy.data.groups.get("Group")
obs = [o for o in group.objects]
if group is not None:
    for g in range(number):
        group_objects = [o.copy() for o in bpy.data.groups['Group'].objects]
        new_group = bpy.data.groups.new("NewGroup")
        for o in group_objects:
            # parent
            if o.parent in obs:
                o.parent = group_objects[obs.index(o.parent)]
            # armature modifiers
            armature_mods = [m for m in o.modifiers if m.type == 'ARMATURE']
            for m in armature_mods:
                if m.object in obs:
                    m.object = group_objects[obs.index(m.object)]
            new_group.objects.link(o)
            scene.objects.link(o)

To check if an object is armature and change action, something like

    for o in group_objects:
        if o.type == 'ARMATURE':
            o.animation_data.action = someaction
        new_group.objects.link(o)
$\endgroup$
4
  • $\begingroup$ How can I set the location after duplication? $\endgroup$
    – John Roper
    Jul 19, 2016 at 19:54
  • $\begingroup$ This duplicated both objects but did not apply the armature to the mesh. $\endgroup$
    – John Roper
    Jul 19, 2016 at 21:11
  • $\begingroup$ Look again! It dupes all objects in the group, however it will get the relationships wrong tho, by keeping the original parent etc. Need to amend to suit your needs re relations, modifiers, constraints etc. As it stood before the "Group" Armature parent object would have all the other groups' mesh objects as children. Fixed for single parent. .. armature would need to be amended as the armature modifier object too. $\endgroup$
    – batFINGER
    Jul 19, 2016 at 21:41
  • $\begingroup$ My question was specifically how to duplicate with animation. I wouldn't be asking if I knew how to do it. Duplicating the objects themselves is not a problem. I need to know how to duplicate them so that each duplicate is animated by it's own armature. $\endgroup$
    – John Roper
    Jul 19, 2016 at 21:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .