V5 ObjectsByMaterialIndex bug?


(Przemysław Doliwa) #1

I assume i should get with this method an array of objects which have certain material applied but instead of that i get only one item selected even when using provided example only selected object stays selected. What is weird here that even copied object around have incrementing material id?

Can someone tell me whats going on?

EDIT: I tried to use Rhino.MaterialName but this method also fails - i mean if material have name like “Basic Material” and try to use SelMaterialName command rhino runs first “SelMaterialName Basic” then “Material” - looks like theres no way to pickup objects by material via script.


(Przemysław Doliwa) #2

@pascal Could you tell anything about this behaviour? Am i doing sth wrong or this is a bug?


(Pascal Golay) #3

Hi DW - The material index is unique per object - it’s not what it may look like, that material indices are shared. Materials are a little tricky to work with in a script…
-SelMaterialName “Material Name” (in double quotes) works though so you can use Rhino.Command() + Rhino.SelectedObjects() to get hold of the array that I think you want.

Sub Main()
	Dim matName
	matName = Rhino.StringBox()
	If isNull(matName) Then Exit Sub
	

	Rhino.Command("_-SelMaterialName " & chr(34) & matName & chr(34))
	Dim ids
	If Rhino.LastCommandResult() = 0 Then
		ids = Rhino.SelectedObjects()
	End If
	
End Sub

-Pascal


(Przemysław Doliwa) #4

Thank you Pascal! Double quotes worked like a charm.

Btw. i thought when im getting material index and then objectsbymaterial providing index i would get array of objects with certain material but if each object has uniqe mat id i dont get this structure.


#5

id and index are two separate things, but in general working with materials in scripts is tricky and not very reliable; I had better luck with using scripting RDK methods than the ones in RhinScript, but still it is not perfect. Seems like scripting never keeps up with changes from v4 to v5 to now WIP.

In many cases to get the material ID from index, I find RDK method actually return real working ID compared to the one from RhinoScript. Here is the somehow cryptic online help for scripting RDK: http://developer.rhino3d.com/guides/rhinoscript/scripting-methods-for-rdk/
You may look into the “MaterialInstanceId(intMaterialTableIndex)” method.

–jarek


(Przemysław Doliwa) #6

Thank you Jarek very much! Script i needed was quite simple but next time ill look into rdk for sure cause rs material methods made me dizzy :stuck_out_tongue: