Search code examples
mayamayavimelnurbspymel

Read NURBS control points in Maya from an ASC file


Say I have an ASC file with 100 (x,y,z) coordinates (representing a helix-like world line trajectory). I want to import that file in Maya and create a NURBS with each of the coordinate from my ASC file as a control point.

I know how to import asc files in python but I don't know how to create NURBS control point out of it!

I assume it should be a simple script but I am new to MEL script and Maya driven Python. Any script snippet in MEL or Python to get it working?

Thanks!


Solution

  • the curve command takes an argument with the flag p (for 'points'). So a simple linear curve from 0,0,0 to 1,0,0 looks like

     import maya.cmds as cmds
     cmds.curve(  p=[(0,0,0), (1,0,0)],  d = 1)
    

    and a degree-3 nurbs curve might look like

    cmds.curve( p=[(0, 0, 0), (3, 5, 6), (5, 6, 7), (9, 9, 9)], d=3 )
    

    Your script will have to convert the asc points to XYZ values. If you are reading these yourself from a text file you'll have to the use standard python open command to open and read from a file. If you already have locators or other physical objects in your scene you can get their positions with the xform command. This will get the position of an object named 'locator_name_goes_here' as an XYZ tuple.

    loc_pos = cmds.xform('locator_name_goes_here', q=True, t=True, ws=True)
    

    So.to run a curve through a bunch of points you would collect all of their positions in a big list and then pass that whole list to the curve command's p flag:

    # assuming all locators or objects are selected in order
    locator_positions = []
    for each_locator in cmds.ls(sl=True):
        locator_positions.append (cmds.xform(each_locator, q=True, t=True, ws=True)
    cmds.curve(p=locator_positions, d=3)