1
$\begingroup$

If I import an SVG curve by python, always is working except of assign material. Manually I can assign any material, but by python nothing works. There is part of my code:

mat = bpy.data.materials.get("Z")
for emboss in embosses:
    emb_name = emboss[:-4]
    loadedSVG = bpy.ops.import_curve.svg(filepath = embossDir + emboss, filter_glob=".svg")
    for obj in bpy.data.objects:
        if obj.tag == False:
            obj.name = emb_name
            obj.tag = True
            x, y, z = obj.dimensions 
            if x >= y:
                needRatio = maxSize / x
            if y > x:
                needRatio = maxSize / y
            bpy.ops.object.select_pattern(pattern = emb_name)
            bpy.data.objects[emb_name].scale = (needRatio, needRatio, needRatio)      

            #two from my attempts, nothing works, material is correct            
            bpy.data.objects[emb_name].materials = [mat]  
            bpy.data.objects[emb_name].setMaterials([mat])  
            #end of not-working code            

            bpy.data.objects[emb_name].rotation_euler = (0, 0, math.radians(90))           
            bpy.data.objects[emb_name].location = (-0.06864 + (maxSize - y * needRatio)/2, 1.18165 + (maxSize - x * needRatio)/2, 0.02419)      
            bpy.ops.object.select_all(action='DESELECT')

Only message "AttributeError: 'Object' object has no attribute 'materials'" in the system console.

I know that I'm doing some mistake, but I can't find anything three days in code but through the web too. Thanks for correcting my code.

$\endgroup$
1
  • $\begingroup$ Please mark Rick Riggs' answer as the solution if it solved your problem or was the most helpful. $\endgroup$ – InverseTelecine Mar 25 '16 at 14:22
2
$\begingroup$

Instead of:

        #two from my attempts, nothing works, material is correct            
        bpy.data.objects[emb_name].materials = [mat]  
        bpy.data.objects[emb_name].setMaterials([mat])  
        #end of not-working code

Try:

        bpy.data.objects[emb_name].active_material = bpy.data.materials['Z']

If this still does not work, you may need to look at adding a material slot first.

        bpy.ops.object.material_slot_add()
$\endgroup$
1
  • $\begingroup$ Thanx to Rick, it works great. $\endgroup$ – Jiri Mlnarik Mar 8 '16 at 5:15

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.