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 Lipid.location = 0 Lipid.location = 0 # set the rotation of object Lipid.rotation_euler = 1.5708 Lipid.rotation_euler = -1.5708 Lipid.rotation_euler = 0.7853 # set material Lipid.active_material.diffuse_color = (0, val(), 0) Lipid.active_material.use_transparency = False Lipid.parent = None #print (lis.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