4
$\begingroup$

Say you have a cube and a sphere grouped as 'Group'. You instanciate that group and link it to the scene. Now when you click on the instance, there is no material panel.

See the two pictures below for a better understanding of what I'm trying to do. current state desired state

Is it possible to access the duplicated objects in the dupli instance in order to give them new materials ?

The only thing I succeeded in doing is to assign a material to an object instance but not to objects within a group instance.

Python answers are welcome.

As requested, here's the code to create a dupli_group instance:

layers = [True] + 19*[False]

# Could replace this with bmesh method
object1 = bpy.ops.surface.primitive_nurbs_surface_sphere_add(radius=1, location=(0, 0, 0), layers=layers)
object1 = bpy.context.active_object

object2 = bpy.ops.mesh.primitive_cube_add(radius=1, location=(4, 0, 0), layers=layers)
object2 = bpy.context.active_object

group = bpy.data.groups.new('some_group_name')
group.objects.link(object1)
group.objects.link(object2)

group_instance = bpy.data.objects.new('some_group_name_instance1', None)
group_instance.dupli_type = 'GROUP'
group_instance.dupli_group = group

bpy.context.scene.objects.link(group_instance)

Then I would expect to have a way to target object1 and object2 within group_instance and to set new materials that won't affect other instances.

$\endgroup$
  • $\begingroup$ Can you outline the exact steps taken to "instantiate that group" ? $\endgroup$ – Mutant Bob Jun 3 '15 at 19:25
  • $\begingroup$ @MrZak I'm sorry, this doesn't answer my question. $\endgroup$ – nantille Jun 4 '15 at 14:45
1
$\begingroup$

Unfortunately, I think it is not possible to do exactly what you want to do.

One of the advantages of using an Empty with dupli_group is that you don't have the overhead of storing much in the way of differences (location, rotation, scale).

If you want to alter the group, you will probably have to instantate objects by copying the data from the group. There's actually a keyboard shortcut to Make Duplicates Real (Ctrl Shift A) that would work for interactive use. Since you were using python in your original project here is a small python script I wrote that might accomplish your mission:

import bpy

def mission1(group, scn):

    parent = bpy.data.objects.new("parent", None)
    scn.objects.link(parent)

    for obj in group.objects:
        o2 = obj.copy()
        o2.parent = parent
        for i in range(len(o2.material_slots)):
            ms = o2.material_slots[i]
            ms.link = 'OBJECT'
            ms.material = o2.data.materials[i]
        scn.objects.link(o2)

    return parent
#
#

anchor = mission1(bpy.data.groups["Group"], bpy.context.scene)
anchor.location = [2,0,2]

The parent object is not strictly necessary, but it gives you the ability to manipulate the location and orientation of the group very much like the dupli_group empty you were using before.

An important drawback is that once you have instantiated these copies modifications to the group (adding, removing, relocating objects) will not be propagated.

$\endgroup$
  • $\begingroup$ Thank you for clarifying this. I'm not sure I understand correctly, do you keep the mesh in memory or do you copy it entirely ? Since I have several thousand instances of my objects, I want to keep the mesh data shared across the instances. $\endgroup$ – nantille Jun 4 '15 at 15:14
  • $\begingroup$ The object.copy() creates a new instance of the object datablock, but the mesh datablock is shared by the original and the copy. If the mesh were not shared, then it would not be necessary to use per-object materials. $\endgroup$ – Mutant Bob Jun 4 '15 at 15:17
  • $\begingroup$ Ok that's what I thought, I used groups because of the dupli_group property but then it lead me to this lack of control. I was using parenting and grouping to simplify selection of instances in the viewport but this came at a certain cost. Thanks ! $\endgroup$ – nantille Jun 4 '15 at 20:53

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.