[maya]MELからPythonへ変数を渡す際にエンコードを変える MEL to Python Variable

global proc BuildMenuItems(string $menuName){
    python("import buildMenu.buildMenuItems as bm");
	python("import codecs");
	
	python("import sys");
    python("print('sys.getdefaultencoding= '+sys.getdefaultencoding())");
	
	print("$menuName= "+$menuName+"\n");
	string $menuNameA = python("menuNameA='"+$menuName+"'");
	print("$menuNameA= "+$menuNameA+"\n");
    python("print('menuNameA= '+str(menuNameA))");
	
	string $menuNameUTF8 = python("menuNameUTF8='"+$menuName+"'.encode('utf_8')");
	print("$menuNameUTF8= "+$menuNameUTF8+"\n");
    python("print('menuNameUTF8= '+str(menuNameUTF8))");
    
	string $menuNameCP932 = python("menuNameCP932='"+$menuName+"'.encode('cp932')");
	print("$menuNameCP932= "+$menuNameCP932+"\n");
	python("print('menuNameCP932= '+str(menuNameCP932))");
	
	string $menuNameDecodeUTF8 = python("menuNameDecodeUTF8=b'"+$menuName+"'.decode('utf-8')");
	print("$menuNameDecodeUTF8= "+$menuNameDecodeUTF8+"\n");
    python("print('menuNameDecodeUTF8= '+str(menuNameDecodeUTF8))");
    
	string $menuNameDecodeCP932 = python("menuNameDecodeCP932=b'"+$menuName+"'.decode('cp932')");
	print("$menuNameDecodeCP932= "+$menuNameDecodeCP932+"\n");
	python("print('menuNameDecodeCP932= '+str(menuNameDecodeCP932))");
	
    //python("bm.main('u" + $menuName + "')");
    //python("bm.main(menuNameDecodeCP932)");
    python("bm.main()");
}

[maya]エクセルをcsv出力してPythonでその日本語unicode型オブジェクトをcp932で出力すると日本語が表示される。

[maya]エクセルをcsv出力してPythonでその日本語unicode型オブジェクトをcp932で出力すると日本語が表示される。

u3042unicodeオブジェクト
https://www.kabipan.com/computer/python/unicode.html

# -*- coding: cp932 -*-

import pymel.core as pm
import pymel.tools.mel2py as mel2py
import re
import math
import random
workspaceDir=cmds.workspace( q=True, dir=True )
workspaceDir="D:/Projects/mocap_data/"
print("workspaceDir= "+workspaceDir)

csvPath=workspaceDir+"csv_utf8.csv"
csvPath=workspaceDir+"csv.csv"
csvPath_out=workspaceDir+"csv_out.csv"
print("csvPath= "+csvPath)
import sys
import codecs

fin  = codecs.open(csvPath, 'r', 'utf_8')    
    
print("fin= "+str(fin))
s = fin.read()
strData=s.rstrip()
#print("strData= "+strData)

#u3042はunicodeオブジェクト https://www.kabipan.com/computer/python/unicode.html

dict={}
slineArr=strData.split("\r")
slineArr_len=len(slineArr)
print("slineArr_len= "+str(slineArr_len))
print("slineArr= "+str(slineArr))
for line in slineArr:
    csvline_arr=line.split(",")
    print("csvline_arr= "+str(csvline_arr))
    try:
        FMNum= csvline_arr[1]
        #print("FMNum= "+str(FMNum))
        sys.stdout.write("FMNum= "+csvline_arr[1].encode('cp932')+"\n")
        
        motionName=csvline_arr[2]
        sys.stdout.write("motionName= "+csvline_arr[2].encode('cp932')+"\n")
        
        dict[FMNum] = motionName
        #print("motionName= "+str(motionName))
        
    except OSError:
        pass
    
print("dict= "+str(dict))

もっと

# -*- coding: utf-8 -*- 
from maya import cmds 
import pymel.core as pm 


def Simple_Avi_Export():
    topNodes=cmds.ls(assemblies=1,type='transform')
    print("topNodes= "+str(topNodes))
    for node in topNodes:
        try:
            cmds.setAttr(node+".visibility", 0)
        except RuntimeError:
            pass
            
    model=cmds.ls('*:*:model')
    print("model= "+str(model))
    cmds.setAttr(model[0]+".visibility", 1)
    model_Child=cmds.ls(model[0],dag=1)
    for node in model_Child:
        try:
            cmds.setAttr(node+".visibility", 1)
        except RuntimeError:
            pass
    
    #pm.setAttr("magF_Rig:secondaryJointCtrl.visibility", 0)
    cmds.select('export_sets_mot', r=1, ne=1)
    export_sets_mot_childs = cmds.listConnections( 'export_sets_mot', destination=True )
    print("export_sets_mot_childs[0]="+export_sets_mot_childs[0])
    childName=export_sets_mot_childs[0]
    print("childName="+childName)
    
    startF=cmds.getAttr(childName+".startF")
    endF=cmds.getAttr(childName+".endF")
    fbxName=cmds.getAttr(childName+".fbxName")
    exportPath=cmds.getAttr(childName+".exportPath ")
    print startF
    print endF
    print fbxName
    print exportPath
    
    sceneName=cmds.file( q=True, sceneName=True,shortName=True )
    root_ext_pair = os.path.splitext(sceneName)
    sceneNameonly=root_ext_pair[0]
    
    videoOutPath=exportPath+"/"+sceneNameonly+fbxName+".avi"
    videoOutPath="video"+"/"+sceneNameonly+fbxName+".avi"
    print("videoOutPath= "+videoOutPath)
    cmds.playblast(fp=4, clearCache=1, format='avi', sequenceTime=0, showOrnaments=1, percent=50, filename=videoOutPath, viewer=1, startTime=startF, endTime=endF, quality=70, compression="none" ,forceOverwrite=1)
    
#Simple_Avi_Export()


def dirAllOpenAndExport(dir_path):
    import os
    import re
    for current_dir, sub_dirs, files_list in os.walk(dir_path): 
      for file_name in files_list: 
        fullpath=os.path.join(current_dir,file_name)
        fullpath=os.path.abspath(fullpath)
        fullpath=re.sub(r'\\', '/', fullpath)
        print("fullpath= "+fullpath)
        cmds.file(fullpath, ignoreVersion=1, typ="mayaAscii", options="v=0;", o=1, f=1)
        Simple_Avi_Export()

dirAllOpenAndExport("D:/Projects/" )

そのほかの例

# -*- coding: cp932 -*-

import pymel.core as pm
import pymel.tools.mel2py as mel2py
import re
import math
import random
import sys
import codecs

workspaceDir=cmds.workspace( q=True, dir=True )
workspaceDir="D:/Projects/"
print("workspaceDir= "+workspaceDir)
csvPath_out=workspaceDir+"csv_out.csv"
csvPath=workspaceDir+"csv_utf8.csv"
csvPath=workspaceDir+"csv.csv"

print("csvPath= "+csvPath)

fin  = codecs.open(csvPath, 'r', 'utf_8')    
    
print("fin= "+str(fin))
s = fin.read()
strData=s.rstrip()
#print("strData= "+strData)

#u3042はunicodeオブジェクト https://www.kabipan.com/computer/python/unicode.html

dict={}
slineArr=strData.split("\r")
slineArr_len=len(slineArr)
print("slineArr_len= "+str(slineArr_len))
print("slineArr= "+str(slineArr))
for line in slineArr:
    csvline_arr=line.split(",")
    print("csvline_arr= "+str(csvline_arr))
    try:
        FMNum= csvline_arr[1]
        #print("FMNum= "+str(FMNum))
        #sys.stdout.write("FMNum= "+csvline_arr[1].encode('cp932')+"\n")
        
        motionName=csvline_arr[2]
        #sys.stdout.write("motionName= "+csvline_arr[2].encode('cp932')+"\n")
        
        dict[FMNum] = motionName
        #print("motionName= "+str(motionName))
        
    except OSError:
        pass
    
print("dict= "+str(dict))



#---------------------------------------------------------------------------------
# -*- coding: utf-8 -*- 
from maya import cmds 
import pymel.core as pm 


def getKeyFrame_3way():
    
    playbackOptions_query_minTime=int(cmds.playbackOptions(minTime=1, query=1))
    print("playbackOptions_query_minTime= "+str(playbackOptions_query_minTime))
    playbackOptions_query_maxTime=int(cmds.playbackOptions(query=1, maxTime=1))
    print("playbackOptions_query_maxTime= "+str(playbackOptions_query_maxTime))

    selected=cmds.ls(selection=1)

    cmds.select("*")

    findKeyframe_which_first=int(pm.findKeyframe(which='first'))
    print("findKeyframe_which_first= "+str(findKeyframe_which_first))
    findKeyframe_which_last=int(pm.findKeyframe(which='last'))
    print("findKeyframe_which_last= "+str(findKeyframe_which_last))

    cmds.select(selected)
    AllCurveStart=float(AllCurve_Start_or_End(0))
    AllCurveEnd=float(AllCurve_Start_or_End(1))
    print("AllCurveStart= "+str(AllCurveStart))
    print("AllCurveStart= "+str(AllCurveStart))
Start_Or_End=""
#global 
animCurve_string_arr = []
#global 

def AllCurve_Start_or_End(Start_Or_End):
    
    animCurve_string_arr=cmds.ls(type=['animCurveTL', 'animCurveTU', 'animCurveTA', 'animCurveTT'])

    keyframetime_arr=cmds.keyframe(animCurve_string_arr, query=1, timeChange=1)

    old_min=0.0
    old_max=0.0
    for i in range(0,len(keyframetime_arr)):
        if i == 0:
            old_min=keyframetime_arr[i]

            old_max=keyframetime_arr[i]

            
        if keyframetime_arr[i]<old_min:
            old_min=keyframetime_arr[i]
            
        if keyframetime_arr[i]>old_max:
            old_max=keyframetime_arr[i]
            
        
    return_float=0.0

    return_float=old_min
    if Start_Or_End == 0:
        return_float=old_min
        
    else:
        return_float=old_max
        
    return return_float

#getKeyFrame_3way()

#----------------------------------------------------------------------------------


from maya import cmds 
import pymel.core as pm 


def Simple_Avi_Export(mayaPath):
    #onColorMgtOutputEnabledCheckBoxChange colorManagementOutputEnableCheckBoxRenderSettings 0;
    #onColorMgtOutputChanged colorManagementOutputTransformOptionMenuRenderTypeSettings colorManagementOutputTransformOptionMenuRenderSettings colorManagementOutputEnableCheckBoxRenderSettings 0;
    #import maya.mel as mel

    #mel.eval('onColorMgtOutputEnabledCheckBoxChange colorManagementOutputEnableCheckBoxRenderSettings 0;')
    #mel.eval('onColorMgtOutputChanged colorManagementOutputTransformOptionMenuRenderTypeSettings colorManagementOutputTransformOptionMenuRenderSettings colorManagementOutputEnableCheckBoxRenderSettings 0;')
    pm.setAttr("persp.translateX", 327)
    pm.setAttr("persp.translateY", 313)
    pm.setAttr("persp.translateZ", 326)

    pm.setAttr("perspShape.centerOfInterest", 463)
    
    topNodes=cmds.ls(assemblies=1,type='transform')
    print("topNodes= "+str(topNodes))
    for node in topNodes:
        try:
            cmds.setAttr(node+".visibility", 0)
        except RuntimeError:
            pass
            
    model=cmds.ls('*:model')
    print("model= "+str(model))
    cmds.setAttr(model[0]+".visibility", 1)
    model_Child=cmds.ls(model[0],dag=1)
    for node in model_Child:
        try:
            cmds.setAttr(node+".visibility", 1)
        except RuntimeError:
            pass
    
    #pm.setAttr("magF_Rig:secondaryJointCtrl.visibility", 0)
    
    startF=int(cmds.playbackOptions(minTime=1, query=1))
    print("startF= "+str(startF))
    endF=int(cmds.playbackOptions(query=1, maxTime=1))
    print("endF= "+str(endF))
    
    #sceneName=cmds.file( q=True, sceneName=True,shortName=True )
    sceneName = os.path.basename(mayaPath)
    root_ext_pair = os.path.splitext(sceneName)
    sceneNameonly=root_ext_pair[0]
    
    #mayaPath=cmds.file( q=True, sceneName=True,shortName=False )
    print("mayaPath= "+mayaPath)
    mayaPath_Arr=mayaPath.split("/")
    mayaPath_Arr_len=len(mayaPath_Arr)
    last1dirName=mayaPath_Arr[mayaPath_Arr_len-2]
    print("last1dirName= "+last1dirName)
    last2dirName=mayaPath_Arr[mayaPath_Arr_len-3]
    print("last2dirName= "+last2dirName)
    
    motionName=""
    if(sceneNameonly in dict):
        motionName=dict[sceneNameonly]
        sys.stdout.write("motionName= "+motionName.encode('cp932')+"\n")
    
        motionName932=motionName.encode('cp932')
        print("motionName932= "+motionName932)
        #motionNameUTF8=motionName.encode('utf_8')
        #print("motionNameUTF8= "+motionNameUTF8)
    videoOutPath="/"+sceneNameonly+".avi"
    videoOutPath=u"D:/Projects/mocap_data"+"/"+last2dirName+"/"+sceneNameonly+u"__"+motionName+u"__"+u".avi"
    print("videoOutPath= "+videoOutPath)
    cmds.playblast(fp=4, clearCache=1, format='avi', sequenceTime=0, showOrnaments=1, percent=50, filename=videoOutPath, viewer=1, startTime=startF, endTime=endF, quality=70, compression="none" ,forceOverwrite=1)
    
#Simple_Avi_Export()


def dirAllOpenAndExport(dir_path):
    import os
    import re
    for current_dir, sub_dirs, files_list in os.walk(dir_path): 
      for file_name in files_list: 
        fullpath=os.path.join(current_dir,file_name)
        fullpath=os.path.abspath(fullpath)
        fullpath=re.sub(r'\\', '/', fullpath)
        print("fullpath= "+fullpath)
        
        root_ext_pair = os.path.splitext(fullpath)
        ExtentionOnly=root_ext_pair[1]
        if(ExtentionOnly==".ma"):
            
            #cmds.file(fullpath, ignoreVersion=1, typ="mayaAscii", options="v=0;", o=1, f=1)
            cmds.file(new=1, f=1)
            cmds.file(fullpath, ignoreVersion=1, type="mayaAscii", namespace="ref", r=1, gl=1, mergeNamespacesOnClash=False, options="v=0;")
            
            Simple_Avi_Export(fullpath)
dirAllOpenAndExport("D:/Projects/mocap_data/" )