[Closed] round float value?
Hey,
I’m currently trying to make a script which rotates selected uv elements untill the vertices of one edge share the same x or y value. (see attached image) My problem is the vertex’s position value is like 0.1234567, to make them fit I have to rotate by 0.0000001…which takes slightly longer than brute force hacking the FBI.
I’ve checked the max script reference for float values, but couldn’t find anything helpful.
Can you post some code? If you’re doing what i think you’re doing, it sounds as if you’re rotating a tiny increment, checking if the x and y are the same and then rotating some more… this is a highly inefficient way of doing this (and as you’ve found, it won’t work unless the increment you use is arbitrarily small). You’re better off pulling out a little trigonometry and simply calculating the proper rotation based on the starting x and y values. At least that’s if I’m reading your intensions right.
Thanks for the input James. Well yes your approach seems to make more sense. I’ve been using 3dsMax only as a user for now and never got that much into scripting, so it’s kinda hard to find the right way to do such things as a newb. Furthermore I’ve never worked with vectors yet…which leads me to the script itself…which actually does something, but the rotation ain’t close to anything…guess a random value would do the same.
$.unwrap_uvw.getSelectedPolygons()
$.unwrap_uvw.FaceToVertSelect()
-- $.unwrap_uvw.setTVSubObjectMode 1
a_vertList = ($.unwrap_uvw.getSelectedVertices() as array)
a_xPos = #()
a_yPos = #()
-- // collect vert positions
for i = 1 to a_vertList.count do
(
a_xPos[i] = ($.unwrap_uvw.getVertexPosition 1 a_vertList[i])[1]
a_yPos[i] = ($.unwrap_uvw.getVertexPosition 1 a_vertList[i])[2]
)
v_edge1 = ([amax a_xPos, amax a_yPos, 0] - [amin a_xPos, amin a_yPos, 0])
v_length = length ([amax a_xPos, amax a_yPos, 0] - [amin a_xPos, amin a_yPos, 0])
v_edge2 = ([(amin a_xPos + v_length), amin a_yPos, 0] - [amin a_xPos, amin a_yPos, 0])
f_rotAngle = dot v_edge1 v_edge2
$.unwrap_uvw.rotateSelectedVerticesCenter f_rotAngle
Edit: I figured out “v_edge1” kinda missed it’s target…but I’m not sure how to get the right values. The picture shows what I think I did wrong about v_edge1.
Hi,
Try this:
(
local sel = selection[1].modifiers[1]
local verts = #(11, 12)
local v1 = [1,0,0]
local v2 = normalize ((sel.GetVertexPosition 0 verts[2]) – (sel.GetVertexPosition 0 verts[1]))
local d = dot v1 v2
local angle = DegToRad (acos d)
if d < 0 do angle *= -1
sel.RotateSelectedVerticesCenter angle
)
The order of vertices is not important.
Light
Thanks for the code Light!
It works fine for clockwise rotated faces, but takes 2-5 steps for counter clockwise rotated faces…or if the face rotation -> 90°. I’ve checked the values for d and angle but can’t figure out why/when it works correctly and when not.
a_vertList = ($.unwrap_uvw.getSelectedVertices() as array)
v_vec1 = [1, 0 , 0]
v_vec2 = normalize (($.unwrap_uvw.GetVertexPosition 0 a_vertList[2]) - ($.unwrap_uvw.GetVertexPosition 0 a_vertList[1]))
f_dotAngle = dot v_vec1 v_vec2
f_rotAngle = DegToRad (acos f_dotAngle)
if f_dotAngle < 0 do f_rotAngle *= -1
$.unwrap_uvw.rotateSelectedVerticesCenter f_rotAngle