1. Hey guyz. Welcome to the All New Phlatforum!



    Sign Up and take a look around. There are so many awesome new features.

    The Phlatforum is a place we can all hang out and

    have fun sharing our RC adventures!

  2. Dismiss Notice

How to get started with Ruby Scripting

Discussion in 'Other Sketchup related softwares/plugins' started by kyyu, Apr 28, 2010.

  1. kyyu

    kyyu Active Member

    Offline
    Messages:
    1,183
    Trophy Points:
    36
    Location:
    Maryland
    I had someone ask me how to get started. So thought I would post it here also, for all.

    Actually, my understanding has improved alot in regard to skechup/ruby just in these last few days. Someone finally published a book on it and I just read the 1st three chapters, that can be downloaded for free: http://www.autosketchup.com I ordered a copy, yesterday, from Amazon. I found this review, just done yesterday: http://www.cfcl.com/rdm/weblog/archives/001735.html This book just came out this March and not having read the rest, I already highly recommend it to anyone learning sketchup ruby. I know sketchup and have read beginners books on ruby, but it's been very hard to understand the basics of the skechup ruby combo. The main skechup ruby documentation can be found here: http://code.google.com/apis/sketchup/ specifically all the methods =>> http://code.google.com/apis/sketchup/docs/index.html But it reads more like a dictionary. Very confusing.

    Besides studing the above, here is some advice to actually get started writing:

    1st another tutorial recommendation, by Chris Fullmer. This is actually what got me started. His two tutorials at SketchUcation, called "Beginning Ruby" http://www.sketchucation.com/advanced-tutorials-index/ In particular, using the "Web Console" plugin written by Jim Foltz. We are familiar with the "Ruby Console" in sketchup. You can find it at Window/"Ruby Console". You enter one line of code at at time, but the Web Console will execute multiple lines (which is what we call a "script"). The Ruby Console is still useful. I normally will have both of them open. The Ruby Console will display errors and also any output statements in your code ("puts" that is). While you are at Jim's site download the "Ruby Toolbar" also. http://sketchuptips.blogspot.com/p/my-plugins.html Jim wrote the unfold plugin, too. You can send him a donation from that page, if you want to say thanks.

    And Chris Fullmer has a set of video tutorial on youtube: http://www.youtube.com/results?search_q ... type=&aq=f

    You can use the web console to test simple stuff. It's easy to start this way, becasue you can simply select a part in sketchup. Then run a script to test the guts of your plugin. So you don't initially have to worry about writing the plugin itself. This is how I figured out the "Reorder Groups" plugin. As long as you plugin is not a tool, then your mostly done. Tools, monitor the mouse, so you have to actually have a plugin running to test it. The way I test them is the reload them in real time with the "Ruby toolbar". As long as you do a check if it's been loaded before and so a new duplicate menu item isn't added each time. Just look at the example of one of my plugins. The end is where the loading part of the code is. Also, by looking there, you can see exactly where it will be found and used in sketchup. So there are probably 2 types of plugins ( my definition): a simple script (activated by menu or right click context menu or nowhere), the other is a tool. I use "Notepad ++" as editor, because it does all the indents so everything is easy to read. And like I said I edit and save and can generally just reload the plugin in real time with the "Ruby Toolbar". Just as long as there are not major changes, like deleting a method. Because that method will still be there. But making changes to a method is ok. It will just get replace when you reload.

    And if you want a simple book on ruby. Here is a ~80 page pdf book called "The Little book of Ruby".
    http://www.sapphiresteel.com/The-Little-Book-Of-Ruby or the direct link to the pdf http://www.sapphiresteel.com/IMG/pdf/Li ... OfRuby.pdf

    Oh, and the SketchUcation Forum is a great resource: http://forums.sketchucation.com/index.php

    -Kwok
     
  2. cncmachineguy

    cncmachineguy Member

    Offline
    Messages:
    133
    Trophy Points:
    16
    Location:
    Odenton MD
    AWESOME!! Thank you Kwok

    Something new to learn with useful application.

    :D :D :D :D :D :D
     
  3. kyyu

    kyyu Active Member

    Offline
    Messages:
    1,183
    Trophy Points:
    36
    Location:
    Maryland
    You're welcome Bert. If you or anyone have any questions that come up, feel free to post them here. I still feel like a beginner, but I will try to help anyone who wants to learn.

    -Kwok
     
  4. 3DMON

    3DMON Moderator Staff Member

    Offline
    Messages:
    2,380
    Trophy Points:
    38
    Location:
    Sebastian, Florida
    Good stuff Kwok.
    I know I eventually want to get more into Ruby for Sketchup and this looks to be the ticket.
    Now I just need to find more time. :D
     
  5. kram242

    kram242 Administrator Staff Member

    Offline
    Messages:
    6,311
    Trophy Points:
    13
    Location:
    NJ
    Great post Kwok! Thank you for sharing
    Mark and Trish
     
  6. TigerPilot

    TigerPilot Well-Known Member

    Offline
    Messages:
    1,578
    Trophy Points:
    48
    Kwok, your timing is impeccable! Just yesterday I was looking at the pocket tool which has a big bug in it and I saw that I so don't know how to do about fixing it and this morning I see the above. I may use your useful info above to see if I will be able to learn Ruby scripting and fix the bug.

    Thanks, my man.
     
  7. kyyu

    kyyu Active Member

    Offline
    Messages:
    1,183
    Trophy Points:
    36
    Location:
    Maryland
    Go for it TigerPilot. :) My book is supposed to be delivered today. I got it from Amazon for $32 shipped. But if you shop around you can get it for alittle less. But Amazon always has good service.

    Here is an example of how easy and at the same time how hard it is to get started in sketchup/ruby. I lurk around sketchucation sometimes and don't even feel I know enough to ask a question from the masters. So one good thing is, we have the phlatforum; so things aren't so intimidating. Anyways, I was at the sketchucation forum last night and saw this post with no replys. The guy was asking about about rotating a plane and the example he gave was 30 deg and then 50 deg. http://forums.sketchucation.com/viewtop ... 23&t=27874 Well, I had been working on something similar, so wrote him a quick sample script. I developed it straight from the "Web Console" and it can be run right there or loaded from a .rb file. That's the easy part and the fact it has non of the normal extra code associated with a plugin. It's really stripped bare, which is where you would start if writing a plugin and also written this way easy to understand. And the hard part is the actual rotation command. It's stuff like that, that really stumped me when I started and almost impossible for a beginner to figure out from the sketchup ruby api. That's were looking at example comes in and copy/paste strategic bits of code, comes in, until you get more familiar with it. Even here I copy pasted the key bits in. Another thing that make it hard is the math. Notice that even in this simple example, I had to use sin, cos and a vector cross product. If you saw my real plugin, you could not follow it. Heck I couldn't barely follow it, since it had been so long.

    Here it is, to try out, if you don't want to go to skechucation. You can draw a few lines and measure the angle to verify the results, which is what you would do if writing the script yourself. You will see it is 30 deg to the right and 50 deg up: Attached files [​IMG] Rotate_Plane_example.rb (899 B)Â Rotate_Plane_example.skp (9.6 KB)Â
     
  8. kyyu

    kyyu Active Member

    Offline
    Messages:
    1,183
    Trophy Points:
    36
    Location:
    Maryland
  9. TigerPilot

    TigerPilot Well-Known Member

    Offline
    Messages:
    1,578
    Trophy Points:
    48
    There must be some kind of interface between the ruby scripting and SU, beside the web console or the ruby console. Lets say you want to make a plugin that when the user is over a face it changes color and when he is gone form the face, it returns to the default color. Writing it in the web console alone will not let you try it since it goes through the code only once and you don't get a chance to go with the mouse over a face.

    I could start trying some stuff if I'll solve this hurdle. :evil:
     
  10. 3DMON

    3DMON Moderator Staff Member

    Offline
    Messages:
    2,380
    Trophy Points:
    38
    Location:
    Sebastian, Florida
    Good tutorial Kwok.
    Cool plugin too.
     
  11. kyyu

    kyyu Active Member

    Offline
    Messages:
    1,183
    Trophy Points:
    36
    Location:
    Maryland
    Thanks 3DMON. :)

    Tiger Pilot,
    What you want to write is call a "tool", which involves mouse interactions. Like the "circle tool" for example. The mouse stuff is pretty much irrelevant during the initial stage of writing the plugin. For your example, the thing that matters is the core few lines of code. That is if you can get the part to actually change color. That's basically your plugin. Everything else is just a wrapper. At this stage, you can simply draw a face and select it. For example, with my "ReOrder Groups" plugin, the core code was just 2 simple lines. The full plugin was ~60 lines. You have to start somewhere and for that plugin it was developing those 2 lines of code. Here is another video, that should help make thing more clear. It uses a tool, I am working on, as an example: http://www.mediafire.com/file/dzwgziwnmmi/Quick%20look%20at%20how%20to%20create%20a%20sketchup%20tool.wmv

    -Kwok
     
  12. TigerPilot

    TigerPilot Well-Known Member

    Offline
    Messages:
    1,578
    Trophy Points:
    48
    I find what you said to be very complicated. How do I know if the line that I've wrote is doing its job. When I code in Visual Basic, I can run it anytime and see if it does what I want. How do I know that I have the right commands to get the face to change? I have to run it before I know if I made a mistake or not.

    I'll see if I can ask Jim, the guy who wrote the unfold plugin.
     
  13. TigerPilot

    TigerPilot Well-Known Member

    Offline
    Messages:
    1,578
    Trophy Points:
    48
  14. kyyu

    kyyu Active Member

    Offline
    Messages:
    1,183
    Trophy Points:
    36
    Location:
    Maryland
  15. kyyu

    kyyu Active Member

    Offline
    Messages:
    1,183
    Trophy Points:
    36
    Location:
    Maryland
  16. TigerPilot

    TigerPilot Well-Known Member

    Offline
    Messages:
    1,578
    Trophy Points:
    48
    OK, OK, I'm downloading it. Man you can be pushy!! :D :D :D :lol: :lol: :lol:
     
  17. tvcasualty

    tvcasualty New Member

    Offline
    Messages:
    637
    Trophy Points:
    0
    Nice job Kwok! Reading material for later on for sure!
     

Share This Page