-1

I am trying to write in this simple code - where it checks the Maya Version, and if it true, it will then executes the following main functions.

This is the code:

if int(mel.eval('about -v')[0:4]) < 2011:
    ...

Then I was given the following error:

ValueError: invalid literal for int() with base 10: 'Exte'
[Renaming] invalid literal for int() with base 10: 'Exte'

And thus as I run this code to check the Maya version that I have: mc.about( v = True ), the result displayed is Extension for Autodesk Maya 2014 Service Pack 2 P03 and hence thats the error. I know that last time when I did the same way for version checking, it is displayed as M2014

As such, is there any ways in which I can revert the current version string into 2014?

yan
  • 631
  • 9
  • 30
  • Instead of `mel.eval('about -v')`, you should just call `cmds.about(v=True)`. It'll be the same return value, but it's cleaner than evaluating mel – mhlester May 27 '14 at 16:27

3 Answers3

1

Use re to find the year:

import re
m1 = re.search(r'\d{4}', s1)
i = m1.group()
print int(i)
2014

s2 = "M2014"
m2 = re.search(r'\d{4}', s2)
j = m2.group()
print int(j)
2014

s3 = "Extension for Autodesk Maya M2014 Service Pack 2 P03"
m3 = re.search(r'\d{4}', s3)
k= m3.group()
print int(k)
2014

Take a look at ast helpers, it would be better using that than using eval.

ast.literal_eval(node_or_string) Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Hi, thanks for getting back to me. Pardon me but I am thinking what happens if in the event, the version was reverted back to 2014 or M2014 (sorry, I am unable to remember which is the right one, but say if it is 2014), will the above code be able to handle it? – yan May 26 '14 at 13:56
  • @yan, I added to my answer, it will work for all those strings. – Padraic Cunningham May 26 '14 at 14:04
  • thank you so much. So can I presume this is more of a `if...or..` situation? – yan May 26 '14 at 14:20
  • Read up on Regular Expressions in Python. The `re.search` method searches for a specific pattern in a string; in this case the pattern is `'\d{4}'`, which in Python's reg-ex language means "digits, exactly 4 of them". So it works as long as the version string contains a 4-digit version number before any other 4-digit string; it breaks if Maya changes to `x.y.z` style version numbering, or returns a version number like `"irrelevant 456789 blah blah 2014"`, in which case it'll match the first 4 digits it finds, `4567`. – Russell Borogove May 26 '14 at 15:06
  • @yan, what if the version number is M2014, do you want the M also or just the year? – Padraic Cunningham May 26 '14 at 15:47
  • @RussellBorogove Thank you for the clear explanation you have given. Will read up on `re` methods – yan May 27 '14 at 08:33
  • As I was googling, I found out about this command `cmds.about(api=True)` and upon executing it in my Maya, I got `201406` as my result. No offences, but will this command be more helpful in any ways? Seeing that it has numerical outputs and I am tallying against maya versions... just asking :) – yan May 27 '14 at 08:34
0

according to what you saying, this is the output of:

mel.eval('about -v')

Extension for Autodesk Maya 2014 Service Pack 2 P03

then instead of getting the first 4 chars: [0:4] take the 28-32 chars. like this [28:32]

that will solve your problem.

that been said:

  1. it is not recommend to use eval. you can read here more about why not.

  2. parsing an output string in this way is also not recommended since you code is not vulnarable to changes in the output format. I would try to find a different way to get the version of Maya.

Community
  • 1
  • 1
Udy
  • 2,492
  • 4
  • 23
  • 33
  • thanks for getting back to me. I did thought up of coding it like the way you did it, but was afraid that it will not work again, should there be a version update etc, and the string goes back to M2014, or 2014 (as I have comment in the above post...) – yan May 26 '14 at 13:58
  • mel.eval is not pythons eval function. Since maya allready is running in client mode the security issue is a lot less severe. Besides maya evals all the time as most gui has no choice about it. Besides if you open maya files fron others you gave them the keys to the computer anyway if it wasnt a ascii file and you didnt manually read it first. – joojaa May 27 '14 at 13:23
0

You could just use the about command and re to search for the version incase it's x64.

import maya.cmds as cmds
import re

if __name__ == '__main__':

    version = cmds.about(version=True)
    match = re.search(r'\d+', version)

    print match.group(0)

Hope that helps!

chribis
  • 277
  • 7
  • 26