ScriptSpot

Forum topic · Scripts Wanted

Center pivot with round number

By Haider of Sweden · 2017-11-19

Description

Hi!

I would like to ask for help to alter a script that I use to center pivot and move it to the bottom of the object.


if selection.count != 0 then 
selection.pivot = [selection.center.x, selection.center.y, selection.min.z]
else messagebox "Pick at least one object!" title:"Warning" beep:false

Before going further, I would like to address a flaw in this script - It won't work on multiple selected items, so if someone can fix that, I would be very thankful.

Now, to the request, if I may :)
I would like to do the pivot-align, but with a clean and rounded value, to the closest 1mm, 1cm, 1 dm or 1m.

Thanks!

Comments (11)

.

Haider of Sweden · 2017-11-23

And yet another question is how to gain more control over the rounded value, ie if I have a value like 113,15 cm and I want:

round to closet 1mm: 113,2
round to closest 1cm: 113
round to closet 1 dm: 110
round to closest m: 100

Also, can we have the script work independent from the system-units like if I tell it to round up to closest 1 dm, I can have the system units as 1 mm, 1 cm or 1 m and it would still work?

.

Haider of Sweden · 2017-11-20

Thank you @Vusta, you fixed that issue.

@Jahman, thanks for your code as well. I can see that it does round up the object coordinates, but I cannot understand the logic of those values.
Could you please explain to me the "+ 0.5" part?

.

jahman · 2017-11-20

it is for down/up value rounding
to make it clear
assume we have 1.23 as our value
int 1.23 returns 1.0
int (1.23 + 0.5) will still give us 1.0

now if we have 1.51
int 1.51 returns 1.0
int (1.51 + 0.5) will give us 2.0 since 1.51 + 0.5 = 2.01

barigazy · 2017-11-23

Or you can use "floor" or "ceil" functions

ceil 1.5 --returns 2.0
floor 1.5 --returns 1.0

.

jahman · 2017-11-23

sure, but how do you know which one to use?

barigazy · 2017-11-25

Here are some examples. p.s. "dp" argument is short for decimal places

#1


--round point3 value
fn round p3 dp =
(
	local tempArr = #(p3.x,p3.y,p3.z)
	local mult = 10.00^(dp as float)
	local rp3Arr = for c in tempArr collect ((floor ((c * mult)+0.5)) / mult)
	point3 rp3Arr[1] rp3Arr[2] rp3Arr[3]
)
round [111.1589,12.856,85.5896] 2
-->[111.16,12.86,85.59]

#2


-- round and convert
fn round val dp type =
(
	local result = formattedPrint val format:("0." + (dp as string) + "f")
	if not iskindof type string (result as type) 
)
round 1.919373023 3 float
-->1.919

#3


-- round in units
fn round val dp = (local result = units.formatValue ((formattedPrint val format:("0." + (dp as string) + "f"))as float))	
round 1.919373023 3
-->"1.919cm"

#4


fn round a = if abs(floor(a) - a)>.5 then ceil(a) else floor(a)
round 5.499 --> 5.0
round 5.599 --> 6.0

.

Haider of Sweden · 2017-11-27

Thank you Barigazy

What if I want to round 111.1589 to 110 or to 100?
The idea is to have a script that places the pivot to a mm, dm, cm or m-grid, or any other given target, eg closest 5cm no matter what the system units are. In other words, you might work in 1m system units, but want to center pivots to the nearest cm.

barigazy · 2017-11-25

@jahman
By using #4 example is the answer

.

jahman · 2017-11-25

I mean why bother to know which one to use at all?
It's almost three times as quick


Time: 0.212sec. Mem: 1697944L
Time: 0.641sec. Mem: 2695776L


(
	
	fn roundQuick a = int (a + 0.5)
	fn round a = if abs(floor(a) - a)>.5 then ceil(a) else floor(a)
		
	gc()
	t1=timestamp()
	hf = heapfree
	
		for i=1 to 1e5 do roundQuick (random 1.0 999.0)
	
	format "Time: %sec. Mem: %\n" ((timestamp()-t1)/1000 as float) (hf-heapfree)
	
	
	gc()
	t1=timestamp()
	hf = heapfree
	
		for i=1 to 1e5 do round (random 1.0 999.0)
	
	format "Time: %sec. Mem: %\n" ((timestamp()-t1)/1000 as float) (hf-heapfree)
	
	
)

quickie...

vusta · 2017-11-19

if selection.count != 0 then
    for o in selection do o.pivot = [o.center.x, o.center.y, o.min.z]        
else messagebox "Pick at least one object!" title:"Warning" beep:false

dunno about 'round number'...

.

jahman · 2017-11-20

for s in selection do s.pivot =  [ int (s.center.x + 0.5), int (s.center.y + 0.5), int (s.min.z + 0.5) ]