Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Simple hair uv script for unreal engine groom # Error: RuntimeError: file <maya console> line 82: (kInvalidParameter): No element at given index #

Simple hair uv script for unreal engine groom # Error: RuntimeError: file <maya console> line 82: (kInvalidParameter): No element at given index #

karl_suh_146
Participant Participant
1,080 Views
10 Replies
Message 1 of 11

Simple hair uv script for unreal engine groom # Error: RuntimeError: file <maya console> line 82: (kInvalidParameter): No element at given index #

karl_suh_146
Participant
Participant

Text truncated (was 6129137 bytes) : [u'Jup_Grp0', u'SplineGrp1|Jup_Grp1', u'SplineGrp0|Jup_Grp1',...
# Error: RuntimeError: file <maya console> line 82: (kInvalidParameter): No element at given index #

 

I have tried the simple hair uv script provided by Unreal engine for both Maya 2024 and 2020 but I keep getting the above error message. I don't know what I'm doing wrong. I pasted the script below.

 

 

from maya import cmds
from maya import OpenMaya
import os


def create_root_uv_attribute(curves_group, mesh_node, uv_set='map1'):
    '''
    Create "groom_root_uv" attribute on group of curves.
    '''

    # check curves group
    if not cmds.objExists(curves_group):
        raise RuntimeError('Group not found: "{}"'.format(curves_group))

    # get curves in group
    curve_shapes = cmds.listRelatives(curves_group, shapes=True, noIntermediate=True)
    curve_shapes = cmds.ls(curve_shapes, type='nurbsCurve')
    if not curve_shapes:
        raise RuntimeError('Invalid curves group. No nurbs-curves found in group.')
    else:
        print ("found curves")
        print (curve_shapes)

    # get curve roots
    points = list()
    for curve_shape in curve_shapes:
        point = cmds.pointPosition('{}.cv[0]'.format(curve_shape), world=True)
        points.append(point)

    # get uvs
    values = list()
    uvs = find_closest_uv_point(points, mesh_node, uv_set=uv_set)
    for u, v in uvs:
        values.append([u, v, 0])
        #print (str(u) + " , " + str(v)  )

    # create attribute
    name = 'groom_root_uv'
    cmds.addAttr(curves_group, ln=name, dt='vectorArray')
    cmds.addAttr(curves_group, ln='{}_AbcGeomScope'.format(name), dt='string')
    cmds.addAttr(curves_group, ln='{}_AbcType'.format(name), dt='string')

    cmds.setAttr('{}.{}'.format(curves_group, name), len(values), *values, type='vectorArray')
    cmds.setAttr('{}.{}_AbcGeomScope'.format(curves_group, name), 'uni', type='string')
    cmds.setAttr('{}.{}_AbcType'.format(curves_group, name), 'vector2', type='string')

    return uvs

def find_closest_uv_point(points, mesh_node, uv_set='map1'):
    '''
    Find mesh UV-coordinates at given points.
    '''

    # check mesh
    if not cmds.objExists(mesh_node):
        raise RuntimeError('Node not found: "{}"'.format(mesh_node))

    # check uv_set
    uv_sets = cmds.polyUVSet(mesh_node, q=True, allUVSets=True)
    if uv_set not in uv_sets:
        raise RuntimeError('Invalid uv_set provided: "{}"'.format(uv_set))

    # get mesh as dag-path
    selection_list = OpenMaya.MSelectionList()
    selection_list.add(mesh_node)

    mesh_dagpath = OpenMaya.MDagPath()
    selection_list.getDagPath(0, mesh_dagpath)
    mesh_dagpath.extendToShape()

    # get mesh function set
    fn_mesh = OpenMaya.MFnMesh(mesh_dagpath)

    uvs = list()
    for i in range(len(points)):

        script_util = OpenMaya.MScriptUtil()
        script_util.createFromDouble(0.0, 0.0)
        uv_point = script_util.asFloat2Ptr()

        point = OpenMaya.MPoint(*points[i])
        fn_mesh.getUVAtPoint(point, uv_point, OpenMaya.MSpace.kWorld, uv_set)

        u = OpenMaya.MScriptUtil.getFloat2ArrayItem(uv_point, 0, 0)
        v = OpenMaya.MScriptUtil.getFloat2ArrayItem(uv_point, 0, 1)

        uvs.append((u, v))

    return uvs

def abc_export(filepath, node=None, start_frame=1, end_frame=1, data_format='otawa', uv_write=True):
    
    job_command = '-frameRange {} {} '.format(start_frame, end_frame)
    job_command += '-dataFormat {} '.format(data_format)
    
    job_command += '-attr groom_root_uv '

    if uv_write:
        job_command += '-uvWrite '
    
    job_command += '-root {} '.format(node)   
    
    job_command += '-file {} '.format(filepath) 
    
    cmds.AbcExport(verbose=True, j=job_command)
    
    


def main():
    
    export_directory = ('C:\\Users\\Grace\\OneDrive\\Documents\\Zbrush\\MayaTransfer')
    hair_file = os.path.join(export_directory, 'Badgerfur3.abc')
    curve_top_group= 'SplineGrp0'
    uv_mesh='HeadShape_Original_Original'
    
    create_root_uv_attribute( curve_top_group , uv_mesh)
    abc_export(hair_file, curve_top_group)
    
main()
0 Likes
Accepted solutions (1)
1,081 Views
10 Replies
Replies (10)
Message 2 of 11

jmreinhart
Advisor
Advisor

Could you please use the "</>" button and re-paste the code? If you just paste it the normal way it messes up with the indenting.

0 Likes
Message 3 of 11

karl_suh_146
Participant
Participant

I have repasted the code.

0 Likes
Message 4 of 11

jmreinhart
Advisor
Advisor

Could you please share the scene that is giving an error? Or a simple scene where it occurs?

0 Likes
Message 5 of 11

karl_suh_146
Participant
Participant

Like the scene file?

0 Likes
Message 6 of 11

jmreinhart
Advisor
Advisor

Yes, because it could be an issue with the script or your scene.

0 Likes
Message 7 of 11

karl_suh_146
Participant
Participant

Here is the scene file. I simplified it a bit so I could upload it.

0 Likes
Message 8 of 11

jmreinhart
Advisor
Advisor

The mesh does not have any UVS, so the getUVAtPoint function doesn't work. I tried setting some automatic UVs on it and running the script and it worked. 

Message 9 of 11

karl_suh_146
Participant
Participant

That's weird, I added the UVs by clicking UV->Automatic and made the fur again, imported the new alembic file, and I still get the same error. I have attached my file below. Can you send me the script you used?

0 Likes
Message 10 of 11

karl_suh_146
Participant
Participant

Could you send me the options you used for that automatic UV mapping? Maybe there is a box I haven't checked off or something.

0 Likes
Message 11 of 11

karl_suh_146
Participant
Participant
Accepted solution

I figured it out. The UV map has to be stored in the map1 UV set in order for the script to recognize it. Thank you for your help!

0 Likes