Input coordinates using script?

Input coordinates using script?

neoxcorex
Enthusiast Enthusiast
2,072 Views
6 Replies
Message 1 of 7

Input coordinates using script?

neoxcorex
Enthusiast
Enthusiast

Hello,

I created an animation of a moving vehicle in another program called HVE.
This program can only export animations as .HVO file format, which Maya cannot import.

To import the animation path, HVE provides the coordinates of the vehicle.

Is there a Mel or Phython script I could use to quickly imput all of the coordinates?
I`m fine with using a primitive shape like a cube in Maya to represent the vehicle.

Here is a small portion of the coordinates:

Time       X               Y                    Z                   Roll             Pitch         Yaw
(sec) (ft) (ft) (ft) (deg) (deg) (deg)
0 301.59 -1166.4 -114.4 7.31 -7.19 48.5
0.1 305.34 -1162.13 -113.63 7.33 -7.27 48.54
0.2 309.08 -1157.82 -112.8 7.44 -7.25 48.67
0.3 312.8 -1153.46 -112.02 7.63 -7.52 48.84
0.4 316.5 -1149.04 -111.32 7.86 -7.71 49.07
0.5 320.19 -1144.58 -110.63 8.02 -7.53 49.39
0.6 323.85 -1140.12 -109.87 8.04 -7.38 49.73
0.7 327.5 -1135.64 -109.04 7.93 -7.49 50.05
0.8 331.13 -1131.13 -108.22 7.69 -7.79 50.37

 

Any help is much appeciated.

Regards,

0 Likes
2,073 Views
6 Replies
Replies (6)
Message 2 of 7

saihtam
Collaborator
Collaborator
Can you please upload a full file?
- Mathias
0 Likes
Message 3 of 7

saihtam
Collaborator
Collaborator

So this is my quick and dirty code for this. I think I have the right axis for the pitch etc. I never use those terms, so I might have messed those up. But you can fix that easily by moving the variables around where I split out the values.

import maya.cmds as cmds

#object in scene to apply animation to
transform = 'locator1'

#path to your file
filePath = 'C:/Users/Mathias/Desktop/something.hvo'

#open the file in python and read it's content as lines and pass that to a list
with open(filePath) as f:
    content = f.readlines()
#delete the two first lines since we're not interested in them
del content[:2]

#loop through the remaining lines and spilt each line to get out the values
for line in content:
    timeVar, tx, ty, tz, rz, rx, ry = line.split()
    
    #now set the values that we've read, I'm using float on value as it will
    #error if we pass in an integer. So just making sure.
    cmds.setKeyframe(transform, time=timeVar, attribute='tx', value=float(tx))
    cmds.setKeyframe(transform, time=timeVar, attribute='ty', value=float(ty))
    cmds.setKeyframe(transform, time=timeVar, attribute='tz', value=float(tz))
    
    cmds.setKeyframe(transform, time=timeVar, attribute='rx', value=float(rx))
    cmds.setKeyframe(transform, time=timeVar, attribute='ry', value=float(ry))
    cmds.setKeyframe(transform, time=timeVar, attribute='rz', value=float(rz))

 

 

 Only thing you need to change is the path to your file and the transform variable, which will be your object in Maya. Make sure you write the filepath with '/' forward slashes and NOT '\' backwards slashes. Stupid Windows... 

 

This works with the information you've provided. I'm assuming the two first lines include the "Time roll" etc. If it only contains raw values and nothing else, then just remove/comment out the 'del content[:2]' line.

- Mathias
0 Likes
Message 4 of 7

neoxcorex
Enthusiast
Enthusiast

Hi Saihtam,

 

I replaced the path and placed the coordinates into an excel file, since Maya isn't able to read the .hvo file.

 

I then received an error on line 3.1

 

------------------------------------------------------------------------------------------------------ 

import maya.cmds as cmds

#object in scene to apply animation to
transform = 'locator1'

#path to your file
filePath = ' C:/Users/vtran.HCE/Desktop/Convert/hve/hve_coordinates.xlsx'

#open the file in python and read it's content as lines and pass that to a list
with open(filePath) as f:
content = f.readlines()
#delete the two first lines since we're not interested in them
del content[:2]

#loop through the remaining lines and spilt each line to get out the values
for line in content:
timeVar, tx, ty, tz, rz, rx, ry = line.split()

#now set the values that we've read, I'm using float on value as it will
#error if we pass in an integer. So just making sure.
cmds.setKeyframe(transform, time=timeVar, attribute='tx', value=float(tx))
cmds.setKeyframe(transform, time=timeVar, attribute='ty', value=float(ty))
cmds.setKeyframe(transform, time=timeVar, attribute='tz', value=float(tz))

cmds.setKeyframe(transform, time=timeVar, attribute='rx', value=float(rx))
cmds.setKeyframe(transform, time=timeVar, attribute='ry', value=float(ry))
cmds.setKeyframe(transform, time=timeVar, attribute='rz', value=float(rz));
// Error: #object in scene to apply animation to
//
// Error: Line 3.1: Syntax error //

------------------------------------------------------------------------------------------------------ 

Here is the code I placed in:

import maya.cmds as cmds

#object in scene to apply animation to
transform = 'locator1'

#path to your file
filePath = ' C:/Users/vtran.HCE/Desktop/Convert/hve/hve_coordinates.xlsx'

#open the file in python and read it's content as lines and pass that to a list
with open(filePath) as f:
content = f.readlines()
#delete the two first lines since we're not interested in them
del content[:2]

#loop through the remaining lines and spilt each line to get out the values
for line in content:
timeVar, tx, ty, tz, rz, rx, ry = line.split()

#now set the values that we've read, I'm using float on value as it will
#error if we pass in an integer. So just making sure.
cmds.setKeyframe(transform, time=timeVar, attribute='tx', value=float(tx))
cmds.setKeyframe(transform, time=timeVar, attribute='ty', value=float(ty))
cmds.setKeyframe(transform, time=timeVar, attribute='tz', value=float(tz))

cmds.setKeyframe(transform, time=timeVar, attribute='rx', value=float(rx))
cmds.setKeyframe(transform, time=timeVar, attribute='ry', value=float(ry))
cmds.setKeyframe(transform, time=timeVar, attribute='rz', value=float(rz))

 
Excel file:

https://www.dropbox.com/s/tli5bwgqkatstse/hve_coordinates.xlsx?dl=0

HVO file:
https://www.dropbox.com/s/mnfjyggi823mv0x/test1.hvo?dl=0

Much thanks

0 Likes
Message 5 of 7

RFlannery1
Collaborator
Collaborator

Not sure about the .xlsx file format, but I'm going to guess this simple script can't read it.  Try saving the file out from Excel as a .csv file.

0 Likes
Message 6 of 7

saihtam
Collaborator
Collaborator
Yeah, for xlsx you need a new module. But I believe the hvo format should work with my script. Just need to try it. I'll see if I can't get around to it later today.
- Mathias
0 Likes
Message 7 of 7

saihtam
Collaborator
Collaborator

Maya is able to read the hvo file(or rather, Python is). It's just a plane text file and Python has no problem reading it. Are the hvo file and the xlsx file from he same animation? Because they have very different values. Something that makes it hard for me to determine which values I should extract from the hvo file, as it's very messy and contains multiple things that are named the same. I've ammended my previous code to work with the hvo file. As the animation only covers 6 seconds it's not really easy to see if it's right. The movement seems right, but the rotation seems a bit strange. If you can provide an hvo file and an xlsx file of the same animation then I can make sure this works as expected.

 

import maya.cmds as cmds

#object in scene to apply animation to
transform = 'pCube1'

#path to your file
filePath = 'C:/Users/Mathias/Downloads/test1.hvo'

#open the file in python and read it's content as lines and pass that to a list
with open(filePath) as f:
    content = f.readlines()
#delete the two first lines since we're not interested in them
del content[:4]

#line = content[2]
#line.replace(',',' ').split()

#loop through the remaining lines and spilt each line to get out the values
for line in content:
    lineContent = line.replace(',',' ').split()
    timeVar = lineContent[0]
    tx = lineContent[43]
    ty = lineContent[42]
    tz = lineContent[44]
    rx = lineContent[41]
    ry = lineContent[46]
    rz = lineContent[45]

    #now set the values that we've read, I'm using float on value as it will
    #error if we pass in an integer. So just making sure.
    cmds.setKeyframe(transform, time=timeVar, attribute='tx', value=float(tx))
    cmds.setKeyframe(transform, time=timeVar, attribute='ty', value=float(ty))
    cmds.setKeyframe(transform, time=timeVar, attribute='tz', value=float(tz))
    
    cmds.setKeyframe(transform, time=timeVar, attribute='rx', value=float(rx))
    cmds.setKeyframe(transform, time=timeVar, attribute='ry', value=float(ry))
    cmds.setKeyframe(transform, time=timeVar, attribute='rz', value=float(rz))

Do you have any experience running python code in Maya? You need to keep the indentations as they are. Whitespace is very important in Python. If you haven't done it before I suggest checking a tutorial. I've also included the code as a text file incase the forum messes up the formatting etc.

- Mathias
0 Likes