Results 1 to 13 of 13
  1. #1
    Member revolt_randy's Avatar
    Join Date
    Oct 2008
    Location
    In a van, DOWN BY THE RIVER!!
    Posts
    3,067

    assign a material to a new material slot

    Hi all,

    Straight to the problem. I have a script that creates a text file containing the names of meshes and the names of their materials. Then the script removes all the materials from all the meshes. Now, after removing all the materials, I need to add the materials back to the meshes based on the names in the text file.

    here's a small bit of the code:
    Code:
    # get text file - 'obj-mat_list' - that contains the object's names and
    # materials assigned to it
    input_filename = "obj-mat_list"
    input = bpy.data.texts.get(input_filename)
    
    print("\n\n", input)
    for index in input.lines:
        
        if index.body:
            if index.body[0] != "[":
                print("\nobject =", index.body)
                current_obj = index.body
                bpy.context.scene.objects.active = bpy.data.objects[index.body] 
                bpy.ops.object.material_slot_add()
            else:
                print("\nmaterial =", index.body)
    This code reads the text file, and if the line in the text file is the name of a mesh, it makes it the active mesh then creates a new material slot for the mesh, the next line of the text file is the name of the material to be assigned to the mesh.

    The text file that my script produces looks like this:
    Code:
    Cube
    ['Blue']
    Icosphere
    ['Green']
    Sphere
    ['Red']
    So the first bit of code goes thru the text file, gets the mesh object's name and makes it the active object, then it adds a new material slot. Now I need to assign the material to the object's new material slot.

    For example, the first line of the text file is the name of the mesh object 'Cube', the second line is the name of the material (it's a python list in case there are multiple materials) as seen in the material panel.

    So how do I assign the material named 'Blue' to the mesh 'Cube' after I've created the material slot? I need to do the assignment in the 'else' statement in the first bit of code.

    Thanks for any help, and if any more information needed, I can supply that.
    Randy
    There's no such thing as gun control, but there is people control......
    My Vimeo Channel with Blender Tutorials



  2. #2
    Code:
    #say cube is your Cube object:
    cube.material_slots[0].material = bpy.data.materials['Blue'] # assigns 'Blue' material to first material slot



  3. #3
    Member batFINGER's Avatar
    Join Date
    Jun 2007
    Location
    Lochiel NSW Australia
    Posts
    1,857
    Here's my take on it.
    You can get away with just adding a material to the materials collection of the mesh, a slot is added for you.

    Code:
    import bpy
    # get text file - 'obj-mat_list' - that contains the object's names and
    # materials assigned to it
    input_filename = "data"
    input = bpy.data.texts.get(input_filename)
    
    print("\n\n", input)
    for line in input.lines:
        txt = str(line.body)
        islist = txt[0] == "["
        if islist:
            if obj is None:
                continue
            matlist = eval(txt)
            for mat in matlist:
                if mat not in obj.data.materials:
                    obj.data.materials.append(bpy.data.materials.get(mat))
                else:
                    print("%s in %s materials" % (mat, obj.name))
        else:
            obj = bpy.data.objects.get(txt)
            if obj is None:
                print("ERROR %s is not an object" % txt)
    Last edited by batFINGER; 06-Mar-12 at 00:47.



  4. #4
    Member revolt_randy's Avatar
    Join Date
    Oct 2008
    Location
    In a van, DOWN BY THE RIVER!!
    Posts
    3,067
    Thanks for the help cmomoney & batFINGER! but I'm still unable to get things to work as I like. To provide more details/better example, I've uploaded the .blend to here:
    http://www.pasteall.org/blend/12283

    Basically, the script generates a text file with the mesh object's name & names of materials assigned to the mesh. I am having problems re-assigning the materials back to the objects. A few parts of the code are remarked out as not needed now. One part that is remarked out removes the materials from the objects.

    I realize that removing materials from objects to only re-assign them again to the objects may seem like a silly exercise, but it's not, it's a work around for a bug in blender....

    Thanks,
    Randy
    There's no such thing as gun control, but there is people control......
    My Vimeo Channel with Blender Tutorials



  5. #5
    Try this: http://pasteall.org/blend/12291

    I changed your output file just a little, but it works, even with multiple materials.



  6. #6
    Member revolt_randy's Avatar
    Join Date
    Oct 2008
    Location
    In a van, DOWN BY THE RIVER!!
    Posts
    3,067
    Thanks cmomoney!! That works. In my attempts I kept failing on
    Code:
    bpy.data.materials.get()
    which no matter what I used as an argument, I couldn't get it to return a material. It got to be quite aggravating that I couldn't get it to work. I was about to go with your first approach, just assigning a single material to the first material slot, which would work in my case (since all my meshes only have 1 material each), but it wouldn't work where there was more than one material.

    Randy
    There's no such thing as gun control, but there is people control......
    My Vimeo Channel with Blender Tutorials



  7. #7
    Glad to help. Looking at your code made what you were trying to do clearer.



  8. #8
    Member batFINGER's Avatar
    Join Date
    Jun 2007
    Location
    Lochiel NSW Australia
    Posts
    1,857
    get is the same as
    Code:
    if name in bpy.data.materials:
        return bpy.data.materials[name]
    else:
        return None



  9. #9
    Member revolt_randy's Avatar
    Join Date
    Oct 2008
    Location
    In a van, DOWN BY THE RIVER!!
    Posts
    3,067
    Thanks batFINGER, didn't know I could do it that way also. I'm thankful to both cmomoney and batFINGER for helping out here.

    I've got the script working in my test case I posted, still have a few hurdles to over come to get it to work on the real thing, which I'm sure I can overcome. Still need to add a GUI & define operators to call various parts of the basic script I posted.

    I'll post the details when I'm finished....

    Randy
    There's no such thing as gun control, but there is people control......
    My Vimeo Channel with Blender Tutorials



  10. #10
    Member
    Join Date
    Feb 2008
    Location
    Sweden
    Posts
    77
    Hi Randy,

    Did yoy got this to work as an addon?

    //Wilnix



  11. #11
    Member revolt_randy's Avatar
    Join Date
    Oct 2008
    Location
    In a van, DOWN BY THE RIVER!!
    Posts
    3,067
    Hi wilnix,

    Quite an old thread... So I looked for the file, and to my surprise, it appears to work.....

    matscript1.png

    I don't know how well it works. Please read the red text in the above screenshot to understand what this script did.

    Could you tell me what you are looking for? I might be able to tell if this script would help you.

    Randy
    There's no such thing as gun control, but there is people control......
    My Vimeo Channel with Blender Tutorials



  12. #12
    Hello. Your script would solve a big problem.
    I can try it?
    if you can tell me where to download it?
    you've got a great idea. compliments.
    thanks, simdal



  13. #13
    Member
    Join Date
    Oct 2015
    Location
    Sweden
    Posts
    11
    Hi

    Check out this Addon on Blendermarket - Simple Material Replacement
    https://www.blendermarket.com/produc...al-replacement

    //M



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •