How do you make plug-ins for sketchup
-
@unknownuser said:
.also, how do you add global variables throughout the script? Is it still setVar("foo",1), getVar("foo"), and getSetVar("foo",0)?
These are not part of the Ruby API - they must be part of the SketchyPhysics API (to confuse things even further.)
-
Just looked it up in that pine-something-or-other tutorial. So it's if you put a @ in front of a variable that makes it global?
-
I've been taking a look at the sketchup API, and am currently a bit intimidated.
I never realized how many different methods, classes and modules there would be! Up until now, everything I've done has been very simple, but I don't even understand half of what these things mean, and it would take me forever to find anything specific!
...also, how do you add global variables throughout the script? Is it still setVar("foo",1), getVar("foo"), and getSetVar("foo",0), like in sketchyphysics? -
SketchUp has (at least)
variable
- local variable
@variable
- Instance variable
@@variable
- Class Variable
$variable
- Global Variablelocal variables are available only within the method in which they are defined.
Instance Variables are available through the entire class instance.
Class Variables are available for all instances of a class.
Global variables are available for all classes. They can be accessed from any ruby script. If you make one called $my_special_global_variable and someone else makes one with the same name, there will be a conflict.I know that doesn't explain instance and class vairables very well. Sorry, I'll have a go at it.
In SketchUp, when you make a tool, you make it as a class. So every time the tool gets activated, it creates a new instance of the class. Instance variables are only available within that instance of the class. When the tool is activated again, those old instance variables are not available to the new instance. But Class variables would be available from one activation of the tool (instance of the class) to the next. So lets say that you have a tool that draws a circle and the user can input the size and segments of the circle. If you used an instance variable to hold the user input size and segment count, then the tool keep their values only while it was active. Once they de-activate the tool, then re-activate it, it creates a new instance of the tool, and the instnace variables are lost, so it would return back to whatever defaults the script provides. But if you use Class variables, then those variables stay active from one activation of the tool to the next. They only get reset once SketchUp is closed down and restarted.
Thats it to the best of my understanding. I only really understand this within a SketchUp context. So I'm sure there;s more to it than just that. But that is a start on local, instance, class, and global variables.
There are also constants, symbols, and maybe more? THey are types of variables I think.
Chris
-
I've started trying to learn how to use ruby but I'm having a lot of trouble getting input points to work. U'm what would be the simplest way for me to make a simple tool that tells me the coordinates in 3 dimensions of whatever I'm clicking on?
-
That seems to be a popular thing to want to do, but in my mind, its a pretty involved task. In order to get the onlinck function to work, it has to be implemented from within a tool. Have you already got a tool class working correctly?
Chris
-
within a tool? (hmm I need to study more! ) well I was wanting to make a line drawing ruby that involved two clicks like the example file. But from just trying to examine the file I realized that it was quiet a complicated process, much more than just drawing a line using ruby! I wish that google could have made this function a little simpler, seeing as like 80% of the plugins I have used use something like this. I guess I need to start on some other more simple things. I have read a book on O O programing before but things are a little rusty now! (classes, methods... arrgghh! )
-
Ahh, thanks for the advice on variables Chris! %(#808080)[(and thanks for all the other helpful advice and tips you've posted in this descussion )]
-
I've learned some of the sketchup ruby API, and understand why most codes work how they do, but my problem is making a code that works. I know how something works when I see it, but can't write one myself. It's like being able to hear or read, but not speak or write
-
I think some of the "easiest" scripts to start writing are ones that begin by iterating through the model and doing something to the geometry. Here's a few variations of some basic examples. Then tweak them and expand on them to make them more and more involved.
pushpull all faces in the model
pushpull selected faces only
select all faces that have a certain surface area.
select all faces that have a surface area within a specified tolerance.
Delete all lines that are not drawn on an axis.
Delete all faces in the model or all faces from a selection, leaving just edgesAnyhow, thats a small start. Those should all be fairly straightforward.
Chris
-
wow im finally interested in learning ruby it would be awesome to not have to request rubies, just make em urself... and share of course.
-
youve got me tempted now, chris. I'll post my efforts this evening
-
Is Ruby on Rails the same as Ruby?
Thanks,
Jeff -
Rails is like an extension of regular ruby. So Ruby is the main language, and rails is a "ruby gem". Its still the same language, but it adds more features and functionality especially developed for web developers (I think).
Chris
-
How about a "heal" script that joins broken, but co-linear edges?
-
I think all those suggestions can be written in 10 lines of code or less. So they are good compact examples too.
Some of the more efficient programmers might be able to pull them off in one or two lines, but for me 10 or less would be good. It might take more than that too of course.
Chris
EDIT: 10 lines doesn't include any menu system implemenetation. Since I write everything in Jim's WebConsole, there's no need to make a menu for it until I'm ready to save it as a ruby file.
-
So literature to buy if one was to, Ruby or ruby on rails? Sounds like ruby on rails would be the next step or more advanced than ruby?
Thanks,Jeff
-
Rails is not Ruby. Rails is a web application framework written in Ruby. Forget about Rails if you're interested in Ruby for SketchUp.
You can't go wrong with the Pickaxe book. However, I don't believe you need to purchase anything as there is plenty of material online to get quite in-depth.
@click draw said:
So literature to buy if one was to, Ruby or ruby on rails? Sounds like ruby on rails would be the next step or more advanced than ruby?
Thanks,Jeff
-
Gotcha, thank you. Good advise.
Cheers,
Jeff
-
@chris fullmer said:
pushpull all faces in the model
AAHHHHH!!!! It took me HOURS to find the typename function!!!!!!!!!!!!!!!!!!!
...but at least I know nowmodel = Sketchup.active_model entities = model.active_entities # - - - - - - - - - - - - entities.each { |entity| if entity.typename == "Face" entity.pushpull 10 end }
(WHY can't it be easier to find things on the sketchup ruby API list????)
Also, it gives weird, unexpected results sometimes: it seems to pushpull the new faces it makes too
Advertisement