简体   繁体   中英

Maya Python: How to convert a list to an Integer

I am still learning python, so bear with me. I get the last keyframe of an animation between keyframe 1000 and 2000.

shotLength = cmds.keyframe(time=(1000,2000) ,query=True)
del shotLength[:-1]
print shotLength 

Result:

[1090.0]

At this point only the desired keyframe remains in the list as a value. I convert this value to an integer like so:

shotLengthInt = list(map(int, shotLength))
print shotLengthInt

Result:

[1090]

Now i want to add +1 to this value so it would look like this:

[1091]

I just cant figure out how.

You can edit the following:

shotLengthInt = list(map(int, shotLength))
print shotLengthInt

We can pass a lambda function to map, to achieve it:

shotLengthInt = map(lambda x: int(x) + 1, shotLength)
print shotLengthInt

Your value is contained within a list (notice the square brackets), so to update this value by 1, you need to reference the first index of the list and increment that by 1

>>> shotLengthInt = [1090]
>>> shotLengthInt
> [1090]
>>> shotLengthInt[0] += 1
>>> shotLengthInt
> [1091]

You can also remove the list() when assigning the value to shotLengthInt

>>> shotLength = [1090.0]
>>> shotLength
> [1090.0]
>>> shotLengthInt = map(int, shotLength)
>>> shotLengthInt
> [1090]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM