Creating copy of static model

I want to create a copy of a loaded (static) model, including it’s material, so that I can change the color in the copy’s material. How do I accomplish this?

I have tried with this code:

	//NodePath wall = window->load_model(framework.get_models(), "models/cube12").instance_to(window->get_render());
	NodePath wall = window->load_model(framework.get_models(), "models/cube12").copy_to(window->get_render());

	LVecBase4f col(0.3, 0, 0, 1);
	Material* mat = wall.find_material("default2");
	PT(Material) newMat = new Material(*mat);

(The model has already been loaded once earlier.)

You’re doing it mostly right, but try replacing:



wall.set_material(newMat, 1);

The reason this is necessary is that you are locating the material with a find_material() call, which automatically searches all child nodes of wall. Presumably it has found the material in question from a child node. That means you need to replace the material by setting the new one on the same child node, otherwise the child node’s original material setting will override whatever you assign to wall, the parent node.

Unless, of course, you use an override value, which is my suggested change. The override value ,1 in this case means to force the material to apply to all child nodes of wall (unless they have a higher override value, which presumably they won’t).

The copy_to() technique will make a copy of all nodes and their properties, but it won’t duplicate out the materials, as you have observed. Note that calling load_model() of the same filename is effectively the same thing as another call to copy_to(), since models are cached internally at each load.


Thanks David! That did the trick.
I’m very grateful for the great support. :slight_smile: