4
$\begingroup$

I am using ePMV Blender addon to import molecular structures into Blender. It generates multiple objects (protein sub-structures called helices, coils and strands) and it assigns a separate material slot to each of them automatically. This generates hundreds of individual default materials.

The materials are named with the convention: mat_molecule_A_Strand1, mat_molecule_A_Strand2, mat_molecule_A_Strand3 ...

mat_molecule_B_Helix1, mat_molecule_B_Helix2, mat_molecule_B_Helix3 ...

A and B are different parts of the molecule (called chains).

Igg molecule imported by ePMV

I would like these parts to have separate materials. On top of that, I would like to change materials WITHIN each chain, for example one shader from object 1 to 15, other shader from object 16 to 30, etc.

I could supposedly change one object material and manually link this material to the rest of them clicking one by one. But some molecules have hundreds of these objects, so I believe I would need to use Python to do it in which I'm not so advanced. Any help with how to bite this highly appreciated.

So the pseudocode would be something like below.

For changing materials for all objects in chain A:

IF object_name CONTAINS "_A_" 
   { FOREACH object_name
     {APPLY material_01 to object_name}
   }

For changing materials for only 15 first objects in chain B:

  IF object_name CONTAINS "_B_" 
       { $i = 1 
         DO {
               APPLY material_02 to object_name$i
               $i++                   

            } UNTIL $i = 15
       }
$\endgroup$
6
$\begingroup$

for the python specific stuff

objects = bpy.data.objects

material_01 = bpy.data.materials['some_named_material']
for obj in objects:
    if '_A_' in obj.name:
        obj.data.materials[0] = material_01

Si = 1
for obj in objects:
    if '_B_' in obj.name:
        obj.data.materials[0] = material_02
        Si+=1
        if Si == 15:
            break

You might have to do additional sorting of all objects that contain those strings.

$\endgroup$
  • $\begingroup$ This is exactly what I was looking for! $\endgroup$ – marcheenek May 11 '15 at 16:04
2
$\begingroup$

Duplicate your materials with this:

import bpy
mat = bpy.data.materials['Material']
duplicate = mat.copy()
# now there is "Material.001" identical to "Material"
duplicate.name = "Some Better Name"   # No one likes .001

Change their properties (cycles or blender internal):

Control Cycles material nodes and material properties in Python

Assign them to objects:

How to assign a new material to an object in the scene from Python? (works also for assigning existing materials)

$\endgroup$

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.