Change the material of vcTriangleSet object by python script

Forum Forums Visual Components 4.1 Python Programming in VC Change the material of vcTriangleSet object by python script

This topic contains 9 replies, has 4 voices, and was last updated by Avatar stransky 1 month ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #8625
    Avatar
    BaiYang
    Participant

    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.

    #8637
    zesty
    zesty
    Moderator

    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.

     

    #8681
    Avatar
    BaiYang
    Participant

    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?

    #8683
    zesty
    zesty
    Moderator

    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.

    #8724
    Avatar
    BaiYang
    Participant

    Got it.

    Thank you very much zesty.

    #16285
    Avatar
    stransky
    Participant

    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()

     

    Attachments:
    You must be logged in to view attached files.
    #16298
    Keke
    Keke
    Moderator

    Hi @stransky,

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

    -k

    #16305
    Avatar
    stransky
    Participant

    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.

    Attachments:
    You must be logged in to view attached files.
    #16310
    Keke
    Keke
    Moderator

    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

    #16312
    Avatar
    stransky
    Participant

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

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.