Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Script Snap to grid

pmax
Registered User
Quote
2014-01-10 09:03:06

searching the net I found a few examples and I tried to create a script.
var s = editorGetSelectedSceneNode();
var position = ccbGetSceneNodeProperty(s, "Position");


function snap(value, gridSize, roundFunction) {
if (roundFunction === undefined) roundFunction = Math.round;
return gridSize * roundFunction(value / gridSize);

}

var moveobjX = snap(position.x,5);
var moveobjY = snap(position.y,5);
var moveobjZ = snap(position.z,5);

ccbSetSceneNodeProperty(s,"Position",moveobjX,moveobjY,moveobjZ);
ccbUpdateSceneNodeBoundingBox(s);
editorUpdateAllWindows();



Select an object and run the script.

PS: Translation by Google


edkeyte
Registered User
Quote
2014-01-10 10:13:54

Thanks very much!

Does this work? I tried to save this as a plugin but didn't work for me.

Thanks anyway. I will look through the code and learn from it. Thank you!

Ed.


pmax
Registered User
Quote
2014-01-10 10:33:07

to me via plugin does not work ,but if you do leave the dialog script works.

View menu -> Show Windows scripting.


PS: translation by Google


edkeyte
Registered User
Quote
2014-01-10 16:30:34

I tried it in the Windows scripting and It works fantastic!! Thanks.

But I wonder why this dosn't work as a plugin:

function snap(value, gridSize, roundFunction){
if (roundFunction === undefined) roundFunction = Math.round;
return gridSize * roundFunction(value / gridSize);
}
var s = editorGetSelectedSceneNode();
var position = ccbGetSceneNodeProperty(s, "Position");

var moveobjX = snap(position.x,5);
var moveobjY = snap(position.y,5);
var moveobjZ = snap(position.z,5);

ccbSetSceneNodeProperty(s,"Position",moveobjX,moveobjY,moveobjZ);
ccbUpdateSceneNodeBoundingBox(s);
editorUpdateAllWindows();
editorRegisterMenuEntry("snap()", "Snap to grid\tCtrl+M");



niko
Moderator
Quote
2014-01-10 22:26:33

Of course it works as plugin as well, you only forgot to wrap your code up into a function to be executed by the menu. Paste this into a file named for example 'snap.js', put it into the plugin directory and restart coppercube, and then it works:

function snap(value, gridSize, roundFunction){
if (roundFunction === undefined) roundFunction = Math.round;
return gridSize * roundFunction(value / gridSize);
}

function doSnapSceneNode()
{
var s = editorGetSelectedSceneNode();
var position = ccbGetSceneNodeProperty(s, "Position");

var moveobjX = snap(position.x,5);
var moveobjY = snap(position.y,5);
var moveobjZ = snap(position.z,5);

ccbSetSceneNodeProperty(s,"Position",moveobjX,moveobjY,moveobjZ);
ccbUpdateSceneNodeBoundingBox(s);
editorUpdateAllWindows();
}

editorRegisterMenuEntry("doSnapSceneNode()", "Snap to grid\tCtrl+M");



techno-valley
Registered User
Quote
2014-01-10 22:51:31

Thanks pmax for this code !!

CopperCube has endless features.

Is there any plan in the future to add the plugins to the context menu (Right Click Menu) ? This would be fantastic


edkeyte
Registered User
Quote
2014-01-11 01:39:07

Thanks niko,

I need to keep learning. :)

Ed.


pmax
Registered User
Quote
2014-01-11 11:16:52

Thanks niko, for improving the script.


pmax
Registered User
Quote
2014-01-11 11:44:15

I added a command to enter the value you want.
So there is no need to change every time the script.
I left the default value of 5.

function snap(value, gridSize, roundFunction){
if (roundFunction === undefined) roundFunction = Math.round;
return gridSize * roundFunction(value / gridSize);
}

function doSnapSceneNode()
{
var s = editorGetSelectedSceneNode();
var position = ccbGetSceneNodeProperty(s, "Position");
var grid = prompt("insert value GridSize",5);

var moveobjX = snap(position.x,grid);
var moveobjY = snap(position.y,grid);
var moveobjZ = snap(position.z,grid);

ccbSetSceneNodeProperty(s,"Position",moveobjX,moveobjY,moveobjZ);
ccbUpdateSceneNodeBoundingBox(s);
editorUpdateAllWindows();
}

editorRegisterMenuEntry("doSnapSceneNode()", "Snap to grid\tCtrl+M");



misterpaint
Registered User
Quote
2014-01-11 13:59:20

Fantastic! thanks to all!!! this group of people is great! ^_^


misterpaint
Registered User
Quote
2014-01-12 11:40:02

And the script SNAP TO OBJECT? :D is possible?


BMSKiwi
Guest
Quote
2014-01-13 23:27:53

Hey Niko. Can you add this script to the plugins download page?


niko
Moderator
Quote
2014-01-14 06:07:56

Yes, just added it: http://www.ambiera.com/coppercub...
Hope this is ok for you, pmax :)

I also modified the script a bit in order to now have two commands: One, with the shortcut to snap an object to the grid, and a second command to specify the grid size (because you usually don't want to do this all the time when snapping an object)

Snip to object: Yes, would be possible too. You would have to calculate the bounding box of both objects and then move one. Not sure how to mark both objects which are relevant, probably by selecting them one after another and hitting a different shortcut each, maybe.


edkeyte
Registered User
Quote
2014-01-14 11:30:04

Thanks pmax for the great plugin! It is very useful indeed!

Ed.


pmax
Registered User
Quote
2014-01-14 15:25:38

Niko Thanks for putting me as the author of the plug-in and have it improved.
Thanks to all for the compliments.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "?nternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact