how to add mat name ?

if i add a new mat with

def addmat1(name5):

obj = bpy.context.scene.objects.active

Add material to selected object

newMat = bpy.data.materials.new(name5)
obj.data.materials.append(newMat)

print ()

print ( ‘$$$$$$$$$$$$$ YYYYYYYYYYYYYYY mat name =’, name5)

print ()

i tmight not work cause if there is already same name being ue i get name.oo1

so how can i add new mat with name and have the real name added for this new mat ?

and also how to selet the new slot with new mat on the object ?

thanks

Couldn’t you just test if the name exists before trying to create the new material?

yes i do have to add checking if there is a mat or not
and also if name exist !

but is there a way to add mat and get the new added name
cause i need that name after for something else

i’ll check my note for this none name got to be somewhere !

did that one here



###
 
def addmat1(name5):
 
 obj = bpy.context.scene.objects.active
 
 if obj != None:   # obj exist selected
 
  for mat_slot in obj.material_slots:
   mat = mat_slot.material
   print ('in selected obj    mat =',mat,' mat name =',mat.name)
 
 else:     
  print("Object does not exist.")
 
  newMat = bpy.data.materials.new(name5)
  obj.data.materials.append(newMat)
 # Add material to selected object
 
 
 
 print ()
 
 print ( '$$$$$$$$$$$$$   YYYYYYYYYYYYYYY  mat name =', name5)
 print ( '$$$$$$$$$$$$$   YYYYYYYYYYYYYYY  mat name =', name5)
 print ( '$$$$$$$$$$$$$   YYYYYYYYYYYYYYY  mat name =', name5)
 print ( '$$$$$$$$$$$$$   YYYYYYYYYYYYYYY  mat name =', name5)
 
 print ()



error on name which does not exist ?

but there could be more then one mat with same name hwo do i find the last one ?

thanks

Hi Ricky

is it as simple as?


newMat = bpy.data.materials.new(name5)
print(newMat.name)

if there are no mat on object
then it works fine !

if there are mat with same name on object

i have to check if name already exist and also and if it does then change the name
for new mat but i got to know what new name is cause i need it after to do something else

i can see the slot name
print (‘mat_slot mat =’,mat_slot)

mat_slot mat = <bpy_struct, MaterialSlot(“Skin1.003”)
but how do i extract the name from this ?

thanks

found this

with this

print (‘mat_slot mat =’,mat_slot.material.name)

i get this

mat_slot mat = <bpy_struct, MaterialSlot(“Skin1”)

how do i get the name Skin1 alone ?

2 - len mat
if there are no mat on object
i get a len = 1

how come?

thanks

mat_slot.name ?!

if you create a new material via bpy.data.materials.new(), it doesn’t matter if there’s already a material with that name, blender will add a sequence number. And as it returns the material instance, you don’t need to know the name (but you do know, if you really wanna reference with the name -> mat.name).

1 - slot len ?
how come i get len = 1 if there are not mat on the object ?

2 - i have a new problem
now when i add a new mat to the ob with right name but empty material


 
def addmat1(name5):
 
 obj = bpy.context.scene.objects.active
 
 if obj != None:      # obj exist selected
 
  print ('len mat =',len(obj.material_slots))
  print ()
  len1=len(obj.material_slots)
 
  for kk1 in range(0,len(obj.material_slots)):
   print ('mat (',kk1,')=',obj.material_slots[kk1])
 
  print ()
 
  if len1&gt;1:
 
   for mat_slot in obj.material_slots  :
    mat = mat_slot.material
 #   print ('  dir obj.material_slots =',dir(obj.material_slots))
 #   print ('  dir  =',dir(mat))
 #   print ('in selected obj    mat =',mat,' mat name =',mat.name)
    print ('mat_slot   mat =',mat_slot.material.name)
    print (' adding new mat on ob mat list ')
  else:       #Add new mat first slot
    print ('name5=',name5)
    newMat = bpy.data.materials.new(name5)
    obj.data.materials.append(newMat)
#    obj.data.materials.append(name5)
    print (' newMat =',newMat)
    print (' adding new mat on ob with no mat')
 
 else:     
  print("Object does not exist.")
 
  newMat = bpy.data.materials.new(name5)
  obj.data.materials.append(newMat)
  print ('name5=',name5)
  print (' adding new mat on mat list only')
 
 # Add material to selected object
 
 
 
 print ()
 
 print ( '$$$$$$$$$$$$$   YYYYYYYYYYYYYYY  mat name =', name5)
 print ( '$$$$$$$$$$$$$   YYYYYYYYYYYYYYY  mat name =', name5)
 print ( '$$$$$$$$$$$$$   YYYYYYYYYYYYYYY  mat name =', name5)
 print ( '$$$$$$$$$$$$$   YYYYYYYYYYYYYYY  mat name =', name5)
 
 print ()
 
 
 
###

when using the # code
how come we loose the indentation now !

thanks

can’t you use ob.data.materials? That should make it easier to simply append a material

i’m already using it

newMat = bpy.data.materials.new(name5)
obj.data.materials.append(newMat)

but instead of adding the newly define mat it adds another one !

it add sne amt which has nothing to do with the predefine mat
like adding another mat

ame5= Skin1
newMat = <bpy_struct, Material(“Skin1.001”)>
adding new mat on ob with no mat

it is supposed to add only skin1 but it is alos adding skin1.001 !
with no specs as defined by function for nodes making

so why is it adding 2 mat instead of 1 mat?

thanks

here are the commands

newMat = bpy.data.materials.new(name5)
obj = bpy.context.scene.objects.active
obj.data.materials.append(newMat)

first line adds a new mat to the mat list only

the other 2 commands line
tries to add mat to the ative object
and it does but not the newly defined mat with proper name it adds a new mat with no specs!

how do you add the defined mat to the active object ?

2- add a new mat slot to ob may be ?
there is a bpy ops command to add new mat slot on ob
bpy.ops.object.material_slot_add() # Add a material slot

is there an equivalent for bpy.data ?
or no need for it and just add a new mat ?

thanks

Ricky everytime you call bpy.data.materials.new(…) it creates a new material.

If you already have a material named skin1 and this is the one you want to use you can do something like


name = "skin1"
myalreadydefinedmaterial = bpy.data.materials.get(name)

if myalreadydefinedmaterial is not None:
    obj.data.materials.append(myalreadydefinedmaterial)
else:
    obj.data.materials.append(bpy.data.materials.new(name))


You can do the same test on the material slot with



obj.material_slots.get(name)


which will return the material_slot with material of name name if it exists or None if it doesn’t.

PS or obj.data.materials.get(name)

ok here is short test file

it does add the proper mat now on the active object
but it also adds another one with no spec called skin1.001 in the mat list which is not needed

and not certain why another mat is added ?

i redid a min function for adding mat call addmat1

object can be selected or not
or might be no active ob which means it should add mat to the mat list and user will add mat to ob and assing it manualy!

i did add the original function addmat2 at the end of the script

testnodemat1.blend (544 KB)

thanks

foud something here

this adds only one mat

newMat =bpy.data.materials[name5]

but if i use this one

newMat = bpy.data.materials.new(name5)

this adds the new mat but also adds another empty mat

why is it adding 2 mat here ?

this function seems to work i think


 
def addmat3(name5):
 
 
 
# for obj in bpy.context.selected_objects:
 
 lenselect=len(bpy.context.selected_objects)
 print (' qty selected ob =',lenselect)
 print ()
 print (' Active ob =',bpy.context.scene.objects.active)
 print ()
 
 
 
 if lenselect &gt;=1:
 
  obj = bpy.context.scene.objects.active     # Selected or not 
  print ()
  print ('before len mat =',len(obj.material_slots))
  print ()
 
#  newMat = bpy.data.materials.new(name5)     # Add new mat to material list
  newMat =bpy.data.materials[name5]
 
#  mat1 = bpy.data.materials[name5]
#  obj.material_slots[obj.material_slots.__len__() - 1].material = mat1
  obj.data.materials.append(newMat)   # Add mat to object 
  print ()
  print ('before len mat =',len(obj.material_slots))
  print ()
 
 else:              # No object selected so add mat to material list 
  mat1 = bpy.data.materials[name5]
 
 
 
###
 
 


anyone knows why we don’t see the indentations anymore in code section ?

thanks

i think i found the second mat !
there is a makemat function being call whch makes a new mat !

thanks