Create own materials to objects from Python

0

People, what I want is to create own materials to each object so that when modifying some property of the material, it does not do it in more than one object. What happens is that all the materials created assign them to only one object, the first to be created, and I do not understand why. Annex code in python to be executed from blender for more clarity. I await an answer and thank you very much.

Another solution could be to create an object with a single material and duplicate it so that everyone has material, but when I modify a property of the material as I could unlink them so that it does not modify it in all the objects but only one in specific.

import bpy

bpy.ops.mesh.primitive_uv_sphere_add(size=1, location=(-1,-2,0))
nombre = "C1"
bpy.context.object.name = nombre

objetoActivo = bpy.context.active_object
mat = bpy.data.materials.new(name ='material')
objetoActivo.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(size=1, location=(1,2,0))
nombre = "C2"
bpy.context.object.name = nombre
mat = bpy.data.materials.new(name ='material1')
objetoActivo.data.materials.append(mat)
    
asked by Luis Gutiérrez 31.05.2018 в 18:54
source

1 answer

0

The problem is that objetoActivo always refers to the first object that you create, reference assigned in objetoActivo = bpy.context.active_object and that you never reassign later. That's why with objetoActivo.data.materials.append(mat) you always add the material to the same object.

You can reassign the value of objetoActivo after creating the second object by objetoActivo = bpy.context.active_object or directly:

import bpy

bpy.ops.mesh.primitive_uv_sphere_add(size=1, location=(-1,-2,0))
nombre = "C1"
bpy.context.object.name = nombre
mat = bpy.data.materials.new(name ='material')
bpy.context.object.data.materials.append(mat)


bpy.ops.mesh.primitive_uv_sphere_add(size=1, location=(1,2,0))
nombre = "C2"
bpy.context.object.name = nombre
mat = bpy.data.materials.new(name ='material1')
bpy.context.object.data.materials.append(mat)
    
answered by 31.05.2018 в 19:48