Change the material of vcTriangleSet object by python script

Hi,

I try to change the material of vcTriangleSet object by python script,as the following code shown:

 

from vcScript import *
import vcGeometrySet

app = getApplication()
comp = getComponent()

def OnSignal( signal ):
pass

def OnRun():
glassMaterial = app.findMaterial(‘frosted_glass’)
print glassMaterial

carFeature = comp.findFeature(‘Fixed-Car_JIAOCHE_1’)
print carFeature
carGeometry = carFeature.Geometry
print carGeometry
carGeometry.GeometrySets[0].Material = glassMaterial
pass

 

but the material of geometrySet not change.

Someone who know the reason? Thanks for your help.

A few things.

  • Changing the material would require a render.
  • Material of geometry set might be forced to inherit its node's material.
  • When you are assigning the material, you are getting two inline references. That may or may not work, so consider getting the geo set first in a variable, and then assigning it a material.
 

Thanks for you reply zesty.

I added a line of code and the material changed, as the following code shown:

from vcScript import *
import vcGeometrySet

app = getApplication()
comp = getComponent()

def OnSignal( signal ):
pass

def OnRun():
glassMaterial = app.findMaterial(‘frosted_glass’)
print glassMaterial

carFeature = comp.findFeature(‘Fixed-Car_JIAOCHE_1’)
print carFeature
carGeometry = carFeature.Geometry
print carGeometry
carGeometry.GeometrySets[0].Material = glassMaterial

comp.rebuild()
pass

 

but when material are changing ,the 3Dview takes longer to have an effect.

In addition, i am not understand the third you said very well, could you tell me how to do this?

instead of comp.rebuild(), try using app.render()

third point refers to known issue in scripts. in your case, everything thing is fine, so you do not need make a change. sometimes when do something like this vcComponent.getProperty(“Example”).OnChanged = test

it might not work, so you first need to get the property in one variable, i.e. its own line in script, and then assign the event handler.

Got it.

Thank you very much zesty.

Hi,

I tried to change material by same way, but I was not successful, even I am not able to change material at Block feature - (Python script in component which is in file in attachment). Interesting to know what I am doing wrong…

 

app = getApplication()
comp = getComponent()

yellow_material = app.findMaterial(“Yellow”)

block = comp.findFeature(‘Block’)
block.Material = yellow_material
app.render()

 

material_1.vcmx (141 KB)

Hi @stransky,

Try adding comp.rebuild() before app.render().

-k

Hi Keke,

thank you for quick response. I try to add comp.rebuild, but any result (layout attached). I noticed, that when I closed my layout and open it in Visual Components again , then material is changed. But I would like to change it during run of simulation.

material.vcmx (148 KB)

Hi,

You seem to be missing parenthesis in comp.rebuild(). So add those and it should work. It works at least for me.

Yeah when you open model again it is rebuilt and that’s why you see the material change then.

-k

YES, it is works . Thank you very much Keke.