Skip to content

Instantly share code, notes, and snippets.

@JokerMartini
Last active September 18, 2023 09:06
Show Gist options
  • Save JokerMartini/161ab72274c675cfe42a to your computer and use it in GitHub Desktop.
Save JokerMartini/161ab72274c675cfe42a to your computer and use it in GitHub Desktop.
Maxscript: Creates a camera and flips it around an origin. Aligns camera to vector.
-- test scene setup
delete objects
tm = Inverse( viewport.getTM() )
camA = FreeCamera wirecolor:orange transform:tm
tm = camA.transform
origin = [0,0,0]
fn flip_tm_along_vector tm:undefined origin:[0,0,0] =
(
dist = distance tm.row4 origin
targetPos = (origin + (normalize (origin-tm.row4)) * dist )
dirVec = normalize (targetPos - origin) --a target position to origin
upVec = normalize (tm.row2) --the local Y axis
crossVec = cross upVec dirVec --a vector perpendicular to both the Z and the direction vector
newTM = matrix3 crossVec upVec dirVec targetPos --make a matrix of the new X, Y and existing Z, keep the position
)
newTM = flip_tm_along_vector tm:tm origin:origin
cam = FreeCamera transform:newTM wirecolor:green
-- test scene setup
delete objects
fn flip_tm_along_vector tm:undefined origin:[0,0,0] =
(
--dist = distance tm.row4 origin
targetPos = origin - (tm.position - origin) -- -tm.position(origin + (normalize (origin-tm.position)) * dist )
zVec = normalize (targetPos - origin) --a vector pointing from target pos to original pos
yVec = tm.row2 --the local Y axis (the 'up' axis
xVec = normalize(cross yVec zVec) --the 'sideways' vector
-- Once we've calculated the two new Z & X vectors,
-- we need to re-calculate the Y to ensure it is orthogonal
yVec = normalize (cross zVec xVec) --the local Y axis
newTM = matrix3 xVec yVec zVec targetPos --make a matrix of the new X, Y and existing Z, keep the position
)
tm = Inverse( viewport.getTM() )
camA = FreeCamera transform:tm
tm = camA.transform
newTM = flip_tm_along_vector tm:tm origin:[0,0,0]
camB = FreeCamera wirecolor:green transform:newTM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment