Moving a 3D object by coordinate, through Ruby
-
I've posted this on the newbie forum, but since it is partly related to Ruby, I'm trying here as well.
What I have been trying to do for the last while is to read a log File through a Ruby script, through Sketchup of course. I've been successful at doing so, as I am able to see the output in the Ruby Console.
My goal, however is to be able to place a 3D object in Sketchup, and assign it a 3D coordinate from the log file as it is being read, which would translate into the object moving around in space. I haven't much of a clue on how to set this 3D object's coordinate, in order to be able to do what I've mentioned above.
I'll be adding this code to my Ruby script, under a method to generate this visual. Can anyone point me in the right direction? Please tell me this is possible...
Someone linked me to a plugin, but it had a discrete number of possible positions, where in my case the number of coordinates in the log file varies.
Thank you to all you Rubyists!
-
bender,
This is possible. It all depends on what your input data looks like.
Here's an example for reading a text file of comma-separated lines of x, y, z coordinates;
IO.readlines("filename").each do |line| x, y, z = line.split(",") x = x.to_f y = y.yo_f z = z.to_f my_group.translation = Geom;;Transformation.new( [x, y, z] ) end
-
Thanks for the reply, however from the code I basically see how to read a text file using a "," as a delimiter and storing each number into its respective variable, which I know how to do.
From the last line, I'm having some trouble:
my_group.translation = Geom::Transformation.new( [x, y, z] )
I've read on the Geom Class, and a few others, however I fail to see how this is doing or will do what I want. The closest thing I've found is to add an "entitie" and remove (erase_entitie) it, and do this continuously as I read the log file to replicate a sort of "movement."
What's my_group? Maybe if you could explain the last line. Anyhow, is my approach a good one? Or is there a better one. I am a beginner, so most things are still not yet clear to me.
Thanks again,
Bender -
Although you could create and erase the geometry for each position you read in, that's seems inefficient. A Group is just a SketchUp Group - when you select multiple entities, right-click and select Make Group. A Group in SketchUp has a position. You can iterate through your points and just reset the Group's position, instead of creating and erasing the geometry.
That last line should read:
my_group.transformation = Geom;;Transformation.new ( [x, y, z] )
my_group is a variable - a reference to a Group entity.
-
Jim,
Thanks for the follow-up. I've been able to move entities this way, and it works well. However, when I read a sequence of coordinates and attempt to move the object (entitie) at each coordinate, it doesn't do it. So, for example as a tester, I had an object which started at (0,0,0) and I wanted to move it to (10,10,10), then (20,20,20) and so on by incrememnts of ten until (100,100,100), but all it does is move it to (100,100,100). I think it DOES move it in between, you just don't see it...for some reason. Almost like the computer is too fast? -->so I added dummy for loops to "slow" it down, but this didn't seem to solve it. Any ideas on why it is doing this?
I am able to progress thanks to all the help here. Much appreciated. Bender
-
I'm posting some code, might be more helpful:
def generateVisual(cell) puts cell #for testing model = Sketchup.active_model entities = model.active_entities cell2 = cell[cell.index(',')+1..cell.index(')')] num1 =cell[cell.index('(')+1..cell.index(',')-1].to_i num2 = cell2[0..cell2.index(',')-1].to_i num3 = cell2[cell2.index(',')+1..cell2.index(')')-1].to_i #puts num1 #puts num2 #puts num3 #puts " " #in the above 4 lines I've printed the 3 numbers to make sure that #it retrieves it properly from the file, and it does...so this cannot be an issue my_group = entities[0] #the one and only object there point = Geom;;Point3d.new num1,num2,num3 my_group.transformation = Geom;;Transformation.new point end
and basically whenever I read one line from the log file and retrieve a cell, I call this function sending the cell in, breaking it up into it's components and setting the object to that coordinate. However, it runs...and once it's done running, it THEN moves it to the last coordinate. As I've said before, I think it is doing it, but for some reason it does not appear in the visual...almost as though it is processing too fast.
Hopefully I can get some help from you rubyists. Thanks, Bender
-
bender,
Read up on the Animation class and the nextFrame method. Use nextFrame to update the coordinates.
-
Once the nextFrame method is implemented...(here I tried putting in the visual update) is it invoked by Sketchup automatically or does one need to make an explicit call somewhere in the code?
I'd like it to display a movement of the object by coordinate at a time, so I suppose if I consider that to be a frame, I'd define those movements within teh nextFrame method (which I did). But then, how is it called?
I'm still a little stuck on this. It's been sometime since I've programmed in Java as well. Anyway, please let me know if it is on the right track, or if I am doing something wrong.
Thanks,
Bender -
In your Plugins/Examples folder, there is a script named animation.rb. It is a good reference for using the Animation class.
Advertisement