Assign Material via .Net during Runtime

Hy,

I successfully assign a Material to a component during simulation but the different color of the material won’t appear!? If I assign the material afterwards manually everything is fine…

But what should I do that this working just by code!?

I’m using IMaterial, Color, ISimNode.

Thx & Regards
Feature

So you are assigning material to root node of component? If a feature has a material assigned to it already, the component material won’t override it. What you can do is force material inheritance.

Color color = new VisualComponents.Create3D.Color((byte)red, (byte)green, (byte)blue, (byte)alpha); //Create the color
customMaterial.Color = color; //Set the color over RGBA
customMaterial.Ambient = color; //Set the Ambient over RGBA
customMaterial.Specular = color; //Set the Specular over RGBA
customMaterial.Emissive = color; //Set the Emissive over RGBA
customMaterial.Opacity = (byte)alpha; //Set Transparency becauseAlphaValue works not well...
customMaterial.ForceUpdate(); //Update Material

ISimNode sn = (ISimNode)component;
sn.MaterialInheritance = MaterialInheritance.ForceInheritNode;
sn.Material = customMaterial;
sn.Rebuild();

I tried different ways to force material inheritance, but it always shows strange results… :frowning:

What is working is:

ISimNode sn = (ISimNode)component;
sn.HighlightMaterial = customMaterial;
sn.Rebuild();

but AlphaValue is not working at all…

Can you tell me how to Update/Render the whole component correctly that the result is shown immediately?

Regards
Feature

Sounds like you should do render because changing material does require render update. You can try contacting support or others can help. I won’t be able to test until later today.

_RenderService.RequestRender(); does the trick! :slight_smile:

Thx & Regards
Feature

1 Like

Hello, could you share the useful code, I get the same problem too.

You can see that I wrote all you wrote but did not work…

Hy,

try it so:

  • Get a standardmaterial
  • Create your userdefined Material
  • Copy all from the standardmaterial without the color, choose your color as you do!

Then it should work! :slight_smile:

Regards
Feature

Hi, there. I have the same problem on assigning materials to the components using C# .net.
The colors can be assigned to the components but the colors shows not correctly. However, It shows the correct colors in the interface of “Tools - Material - assign”, if I change a little bit of the corresponding color in the interface of “Tools - Material - assign”, the color on the component can immediately show the correct color. I am wondering what the problem it is, is it a problem of render update? How to use code to solve this problem?

Hy,

load up your method and I will have a look on…

Regards
Feature

Hi, this is my method, please have a look.

Color color = new VisualComponents.Create3D.Color((byte)42, (byte)73, (byte)114, (byte)255);
IMaterial material = _app.CreateMaterial(“conveyor_belt_8”);
material.Color = color;
material.Ambient = color;
material.Specular = color;
material.Emissive = color;
material.ForceUpdate();
ISimNode sn = newcomponent.RootNode;
sn.MaterialInheritance = MaterialInheritance.ForceInheritNode;
sn.Material = material;
sn.Material.ForceUpdate();
sn.Rebuild();
IoC.Get().RequestRender();

Hy,

you did not what in this thread is recommendet! → Get some material from the library example “red”! → And copy everything to your customMaterial without the color then it should work…

Regards
Feature