Home Technical Talk

Newbie MaxScript Question; Creating Objects

polycounter lvl 10
Offline / Send Message
r0xmys0x polycounter lvl 10
Hey guys, just started learning MaxScript and I'm trying to make a tool for work but I've hit a wall. Embarrassingly early as well :P

So what I'm trying to do is this;

I have an object that's broken up into lots of chunks. Basically we need a dummy object positioned at the pivot position of each of these chunks.

place = $.pos
Dummy pos:place

Does it no problem. But I have an object here that has 55 pieces and while its already a time save condensing it down to a button press I need to work out how to loop this through all the selected objects not only for this script but as a basis of learning how to do this stuff.

Any help would be very much appreciated :)

Replies

  • xXm0RpH3usXx
    Options
    Offline / Send Message
    xXm0RpH3usXx polycounter lvl 13
    for i= 0 to $.count do(

    CODE HERE

    );

    (i think, newbie myself :P)
    This should loop through all your objects you have selected.

    I would however place the dummy at the lowest point of your model, not just its position.

    Gimme a holla if you want to go a step into this direction
  • r0xmys0x
    Options
    Offline / Send Message
    r0xmys0x polycounter lvl 10
    Thanks for the reply buddy but it didn't work.

    EDIT:

    place = 0
    for i = 1 to selection.count do(
    place = selection.pos
    Dummy pos:place
    )

    Works, cheers buddy. One more question; how do I select all the dummy objects in a scene and delete them?
  • xXm0RpH3usXx
    Options
    Offline / Send Message
    xXm0RpH3usXx polycounter lvl 13
    for i = 0 to selection.count do(

    if $ = dummy (phew, however you will check this, do not know the exact line)
    then delete.$ (does that work? Oo)

    )

    maybe you need to put in an else, but i dont think so though...
  • r0xmys0x
    Options
    Offline / Send Message
    r0xmys0x polycounter lvl 10
    Cheers for the help buddy. As the only dummies in the scene will be made by the script I managed to just mass delete them all with;

    delete $Dummy*
  • xXm0RpH3usXx
    Options
    Offline / Send Message
    xXm0RpH3usXx polycounter lvl 13
    well, what you could do is save all the dummys you created with the first script in an array and then cycle thru that array with the second script deleting them...

    this way you could have other dummys in there, too

    or you look into automatically renaming those dummys (e.g. putting a prefix like "pivotdum_" before you create a dummy with the first script and then search through all objects in a scene and delete all those that have "pivotdum_" in their name...
  • r_fletch_r
    Options
    Offline / Send Message
    r_fletch_r polycounter lvl 9
    selection is an object collection. You can access more specific object collections within your scene. for instance

    objects -- all the objects

    geometry

    lights

    cameras

    helpers

    shapes

    systems

    spacewarps

    and we know that dummys are of the class dummy (type classof $ to get your selections class)
    for obj in helpers do
    (
       if classof obj == dummy then delete obj
    )
    

    Deleting by class is in this case a much better option. Someone could name a scene element dummy and your script will delete it. not a good way to make friends :)
    In general working with names is a no no, unless you make a name exceptionally specific.
  • r0xmys0x
    Options
    Offline / Send Message
    r0xmys0x polycounter lvl 10
    r_fletch_r; cheers for this buddy, very helpful.

    fn delDummies =
    (
    delete (
    for o in helpers where classOf o == Dummy collect o
    )
    );

    is what i came up with based on your suggestions.

    At the moment I'm trying to expand this tool to make it more autonomous. To do that I want to;

    • Take all the objects in a selection and name them in sequence.
    • Create a duplicate of all these objects in the scene adding a prefix to the current names and have these sorted in an array. While hiding all the original object selections
    • Create a dummy at the origin of all these pieces (this bit is done :) ), but have the dummies named so they correspond to the piece of geometry they are sitting on.
    • Parent the geometry to its corresponding chunk.
    At the moment I'm aware of selection as array but don't fully understand how to sort them and use them, any help would be awesome :D

    Cheers.
  • r_fletch_r
    Options
    Offline / Send Message
    r_fletch_r polycounter lvl 9
    How do you want to sort the array?

    also what is its coresponding chunk? is that the dummy or the hidden geometry?
  • r_fletch_r
    Options
    Offline / Send Message
    r_fletch_r polycounter lvl 9
    Copy this into a maxscript window and read the comments. I think this should do what you need
    aSelection = ( selection as array ) --store the selection in an array
    aDuplicatedObjects = #() -- create an empty array for storing duplicated objects
    
    for i = 1 to aSelection.count do
    (
    	obj = aSelection[i] --pull the object from the array at index i
    	--name the object with a string and a number. the number needs to be cast as a string for it to be added to the string.
    	-- "Object" can be anything you want
    	obj.name = "Object" + ( i as string )
    	
    	newObj = ( copy obj ) -- copy the object and store it in newObj
    	newObj.name = "duplicate_" + obj.name -- name the duplicate something meaningfull
    	
    	append aDuplicatedObjects newObj --add the new object to the end of the aDuplicatedObjects array
    	
    	hide obj -- hide the original object
    	
    	--Create a dummy, position it and name it using dummy_ as a prefix and the objects name
    	--strings can be stitched together with +, they can also be compared with ==
    	nDummy = dummy pos: ( obj.position ) name: ( "dummy_" + obj.name)
    	newObj.parent = nDummy -- parent the new object to the dummy (presuming thats the 'chunk' you mentioned)
    )
    
  • r0xmys0x
    Options
    Offline / Send Message
    r0xmys0x polycounter lvl 10
    Worked like a charm, cheers for the help buddy. :thumbup:
  • r_fletch_r
    Options
    Offline / Send Message
    r_fletch_r polycounter lvl 9
  • r0xmys0x
    Options
    Offline / Send Message
    r0xmys0x polycounter lvl 10
    Since I've been so successful finding grade A help here before.... I'm back :D Still working on this script which is almost done. However I'm having more trouble. What I'm trying to do it;

    • snapshot all the meshes in an array to a new single mesh. (tempMesh)
    • determine the centre point on x and y - store these
    • determine the lowest z point of the new mesh - store that
    • create a dummy in this place (center x/y, lowest z)
    • delete tmpMesh
    Some of these things I can already do, the biggy for me is getting a snapshot of an array of meshes in the scene as 1 single mesh, that I can then use and discard.
  • monster
    Options
    Offline / Send Message
    monster polycounter
    You can skip creating a tempMesh by using selection properties.
    select MeshArray
    NewDummy = dummy pos:[selection.center.x,selection.center.y,selection.min.z]
    
  • xXm0RpH3usXx
    Options
    Offline / Send Message
    xXm0RpH3usXx polycounter lvl 13
    j = targetMesh
    for i in collection do //change that to something like i in (yourarray) do
    (
    meshop.attach j i
    )

    found that on CGTalk, was a three minutes search.
    there is also a "snapshotAsMesh" function in max, might wanna google that.

    you can then check the snapshot (or merged) mesh with something like


    for v=1 to num_verts do
    (
    vert = getVert checkMesh v
    if min_v > vert.z then
    (
    min_v = vert.z
    )
    )
    delete checkMesh
    )
    )

    (got that from David Liam's AO render script where he puts a plane under your mesh at rendertime)
Sign In or Register to comment.