Home Technical Talk

Maxscript - frustrating group issue

I wrote a quick function to move a couple of objects to the bottom right quadrant whilst keeping their relative positions. I thought the best way to do this would be to group and ungroup them - I noticed this is what happens when you merge a max file into your current scene. The problem I'm having is that it doesn't have the effect that I'm expecting.

If I call the function the objects are placed centrally, not in the quadrant. However if I type the script line by line in the listener it works flawlessly. Maybe I'm missing something?
fn MoveQuadrant = (
		if selection.count > 1 then
		(
			group selection name:"tempGroup"
			select $tempGroup
			-- Center object
			move $tempGroup [-($tempGroup.pos.x),-($tempGroup.pos.y),-($tempGroup.pos.z)]
			-- Move to correct max quadrant
			move $tempGroup [($tempGroup.max.x),-($tempGroup.max.y),-($tempGroup.min.z)]
			clearSelection()
			ungroup $tempGroup
		)
		else
		(
			-- Put Pivot on bottom of object
			$.pivot = [($.max.x+$.min.x)/2, ($.max.y+$.min.y)/2, $.min.z]
			-- Center object
			move $ [-($.pos.x),-($.pos.y),-($.pos.z)]
			-- Move to correct max quadrant
			move $ [($.max.x),-($.max.y),0]
		)

Replies

  • eddybrown
    This worked for me.
    a = #($Teapot001, $Sphere001, $Tube001)
    select a
    group selection
    in coordsys local $.pos = [20,20,0]
    ungroup $group001
    

    Thanks, that moves them all together by 20,20,0 but it doesn't position them in the lower right quadrant which is what I was trying to achieve. To do that I was taking the min and max values of the group and using that amount to work out where to move it to. Does that make sense?
  • cglover
    -- group objects as a variable
    groupedObjects = group selection name:"tempGroup"
    --select group
    select groupedObjects
    --now work with $ as selection
    --get the min and max values of the bounding box and apply to variable as Point3
    BoundingBoxObjects = [$.max.x - $.min.x,$.max.y - $.min.y,$.max.z - $.min.z]
    --set new position already with the proper offset to the bottom right quadrant
    $.pos = [BoundingBoxObjects[1]/2,-BoundingBoxObjects[2]/2,0]
    --ungroup object
    ungroup groupedObjects


    i don't consider the z value, but you can position on the ground plane with

    BoundingBoxObjects[3]/2

    means the half of the bounding box height is the right position for the group. With this method i got an issue with objects smaller then 0.02m in height (3dsmax 2010, system units meters) although the listener shows the correct values there are internal roundings (almost mystical ;) ). See also Bobo's answer to float precision. If you have not such thin plates, it should be no problem.
  • eddybrown
    Thanks for the reply, I just tried your method however it doesn't give me the results I expected. This method moves the different objects to the same coordinates and doesn't respect the differences between original positions.

    In the end I decided to try a different approach, decided to move away from the grouping the objects and went with this instead which is probably slower way of doing it with the arrays. But it does seem to work well.
    --set blank arrays
    groupMinXArray = #()
    groupMaxYArray = #()
    groupMinZArray = #()
    -- set pivots, gather min, maxs
    for s in selection where ((superClassOf s) == geometryClass) do (
    s.pivot = [(s.max.x+s.min.x)/2, (s.max.y+s.min.y)/2, s.min.z]
    append groupMinXArray s.min.x
    append groupMaxYArray s.max.y
    append groupMinZArray s.min.z
    )
    -- calculate min, max values and positions
    groupMinX = amin groupMinXArray
    groupMaxY = amax groupMaxYArray
    groupMinZ = amin groupMinZArray

    --move all the objects
    for s in selection where ((superClassOf s) == geometryClass) do (
    in coordsys local s.pos = [-(groupMinX),-(groupMaxY),-(groupMinZ)]
    )
  • monster
    Offline / Send Message
    monster polycounter
    This line of code should do what you are trying to do above. It doesn't center the pivot on X and Y, but you didn't state that was your goal.

    MOVE THE SELECTION
    move selection [-selection.min.x,-selection.max.y,-selection.min.z]
    

    OR ONLY MOVE GEOMETRY CLASS
    move geometry [-geometry.min.x,-geometry.max.y,-geometry.min.z]
    
  • eddybrown
    monster wrote: »
    This line of code should do what you are trying to do above. It doesn't center the pivot on X and Y, but you didn't state that was your goal.

    MOVE THE SELECTION
    move selection [-selection.min.x,-selection.max.y,-selection.min.z]
    

    OR ONLY MOVE GEOMETRY CLASS
    move geometry [-geometry.min.x,-geometry.max.y,-geometry.min.z]
    

    That's amazingly simple! Brilliant! The pivot thing was an addition. I'm still learning at max script so far I think I can get things to work but not necessarily in the fastest or most efficient way. Thanks for the help guys :)
Sign In or Register to comment.