# How to assign a random color to objects and make them parent to specific objects using Python?

I have two objects lists in the scene:

Lipid_names = ['DGDG','DGTS','MGDG','PE','PI','SQDG','PG']
Lipid_regions = ['Lipid_DGDG','Lipid_DGTS','Lipid_MGDG','Lipid_PE','Lipid_PI','Lipid_SQDG','Lipid_PG']


The objects in the list Lipid_names should be set as child to objects in the list Lipid_regions.

So, DGDG should be set as child of Lipid_DGDG, DGTS should be set as child of Lipid_DGTS, and so on.

AND I also want to change the color of these objects:

Now, when I run the code, it does not make parent-relation to all objects but only first one. The code seems okay to me as I am not an expert programmer.

For reference, I uploaded the .blend file with all data inside it. https://www.dropbox.com/s/n52kay7jx1u13hc/Lipid_packing.blend?dl=0

import bpy
import random
from random import uniform
scene = bpy.context.scene

for selected_obj in scene.objects:
selected_obj.select = False

Lipid_names = ['DGDG','DGTS','MGDG','PE','PI','SQDG','PG']
Lipid_regions = ['Lipid_DGDG','Lipid_DGTS','Lipid_MGDG','Lipid_PE','Lipid_PI','Lipid_SQDG','Lipid_PG']

# All regions over the lipids will be spread
regions = [i for i in scene.objects if i.name.startswith('Lipid_')]

# All lipids
lipids = [i for i in scene.objects if i.name in Lipid_names]
lpds = [print(i.name) for i in scene.objects if i.name in Lipid_names]

for Lipid in lipids:
val = lambda: random.random()
# set Location of the object to origin
Lipid.location[0] = 0
Lipid.location[1] = 0
Lipid.location[2] = 0
# set the rotation of object
Lipid.rotation_euler[0] = 1.5708
Lipid.rotation_euler[1] = -1.5708
Lipid.rotation_euler[2] = 0.7853
# set material
Lipid.active_material.diffuse_color = (0, val(), 0)
Lipid.active_material.use_transparency = False
Lipid.parent = None

#print (lis[0].name)
for iter in range(len(lipids)):
print(iter)
for lipid in lipids:
index = 0
if regions[index].name == 'Lipid_' + lipid.name:
lipid.parent = bpy.data.objects[regions[index].name]
index += 1

• What is your question exactly? – dr. Sybren May 31 '17 at 19:56
• Try using random.randint(0,255) for the val. – Strapicarus May 31 '17 at 20:01
• +Sybren I edited my question. I hope it is more understandable now. – mGm May 31 '17 at 22:37
• I tried random.randint(0,255) as well, but this code change the color for only 1 object. The other objects have same colors. – mGm May 31 '17 at 22:38

you can change the color with a simple random in r - g -b and in order to assign each lipid_name as child of lipid_region you should use some loop... check this after run the script:

import bpy
import random

scene = bpy.context.scene

for selected_obj in scene.objects:
selected_obj.select = False

Lipid_names = ['DGDG','DGTS','MGDG','PE','PI','SQDG','PG']
Lipid_regions = ['Lipid_DGDG','Lipid_DGTS','Lipid_MGDG','Lipid_PE','Lipid_PI','Lipid_SQDG','Lipid_PG']

for object in bpy.context.scene.objects:
if object.name in Lipid_names:
#color
r, g, b = [random.random() for i in range(3)]
object.active_material.diffuse_color = (r, g, b)

#parent assign
lipid_index = Lipid_names.index(object.name)
parent_name = Lipid_regions[lipid_index]
object.parent = bpy.data.objects[parent_name]