I cant get this pyton wrapper to be recognized by maxscript. Look for assistance

I cant get this pyton wrapper to be recognized by maxscript. Look for assistance

Anonymous
Not applicable
873 Views
3 Replies
Message 1 of 4

I cant get this pyton wrapper to be recognized by maxscript. Look for assistance

Anonymous
Not applicable

Hello,

I currently have python script named MyfirstSpiceTestinMax.py in a directory on C: called "mydirectory" that requires me to on line 1 have: "import awrapper as spice"
So you know "awrapper" is a python wrapper.

When I create a maxscript .ms file I type "python.ExecuteFile @"C:\mydirectory\MyfirstSpiceTestinMax.py"

The result is I get an error:

Runtime error: Line 1 <module>()
<type 'exceptions.importError'> No module named awrapper

 


Should this be working? or if not why not? or how can I make max accept using this wrapper if thats even allowed?

when I run the line "import awrapper as spice" within python it works, as well as the following python wrapper code

When I create a maxscript .ms file that just says hello world "python.ExecuteFile @"C:\mydirectory\helloworld.py" this works fine so
I think max doesnt like the "awrapper" or doesnt know where to find it?


I am total python newby.
Any ideas how to get it to work?
thanks

0 Likes
874 Views
3 Replies
Replies (3)
Message 2 of 4

malcomarmstrong
Advocate
Advocate

If you are a total Python newbie, I would suggest looking at some Python fundamentals, especially in the areas of modules such as os and sys as these are fundamental to python use.

 

Have you updated your sys.path to include the directory location of the file?

Does the folder it is in have an __init__.py file so that Python can recognise it as an importable location? 

 

You need to be wary or mindful though, of the way python external to max runs and how it runs as a wrapper for other code in Max.

 

Often, if you write code in the editor (as Python), it does not always evaluate to what you may expect as I found out while trying to update my sys.path with locations to call scripts from.

 

The best practice I have so far is below.

 

in the Maxscript editor, type this (make sure language is set to Python):

import os, sys
print"\n".join(str(i) for i in sys.path)

This will print out the locations Max looks in for modules etc. With this information, you can start to add locations:

 

As mentioned, you will need a __init__.py file in the scripts location. You can easily do this by navigating to the folder and creating an empty text file. Simply rename it to __init__.py and that's the file required for python to allow imports from that location, but we still need to get the folder location onto the sys.path.

 

In your location to be added, create a file with the actions, methods and classes (if using them). At the top of the file add this: 

 

import os, sys
fileDir = os.path.dirname(__file__)
if fileDir not in sys.path:
    sys.path.append(fileDir)
print"\n".join(str(i) for i in sys.path)
print("The system path has been updated\n\n")

#MORE CODE ETC ETC

Now, Using Ctrl + E (if using the Maxscript Editor) will not give you the result you expect due to the way in which the internal python is being wrapped. What you can do, is to have a .ms file also in the same location which has the following:

thisFile = getFilenamePath(getThisScriptFilename())
python.ExecuteFile(thisFile+"nameOfYourFile.py")

The above file can be open in the maxscript editor, so each time you update your .py file, all you have to do is to use Ctrl + E in the .ms file to run the .py file. This also means that if you like to code in an external ide, you can. I wont add to the confusion by talking about sublime packages to run max code from it, thats another topic.

So, to confirm.

1. Check the sys.path

2. Create a __init__.py file in the folder to call scripts from

3. In your .py file include the sys.path.append() code

4. In the same location, create a.ms file with the maxscript code so you can execute your .py file without having to source it from the run menu or other methods.

 

Whatever works for you though, many ways to achieve a result.

 

Hope this helps.

 

Message 3 of 4

Anonymous
Not applicable

Thank you for that help.

I have not got it work yet but your information is giving me a direction to work in.

0 Likes
Message 4 of 4

malcomarmstrong
Advocate
Advocate

You might also want to use double backslashes in your url i.e C:\\place\\place1\\object.py so that there are no escape characters affecting your address.

 

Regards

0 Likes