[PYTHON] By linking Maya with Excel or spreadsheets ~

It may be obvious to those who make tools with python, but It's a great opportunity, so I'd like to leave something together ٩ (๑ • ̀ω • ́๑) ۶

Motivation

○ csv is fine, but designers also want to read and use Excel ~ ○ I want to get the number of frames from the Excel motion list ~ ○ I want you to update the status of Google Spreadsheet without permission after outputting and uploading ~

I want to refer to and update external data such as ...! !! !! !! !! !! !!

What is an external module?

There are many third-party external modules in python. You can easily do such things that mel cannot do.

import csv
import os

In python, you can call various modules by inserting import first. Since csv and os are original modules, there is no need to drop them separately.

Maya is Python2 series, so be careful to drop the module for it!

This time I will put the modules directly in the script folder of maya, In the case of team management, it is better to manage in a separate directory, so For details, see here! http://www.comtec.daikin.co.jp/DC/UsersNotes/Ritaro/tutorial/maya_04/ http://tm8r.hateblo.jp/entry/2016/10/18/222755

I put it directly in the scripts folder like this.

image

This environment ○ Windows 7 ○ Maya 2016 ○ (○. ○. ○) of each module is the version used this time!

I think it's better to install using pip How to use pip http://www.python-izm.com/contents/basis/pip.shtml This time, I simply moved the one installed in another place to scripts.

use excel

xlrd (0.9.4) xlwt (1.0.0) xlutils (1.7.1)

Get the number of frames from the scene name in Excel and write today's date Excel should be saved in .xls!

image

import xlrd
import xlwt
from xlutils.copy import copy
import maya.cmds as cmds
import os
import datetime

excel = "" #Excel file path
scene = os.path.splitext(cmds.file(q=True, shortName=True, sceneName=True))[0] #Get the scene name

read = xlrd.open_workbook(excel)

ex_row = 0  #Save which line will be updated

d = datetime.datetime.today() #Get today's date
today = d.strftime("%Y/%m/%d")

sheet_1 = read.sheet_by_index(0)
for col in range(sheet_1.ncols):
    for row in range(sheet_1.nrows):
        if scene == sheet_1.cell(row, col).value:
            ex_row = row
            print u"Start frame:" + sheet_1.cell(row, 1).value + u"End frame:" + sheet_1.cell(row, 2).value
            continue
            
#Output processing, etc. if necessary

write = copy(read)   #Completely copy the object for reading to the object for writing, including the style
sheet1 = write.get_sheet(0)    
sheet1.write(ex_row, 3, today) #Add today's date in the 4th column
write.save(excel)

Use spreadsheet

I referred to this area! http://qiita.com/koyopro/items/d8d56f69f863f07e9378

Basically do the same as Excel above

gspread (0.4.1) httplib2 (0.9.2) oauth2client (4.0.0) requests (2.12.1) six (1.10.0)

import os
import datetime
import maya.cmds as cmds
import gspread
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow


flow = OAuth2WebServerFlow(client_id='Client ID',
                           client_secret='Client secret',
                           scope='https://spreadsheets.google.com/feeds',
                           redirect_uri="urn:ietf:wg:oauth:2.0:oob")
                           
scene = os.path.splitext(cmds.file(q=True, shortName=True, sceneName=True))[0]

d = datetime.datetime.today() #Get today's date
today = d.strftime('%Y/%m/%d')

storage = Storage('cred.dat') #File to store authentication information
credentials = storage.get()
if credentials is None or credentials.invalid == True:
    authorize_url = flow.step1_get_authorize_url()
    cmds.confirmDialog(m='Go to the following link in your browser: ' + authorize_url)
    code = raw_input('Enter verification code: ').strip()
    credential = flow.step2_exchange(code)
    storage.put(credential)
    credential.set_store(storage)
    credentials = credential
    
gc = gspread.authorize(credentials)
spr = gc.open_by_key('Spreadsheet ID')

for j, worksheet in enumerate(spr.worksheets()):
    worksheet.get_all_values()
    list_of_lists = worksheet.get_all_values()
    for i, l in enumerate(list_of_lists):
        if l[0] == scene:
            print u'Start frame:' + l[1] + u'End frame:' + l[2]
            continue
            
for j, worksheet in enumerate(spr.worksheets()):
    worksheet.get_all_values()
    list_of_lists = worksheet.get_all_values()
    for i, l in enumerate(list_of_lists):
        if l[0] == scene:
           worksheet.update_acell('D' + str(i+1), today)

When you access it for the first time spread1.png Because this kind of thing will come out Copy the address and paste it into your browser spread2.png When you press confirm with maya, the following window will appear, so copy and paste the browser code spread3.png From the second time onward, the information in cred.dat will be viewed and accessed, so this work will be done only once.

It has nothing to do with Maya, but the data in the spread sheet I also made something like checking if it is on the company's server and updating the status. If you can do various things from Maya, designers may be able to distribute various tools without dropping Python!

I'm sorry I don't have much to do with Maya Python!

My promotion

If you want to get help from a cat, give me a job! !! !! !! !! !! !! twitter: @ yukarin33

Recommended Posts

By linking Maya with Excel or spreadsheets ~
Excel with Python
Handle Excel with python
3D or D with Py
Read excel with openpyxl
Operate Excel with Python (1)
Operate Excel with Python (2)
Improve your productivity by processing huge Excel files with Python
Scraping desired data from website by linking Python and Excel