[Closed] Reference problem
Hello,
I’m having a problem with trying to change a material to another material type. I’m guessing it might be a problem with how I’m passing the reference of the material? All I can do is change the properties of the material through a function, e.g. name, but if I try making a new material and assigning the reference of the old material to the new material, it does nothing.
The following is a simplified version of what I’m trying to do:
(
fn convertMat &mat =
(
tempMat = VrayLightMtl()
tempMat.name = mat.name
tempMat.texMap = mat.diffuseMap
mat.name = "new material name" -- works
mat = tempMat -- does nothing, material is created but not replaced
)
-- array to hold the materials of the selected objects
local mats = #()
for i = 1 to selection.count do
(
mats[i] = selection[i].material
)
-- convert the materials to VrayLightMtl
for i = 1 to mats.count do
(
convertMat &mats[i]
)
)
Any help on this greatly appreciated!
I should add that if I do:
convertMat &selection[i].material
it works. But when I save the materials to an array first, it doesn’t work.
Thanks
Any ideas, anyone? I’ve tried heaps of different things without any luck so far =\
Hi,
By-Reference operates on the data of the variable or property, so there is a difference between replacing the whole object inside an array or inside the living scene objects tree structure.
When you pass &mats[i] to the function, you are sending the pointer to the i-th element of the array. The result is that the array’s i-th element gets replaced, so at the end, mats contains new materials. So it actually works, but of course it does not affect any scene material assignments, because the array is a “snapshot” of the materials assigned to the selected objects, but does not include any information about where these materials are being used.
If you pass &selection[i].material, you are operating on the material property of a specific object, so replacing the material passed by reference changes what is assigned to the object, but does not mean that you changed the class of the material itself. You only created a new material and assigned it back to the scene object, but any other UNselected objects that have the SAME material assigned will not be updated!
So there is no real reason to pass the material by reference in the first place. You could simply loop through the selection, pass the material of each selected object to the function AND pass back the resulting new material as the result of the function. You could simply assign the returned material to the current object and keep on looping.
In other words,
for i = 1 to selection.count do
(
convertMat &selection[i].material
)
and
for i = 1 to selection.count do
(
selection[i].material = convertMat selection[i].material
)
and
for i in selection do
(
i.material = convertMat i.material
)
would do the same
(in the second and third example, the function should be changed to not expect an argument by-reference and return the tempMap.
Thank you so much Bobo! That really clears up a lot of what I thought should work, but didn’t!
Can’t thank you enough for that explanation! Cheers!