Get material by name and result is blank

I wanna get the duplicated material but the result is blank (I’m sure the material exists). Why?
And I detected that when I run the following code second times, everything work perfectly, so I think I need some method to update something?

all_entities = Sketchup.active_model.entities
all_mtls = Sketchup.active_model.materials
context = VRay::Context.active
scene = context.scene

# Get mtl by name (using VRay method) 
mtl_plugin = scene['/red hat']
# Duplicate mtl (using VRay method)
mtl_plugin.duplicate(name:'/red hat2' ,include_refs: true)

VRay::refresh_ui

face_array = Array.new
all_entities.each do |entity|
	face_array << entity if entity.class == Sketchup::Face
end

face_array.each do |face|
	duplicated_mtl = all_mtls['red hat2']  # This blank
	puts 'Duplicated_mtl : ' + duplicated_mtl.to_s
	face.material = duplicated_mtl 
end

I have no idea what is this…and where its from:

but I can see some potential mistake like:

'/red hat2' 
# is not equal to   
'red hat2'

_


Some advise:

You can simplify using Enumerable #grep method e.g. like:

face_array = all_entities.grep(Sketchup::Face)
3 Likes

I wrote code follow the document of VRay Renderer.


It’s not mistake, the syntax of VRay requires ’ / ’


When I count the number of all_mtls, I saw that after duplicated mtl, the number doesn’t changed. How to update all_mtls? (as I said before, when run 2 times, the all_mtls will update)

number_mtl = all_mtls.count  # It return 21 
puts 'number: ' + number_mtl.to_s

# Get mtl by name (using VRay method) 
mtl_plugin = scene['/red hat']
# Duplicate mtl (using VRay method)
mtl_plugin.duplicate(name:'/red hat2' ,include_refs: true)

VRay::refresh_ui

number_mtl = all_mtls.count  # After duplicated mtl, I expected that it return 22 but it still 21
puts 'number: ' + number_mtl.to_s

Sorry I cant help more, I deleted everything related to Chaos from my computer.
Perhaps you can ask on their forum how their methods works…

However the key sentence can be:

To set a parameter value in the V-Ray UI via a script, the “user data” counterpart must be adjusted as well. Otherwise, the change will only affect the V-Ray scene.

1 Like

Thanks for your help :heart: I’ll ask on VRay forum.