Maya Python: FTP Upload

Tutorial how to upload files using FTP in maya with python

In this tutorial you will create a script that upload your scene to your server.

What you will be creating:


Let’s start by creating a new script. Just open the Script Editor and type everything in the Python tab. Let’s test if we can upload one file. This code should upload a text file to your server. (This should work before we continue.)

import ftplib
s = ftplib.FTP('josbalcaen.com','user','pass')  #connect
f = open('C://Users/Jos/Desktop/test.txt', 'rb') #open file
s.storbinary('STOR uploadtest.txt', f) #upload our file
f.close() #close text file
s.quit()

For this tutorial we will create a window with a progressbar. So let’s make a simple window with some textfiels, a button and a progressbar.

import maya.cmds as cmds
class ftpUpload:
    def __init__(self):
        self.name = 'FTP Upload'
        self.title = 'FTP Upload'

        self.window = cmds.window(title=self.title)
        cmds.rowColumnLayout( numberOfColumns=2,
columnAttach=(1, 'right', 0), columnWidth=[(1, 30), (2, 200)] )

        cmds.text( label='FTP ' )
        self.ftpText = cmds.textField( text = 'josbalcaen.com' )
        cmds.text( label='USER ' )
        self.userText = cmds.textField( text = 'user' )
        cmds.text( label='PASS ' )
        self.passText = cmds.textField( text = 'pass' )
        cmds.text( label=' ' )
        self.uploadButton = cmds.button( label='Upload', width=200,
command=self.upload)
        cmds.text( label=' ' )
        self.progress = cmds.progressBar(height=10, width=200)

        cmds.showWindow( self.window )
    def upload(self, *args):
        print 'button pressed'

If this code evaluates fine you should be able to create a window adding the following line :

ftpUpload()

So now we have to upload the scene if you press the “Upload” button. So lets change the upload(self, *args) method to the following…

ftpText = cmds.textField(self.ftpText, q=True, tx=True)
userText = cmds.textField(self.userText, q=True, tx=True)
passText = cmds.textField(self.passText, q=True, tx=True)

print 'upload to %s with user %s and pass %s' % \
(ftpText, userText, passText)

name = os.path.basename(cmds.file(q=True, l=True)[0])
cmds.file( rename='tmp.ma' )
cmds.file( save=True, de=False, type='mayaAscii' )
sceneName = cmds.file(q=True, l=True)
s = ftplib.FTP(ftpText,userText,passText) #connect
f = open(sceneName[0], 'rb')
s.storbinary('STOR '+name, f)
f.close()
s.quit()

os.remove(sceneName[0])
cmds.file(rename=name)

Now we have the code to upload our scene, but it would be nice if the progressbar should show something! To make this work we need a callback mechanism. Change the upload code to

def callback(p):
    cmds.progressBar(self.progress, edit=True, step=1)
ftpText = cmds.textField(self.ftpText, q=True, tx=True)
userText = cmds.textField(self.userText, q=True, tx=True)
passText = cmds.textField(self.passText, q=True, tx=True)

print 'upload to %s with user %s and pass %s' % \
(ftpText, userText, passText)

name = os.path.basename(cmds.file(q=True, l=True)[0])
cmds.file( rename='tmp.ma' )
cmds.file( save=True, de=False, type='mayaAscii' )
sceneName = cmds.file(q=True, l=True)

s = ftplib.FTP(ftpText,userText,passText)  #connect
f = open(sceneName[0], 'rb')
filesize = os.path.getsize(sceneName[0])
s.storbinary('STOR '+name, f,filesize/100,callback)
f.close()
s.quit()

os.remove(sceneName[0])
cmds.file(rename=name)

Done!
Feel free to post any comment!
Cheers!

Tagged , , , ,

One thought on “Maya Python: FTP Upload

  1. you should also implement with a logging of event when file upload process begins…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>