sketchucation logo sketchucation
    • Login
    šŸ›£ļø Road Profile Builder | Generate roads, curbs and pavements easily Download

    Edit materials from code

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 4 Posters 206 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      Access the material object, via the Materials collection.

      Then change it's properties. (See the API dictionary for examples, on both.)

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        I just tried changing the display name for a new file, opened with the Susan component.

        Her trousers have material 'jean blue'

        At the console:

        mats = Sketchup.active_model.materials
        #>> #<Sketchup;;Materials;0x656d2f8>
        m = mats['jean blue']
        #>> #<Sketchup;;Material;0x6525214>
        m.name.inspect
        #>> "jean blue"
        m.display_name.inspect
        #>> "jean blue"
        m.name="bluejeans"
        #>> "bluejeans"
        m.display_name.inspect
        #>> "bluejeans"
        
        

        And I visually see in the Materials Inspector that the display_name has indeed changed.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • D Offline
          dacastror
          last edited by

          thank you very much, functioned to change the display name šŸ˜„

          I looked at the API dictionary do not understand,
          could you give me an example?
          such as changing the color of the material 'blue jean' to red

          1 Reply Last reply Reply Quote 0
          • D Offline
            dacastror
            last edited by

            I have understood

            mats = Sketchup.active_model.materials m = mats['jean blue'] m.color=[180, 0, 0]

            I am new to this,that good there are good people willing to help, thank you very much Dan

            (I know very little English)

            1 Reply Last reply Reply Quote 0
            • D Offline
              dacastror
              last edited by

              I have another related question,

              if I have a cube, each side painted a different color and then create a group or component, how I can access the list of materials the component or group to modify these materials from the code?

              Would greatly appreciate a simple example of this, I could not find him anywhere

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                Any group [or a component-instance] can themselves have a 'material' - e.g. group.material if it's ' nil' then it has no material, however if it has a material any faces within the group that 'have no material' will display with that group's material [although they still have ' nil' material individually, they will display with the group's material].
                The faces [and edges for that matter] inside a group.entities can have materials [ face.material] - or ' nil' if none.
                Therefore, to find the materials used inside a group [or component definition found from its instance], make a collection of the group.entities faces and then iterate through those faces and collect each face.material, use .uniq! on that array, so you have only one of each; then iterate that array of materials and make a list of their 'names' etc etc...
                šŸ¤“

                TIG

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  Note: Material.name is the internal ID name - while Material.diplay_name is what SU display in the material browser (exposes to the UI).
                  For instance, you cannot use Material.display_name as a key in the Model.materials collection - it will fail in some circumstances (for instance when the name is wrapped in [])

                  Read more: http://www.thomthom.net/thoughts/2012/03/the-secrets-of-sketchups-materials/

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    Diego... see: Ruby Newbie's Guide to Getting Started

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • D Offline
                      dacastror
                      last edited by

                      wow, is more complicated than I expected, I doubt that achieves do with my current knowledge of the Ruby API, I guess I'll have to wait a while thank you very much Dan, Tig and Thomthom for guiding me

                      šŸ‘

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        dacastror
                        last edited by

                        this is what I have achieved so far

                        entidad = %(#FF8000)[Sketchup]%(#0040BF)[.active_model.selection][0] curretmaterial = entidad%(#0040BF)[.material] nameM = curretmaterial%(#0040BF)[.name] curretmaterial%(#0040BF)[.color]=[102, 0, 51] curretmaterial%(#0040BF)[.name]=%(#BF8080)["nuevo tono"]

                        I shall be able then apply the recommendations

                        (sorry for bad English)

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement