[solved!!]onMouseMove show component instance
-
Hello, perhaps this next question is hoping for too much, but I was wondering if I can have it so the model instance follows the mouse around before it is placed down.
i.e. just like when you add a model from Google 3D warehouse
I am inserting prepackaged component definitions that the user is asked to place over the map (ie trees onto the basemap) and I am using a tool which takes the insert location from a click, however the model will only appear after the click is made. I just want it to follow around the mouse first so the user can get a sense of the scale.
is this possible? From what I have found I am leaning towards no....but I figured I would throw the question out here before I wrote it off.
Cheers
-korbin -
Use @ variables so they pass across methods.
Add the component-definition's instance at the ORIGIN [instance=entities.add_instance(definition, Geom::Transformation.new)
].
In the Tool'sonMouseMove()
method set a point to the cursor's input_point.position, set a transformation to that point andinstance.transform!(transformation)
.
In the Tool'sonLeftButtonDown()
method set the point to the current cursor's input_point.position, set a transformation to that point and instance.transform!(transformation),
Stop theonMouseMove()
code using a @state variable and 'case' tests... -
@tig said:
Use @ variables so they pass across methods.
Add the component-definition's instance at the ORIGIN [instance=entities.add_instance(definition, Geom::Transformation.new)
].
In the Tool'sonMouseMove()
method set a point to the cursor's input_point.position, set a transformation to that point andinstance.transform!(transformation)
.
In the Tool'sonLeftButtonDown()
method set the point to the current cursor's input_point.position, set a transformation to that point and instance.transform!(transformation),
Stop theonMouseMove()
code using a @state variable and 'case' tests...Thanks TIG, I admit I'm not 100% sure how to implement what you said. From what I gathered I got this:
def onMouseMove flags, x, y, view if @clicked == false @pt1.pick view, x, y newpt = Geom;;Transformation.new( @pt1.position ) @insert_tree_1.transform!( newpt ) end end def onLButtonDown flags, x, y, view @pt1.pick view, x, y newpt = Geom;;Transformation.new( @pt1.position ) @insert_tree_1.transform!( newpt ) @clicked = true reset
I start the the instance at the origin like you said (i assume that because the transformation.new moves from the origin if only a point is given?)
but when I move my mouse the tree is waaay back in the distance, not where my mouse is. Is this an issue with the pick?thanks for the help~
korbin
-
I just suggested adding the instance at the ORIGIN as it was simple - you could add it onto the current cursor position.
Your code is missing some important parts... like how the instance is added and referred to, and the closing 'end' to the 'onLButtonDown' method...
The .transform! method should work...
There is also a 'translation' transformation where you find the current instance.position and the vector to the current cursor.position and slide the instance along it BUT it should work as well as the 'transform!......... -
@tig said:
I just suggested adding the instance at the ORIGIN as it was simple - you could add it onto the current cursor position.
Your code is missing some important parts... like how the instance is added and referred to, and the closing 'end' to the 'onLButtonDown' method...
The .transform! method should work...
There is also a 'translation' transformation where you find the current instance.position and the vector to the current cursor.position and slide the instance along it BUT it should work as well as the 'transform!.........I didn't post all the code sorry I thought it would be simpler if I just put those methods in.
The things you referred to are in the code. the tree appears at the origin, then as I move my mouse it gets transformed but in some other direction, and farther and farther away quite rapidly...
This is what I have been using to test it with:
(I know I might be using class variables when I could be using instance...but that shouldn't be the problem)class AddTree1 @@model = Sketchup.active_model @@ents = @@model.entities # Initialize the input point upon start def activate dl = @@model.definitions file_path = Sketchup.find_support_file "KorbinTest/components/Tree_2D_Schematic_Sparse_12_.skp", "Plugins" @pt1 = Sketchup;;InputPoint.new @@comp_def_tree1 = dl.load file_path @insert_tree_1 = @@ents.add_instance( @@comp_def_tree1, Geom;;Transformation.new) @clicked = false end def onMouseMove flags, x, y, view if @clicked == false @pt1.pick view, x, y @newpt = Geom;;Transformation.new( @pt1.position ) @insert_tree_1.transform!( @newpt ) end end def onLButtonDown flags, x, y, view @pt1.pick view, x, y @newpt = Geom;;Transformation.new( @pt1.position ) @insert_tree_1.transform!( @newpt ) @clicked = true reset end def reset @pt1.clear end def onCancel flags, view reset end end Sketchup.active_model.select_tool AddTree1.new
-
hmmm i have been testing away and:
(this is just the one relevant methods)
def onLButtonDown flags, x, y, view @pt1.pick view, x, y newpt = Geom;;Transformation.new( @pt1.position ) puts newpt.origin @insert_tree_1.transform!( newpt ) reset end
(reset is just @pt1.clear)
So from my testing I am pretty sure the pick is working because when I just had a new tree put down for every new click it went where it was supposed to. So I started testing the above code, so i could keep better track of what was going on then with the onMouseMove (when it was in onMouseMove it spazzed off really fast is some direction). From the above code I could tell that the first click I do (my tree starts at the origin) it successfully translates to the proper position, however each subsequent click something weird goes on.
I clicked on the red axis, the tree went to the right spot, I then click on approximately the same distance down the green axis and the tree translated to from what I could tell the intersect of going straight out from the point on the red and the green (my math fails me off the top of my, inst that the the sum of the two points?)...some sort of math went on..
I tried adding
@insert_tree_1.transform!( [0,0,0] )
right before the translation in hopes that it would reset the tree position but I got the same result. So i am lead to believe its a problem with my @pt1? is it perhaps not getting reset? (i use .clear after each click)......
This is as far as my investigating could get me
If anyone can shed any light for me it would be appreciated.
*edit
Im pretty sure now that it is to do with the my instance variable and the translation.....from what I can tell it is adding its current location before it translates itself...thus why the first translation works because its adding 0,0,0. ....could I subtract the current location from the transformation to cancel this out?? I think I'm just using the method wrong...Cheers,
Korbin -
The code should move the instance with the cursor until you click then it stops.
After that your code needs to add a new instance and start again.
In initialize() set @instance=nil.
When you add an instance set it to be @instance [within activate].
In the mouse-move method add a test at the start like 'return nil if not @instance' - so nothing is done unless @instance is set...
BUT when you click to place it you run reset which sets @instance=nil again, so it is then fixed where you clicked, reset also runs 'activate' again...
Repeat adding another instance etc etc... -
@tig said:
The code should move the instance with the cursor until you click then it stops.
Thank you for continuing to help, and I did implement what you suggested however I still have my main problem which is that, although the code does move the instance with my cursor (or with my clicks in the more controlled testing) it does not move to where the cursor is.....however the transform! method is working it is not simply moving the instance to my cursor...it is moving it in some other weird direction, which I beleive I found out somewhat why in my previous post...however I don't know how to make it not do that
cheers,
Korbin -
@kdasilva said:
@tig said:
The code should move the instance with the cursor until you click then it stops.
Thank you for continuing to help, and I did implement what you suggested however I still have my main problem which is that, although the code does move the instance with my cursor (or with my clicks in the more controlled testing) it does not move to where the cursor is.....however the transform! method is working it is not simply moving the instance to my cursor...it is moving it in some other weird direction, which I beleive I found out somewhat why in my previous post...however I don't know how to make it not do that
cheers,
Korbin
For example - assuming the component-instance is added and referenced as @instancedef onMouseMove(flags, x, y, view) return nil if not @instance ### only works when instance is set @ip.pick(view, x, y) if @ip != @ip1 view.invalidate if( @ip.display? or @ip1.display? ) @ip1.copy!(@ip) view.tooltip=@ip1.tooltip @instance.transform!(Geom;;Transformation.new(@ip1.position)) end end
Later
def onLButtonDown(flags, x, y, view) return nil if not @instance @ip1.pick(view, x, y) if @ip1.valid? @instance.transform!(Geom;;Transformation.new(@ip1.position)) @instance=nil ### un-sets instance end end
Then reset @instance to another new instance...
-
@tig said:
@kdasilva said:
@tig said:
The code should move the instance with the cursor until you click then it stops.
Thank you for continuing to help, and I did implement what you suggested however I still have my main problem which is that, although the code does move the instance with my cursor (or with my clicks in the more controlled testing) it does not move to where the cursor is.....however the transform! method is working it is not simply moving the instance to my cursor...it is moving it in some other weird direction, which I beleive I found out somewhat why in my previous post...however I don't know how to make it not do that
cheers,
Korbin
For example - assuming the component-instance is added and referenced as @instance> def onMouseMove(flags, x, y, view) > return nil if not @instance ### only works when instance is set > @ip.pick(view, x, y) > if @ip != @ip1 > view.invalidate if( @ip.display? or @ip1.display? ) > @ip1.copy!(@ip) > view.tooltip=@ip1.tooltip > @instance.transform!(Geom;;Transformation.new(@ip1.position)) > end > end >
Later
> def onLButtonDown(flags, x, y, view) > return nil if not @instance > @ip1.pick(view, x, y) > if @ip1.valid? > @instance.transform!(Geom;;Transformation.new(@ip1.position)) > @instance=nil ### un-sets instance > end > end
Then reset @instance to another new instance...
wow so i was definitely missing a few things...alas put your code in directly, changed the necessary variable names, ...but same result
When I creep my mouse onto the screen, it will move to my mouse for the very first move...but any subsequent move sends the instance off rapidly in a another direction...is this just a problem for me??
-
Do you get any errors in the Ruby Console ?
-
no totally error free BUT..i just made it work by.....(drumroll)... changing .transform!() to .move!()
I don't know why it worked...i just did it on a whim, but presto!
thanks for all the help, this was driving me crazy...so much thesis to do, but I tend to obsess on things I cant solve.
cheers
Korbin -
@kdasilva said:
no totally error free BUT..i just made it work by.....(drumroll)... changing .transform!() to .move!()
I don't know why it worked...i just did it on a whim, but presto!
thanks for all the help, this was driving me crazy...so much thesis to do, but I tend to obsess on things I cant solve.
cheers
KorbinI was just going to suggest .move! instead of .transform! as it's 'outside of the undo stack'... [honest!]
Glad it's working at last... -
(I'm not sure that there is a question in this - its more of me just talking out loud).
I'm trying to do the same, and I'm getting the problem that once I attache the component to the cursor, then the ip point easily grabs onto the component when the mouse moves. And sine the component is closer to the camera, than the center of the component that is attached tot he cursor, then the ip jumps to the closer position, moving the whole component closer. And repeats. So every time I start move the mouse,t he component gets closer and closer to the camera until it clips out of view, and the mouse can reach geometry in the model to reset the ip back to the model.
I have not thought much about this yet, but I was wondering how you guys got it to work? It just doesn't seem to do the trick for me. I was really hoping it would be easy to place a component in the model, and have the component attach to the cursor.
Its not the end of the world if it turns out to not work, I don't truly need to show the component.
-
Do you need a custom tool? Can you not use model.place_component ?
-
I think I do - I need to know what edge in what group it was placed on so I can break the edge and insert new geometry. I can do all that easy enough if I do it with my own tool. But I'm scared of relying on too many observers. I've had bad luck with them already, and I know you (thom) have had many projects ruined by them.
So I think I need my own tool. Its working ok though, I'm actually drawing lines to the viewport that represent the component I'm placing. I can attach that to the mouse easy enough, and it looks pretty cool.
-
Hmmm... would have been nice if the method returned a ref to the newly placed instance (instead of the model object itself*****.) Then you might have been able to easily get the instances origin point and go from there.
Just weird that a ref to the placed instance is the obvious preferred return for a method like this. Sometimes I just do not understand how the obvious can be so often overlooked.
***** The API doc is in error when it says it returns
nil
. -
Ok.. you could call:
lastdefn = defn.instances.last
after usingmodel.place_component( defn, false )
Wait... weirdness, the method returns immediately, BEFORE the instance is actually placed. (It spits out a redraw time to
$stdout
after the instance is placed.) The move component tool does not exit with afalse
2nd arg, but remains in the move tool, and the user could inadvertently move the instance again.
It would be nice if this method took a block to be run after the instance was placed. -
Agree 100% with you Dan.
-
I think eventhough the title has "Solved" the last solution is missing
Here is one of them.
model = Sketchup.active_model ent = model.entities sel = model.selection defn = Sketchup.active_model.definitions def onMouseMove(flags, x, y, view) return nil if not @instance ### only works when instance is set @ip.pick(view, x, y) if @ip != @ip1 view.invalidate if( @ip.display? or @ip1.display? ) @ip1.copy!(@ip) view.tooltip=@ip1.tooltip @instance.transform!(Geom;;Transformation.new(@ip1.position)) end end def onLButtonDown(flags, x, y, view) return nil if not @instance @ip1.pick(view, x, y) if @ip1.valid? @instance.transform!(Geom;;Transformation.new(@ip1.position)) @instance=nil ### un-sets instance end end lastdefn = defn[0] # ! I had just one definition in my component definition list therefor defn[0] worked well for me model.place_component( lastdefn , false )
Advertisement