How to duplicate a material?

I’m trying to duplicate a material but I can’t see any methods to do this in the docs. I’d like it to be a total duplicate including the attribute library, texture, colour etc… - this is obviously simple to do when navigating the material library (right click > duplicate…)

Am I missing something in the docs, or do you have to do this by creating a new material and manually setting all the options in the material to match?

Thanks!

In the Materials palette - select the materials that are “In Model.”
Materials need to be in the current model in order to copy them.
Select the material you want to copy - and hit the plus sign to duplicate.
Rename it accordingly.
Material Copy

@KyleB ,
wawmsey7 is on Mac and we are in a Ruby API category :wink: :beers:

1 Like

I guess so. The good news is that method you have to creat only once… :innocent:

1 Like

ahhhhh! Thanks for stepping in!

1 Like

If you have version 2017 or higher, you might try going through a temporary file.

(1) Change the name of the source material to that which you want the copy to be.
See Material#name=

(2) Save out a SKM file using the copy name for the SKM
See Material#save_as

(3) Change the name of the source material back to what it was before.

(4) Load the copied material into the model’s materials collection:
See: Materials#load

It seems that I remember that at one time attached attribute dictionaries did not save with the SKM. I think this was fixed but do not remember quite when (ie, what version.)

I do see that there was an attribute dictionary fix (re array data) in v2019.2.

2 Likes

No API method that does this, no. You need to do it manually.
(Btw, feel free to log a feature request in our issue tracker: Issues · SketchUp/api-issue-tracker · GitHub)

2 Likes

Thanks for all your responses - I’m surprised this isn’t a thing, but good to know I won’t be making a bodge of a function that already exists!

In my circcumstances this is what seems to have worked for me in the end…

What I’m doing is loading a file that just contains 1 face with a material - then getting the material from the face - like this…

file=Sketchup.find_support_file( path )
def=Sketchup.active_model.definitions.load(file)
mat=def.entities.grep(Sketchup::Face)[0].material

and then I’d say change the colour or texture whilst wanting everything else to stay the same

the problem I was finding was that every time I tried to re-run this i’d get the same material and just end up editing that

so simply removing the defintion from the definition list so that it forgets the reference to it seems to have done the trick

Sketchup.active_model.definitions.remove(def)

I guess so making sure it’s coming from an external source every time, similar to how Dan’s method would work…