Error in python script for midi files

Error in python script for midi files

Anonymous
Not applicable
4,304 Views
11 Replies
Message 1 of 12

Error in python script for midi files

Anonymous
Not applicable

There are three files attached.

THE .gif FILES ARE ACTUALLY .py FILES (.py is not a supported extension on this forum)

When I try to run the"Piano LR.py' after importing 'Grand LR.mb'

I get the following output

 

import sys
import math
#sys.path.append('D:/My MAYA Files/midi piano Maya')
#put the midiparser.py file in X:/Program Files/Autodesk/Maya2011/Python/lib so you don't need to specify the path explicitly
import midiparser
import pymel.core as pm
start_note="C,1"
end_note="E,8"
note_list = ['C','D','E','F','G','A','B']
sharp_note='S'
fps=24
offset=10
ch_colors=[[0, 1, 0], [1, 0, 0]]		#Color(R,G,B) for Right and Left Channel

cur_channel=0;
def enum_keys():

    start_octave= int(start_note.split(',')[1])
    end_octave= int(end_note.split(',')[1])
    end_octave_note=end_note.split(',')[0]
    for i in range(start_octave, end_octave+1):
        if i!=end_octave:
            for j in range(0,7):
                assign_texture (note_list[j]+'_'+ repr(i))
                if j==0 or j==1 or j==3 or j==4 or j==5 :
                    assign_texture (note_list[j]+'_'+ repr(i)+'S')
        else:
            for j in range(0,7):
                if note_list[j]!=end_octave_note:
                    assign_texture (note_list[j]+'_'+ repr(i))
                    if j==0 or j==1 or j==3 or j==4 or j==5 :
                        assign_texture (note_list[j]+'_'+ repr(i)+'S')   
                else:           
                     assign_texture (note_list[j]+'_'+ repr(i))
                     break      
    return


def getNote (note):
	octave=0

	temp=0
	if note>=0 and note<=11 :
		octave=0
	if note>11 and note<=23 :
		octave=1
	if note>23 and note<=35 :
		octave=2
	if note>35 and note<=47 :
		octave=3
	if note>47 and note<=59 :
		octave=4
	if note>59 and note<=71 :
		octave=5
	if note>71 and note<=83 :
		octave=6
	if note>83 and note<=95 :
		octave=7
	if note>95 and note<=107 :
		octave=8
	if note>107 and note<=119 :
		octave=9
	if note>110 and note<=127 :
		octave=10

	temp=note-((11*octave)+(octave-1))
	
	if temp==1:
		return note_list[0]+'_' + repr(octave)
	if temp==2:
		return note_list[0]+'_' + repr(octave) + sharp_note
	if temp==3:
		return note_list[1]+'_' + repr(octave)
	if temp==4:
		return note_list[1]+'_' + repr(octave) + sharp_note
	if temp==5:
		return note_list[2]+'_' + repr(octave)
	if temp==6:
		return note_list[3]+'_' + repr(octave)
	if temp==7:
		return note_list[3]+'_' + repr(octave) + sharp_note
	if temp==8:
		return note_list[4]+'_' + repr(octave)
	if temp==9:
		return note_list[4]+'_' + repr(octave) + sharp_note
	if temp==10:
		return note_list[5]+'_' + repr(octave)
	if temp==11:
		return note_list[5]+'_' + repr(octave) + sharp_note
	if temp==12:
		return note_list[6]+'_' + repr(octave)

def keypress(obj_name,fnum,vel):
    if vel>0:
        temp=int(math.ceil(127/vel))
        if len(obj_name)>3:				#It is a Black Key
            pm.setKeyframe( "phong"+obj_name, attribute='colorR', v=0.073167,t=fnum-temp)
            pm.setKeyframe( "phong"+obj_name, attribute='colorG', v=0.073167,t=fnum-temp)
            pm.setKeyframe( "phong"+obj_name, attribute='colorB', v=0.073167,t=fnum-temp)
            pm.setKeyframe( "phong"+obj_name, attribute='colorR', v=ch_colors[cur_channel][0],t=fnum)
            pm.setKeyframe( "phong"+obj_name, attribute='colorG', v=ch_colors[cur_channel][1],t=fnum)
            pm.setKeyframe( "phong"+obj_name, attribute='colorB', v=ch_colors[cur_channel][2],t=fnum)	
        else:
            pm.setKeyframe( "phong"+obj_name, attribute='colorR', v=1,t=fnum-temp)
            pm.setKeyframe( "phong"+obj_name, attribute='colorG', v=1,t=fnum-temp)
            pm.setKeyframe( "phong"+obj_name, attribute='colorB', v=1,t=fnum-temp)
            pm.setKeyframe( "phong"+obj_name, attribute='colorR', v=ch_colors[cur_channel][0],t=fnum)
            pm.setKeyframe( "phong"+obj_name, attribute='colorG', v=ch_colors[cur_channel][1],t=fnum)
            pm.setKeyframe( "phong"+obj_name, attribute='colorB', v=ch_colors[cur_channel][2],t=fnum)			
        pm.setKeyframe( obj_name, attribute='rotateX', v=0,t=fnum-temp)
        pm.setKeyframe( obj_name, attribute='rotateX', v=-4.5,t=fnum)
    elif vel == 0:
        if len(obj_name)>3:				#It is a Black Key
            pm.setKeyframe( "phong"+obj_name, attribute='colorR', v=ch_colors[cur_channel][0],t=fnum-1)
            pm.setKeyframe( "phong"+obj_name, attribute='colorG', v=ch_colors[cur_channel][1],t=fnum-1)
            pm.setKeyframe( "phong"+obj_name, attribute='colorB', v=ch_colors[cur_channel][2],t=fnum-1)	
            pm.setKeyframe( "phong"+obj_name, attribute='colorR', v=0.073167,t=fnum+2)
            pm.setKeyframe( "phong"+obj_name, attribute='colorG', v=0.073167,t=fnum+2)
            pm.setKeyframe( "phong"+obj_name, attribute='colorB', v=0.073167,t=fnum+2)			
        else:
            pm.setKeyframe( "phong"+obj_name, attribute='colorR', v=ch_colors[cur_channel][0],t=fnum-1)
            pm.setKeyframe( "phong"+obj_name, attribute='colorG', v=ch_colors[cur_channel][1],t=fnum-1)
            pm.setKeyframe( "phong"+obj_name, attribute='colorB', v=ch_colors[cur_channel][2],t=fnum-1)	
            pm.setKeyframe( "phong"+obj_name, attribute='colorR', v=1,t=fnum+2)
            pm.setKeyframe( "phong"+obj_name, attribute='colorG', v=1,t=fnum+2)
            pm.setKeyframe( "phong"+obj_name, attribute='colorB', v=1,t=fnum+2)
        pm.setKeyframe( obj_name, attribute='rotateX', v=-4.5,t=fnum-1)
        pm.setKeyframe( obj_name, attribute='rotateX', v=0,t=fnum+2)
    return

def assign_texture(obj_name):
	if len(obj_name)>3:				#It is a Black Key
		key = pm.shadingNode("phong", asShader=True, name ="phong"+obj_name)
		pm.setAttr(key + ".color", 0.073167, 0.073167, 0.073167)
		pm.setAttr(key + ".ambientColor", 0.04065, 0.04065, 0.04065)
		pm.setAttr(key + ".incandescence", 0.0487831, 0.0487831, 0.0487831)
		pm.setAttr(key + ".diffuse",0.8)
		pm.setAttr(key + ".translucence",0)
		pm.setAttr(key + ".specularColor", 0.105684, 0.105684, 0.105684)
		pm.setAttr(key + ".reflectivity", 0.5)
		pm.setAttr(key + ".reflectedColor", 0.0894331, 0.0894331, 0.0894331)		
		pm.sets( renderable=True, noSurfaceShader=True, empty=True, name="shader"+obj_name )
		# Connect material to shader
		pm.connectAttr("phong"+obj_name+".outColor", "shader"+obj_name+".surfaceShader")
		pm.sets("shader"+obj_name, edit=True, forceElement=obj_name)	
	else:
		key = pm.shadingNode("phong", asShader=True, name ="phong"+obj_name)
		pm.setAttr(key + ".color", 1,1,1)
		pm.setAttr(key + ".ambientColor", 0.0569162, 0.0569162, 0.0569162)
		pm.setAttr(key + ".incandescence", 0.195117, 0.195117, 0.195117)
		pm.setAttr(key + ".diffuse",0.8)
		pm.setAttr(key + ".translucence",0)
		pm.setAttr(key + ".specularColor", 0,0,0)
		pm.setAttr(key + ".reflectivity", 0.5)
		pm.setAttr(key + ".reflectedColor", 0.0243839, 0.0243839, 0.0243839)	
		pm.sets( renderable=True, noSurfaceShader=True, empty=True, name="shader"+obj_name )
		# Connect material to shader
		pm.connectAttr("phong"+obj_name+".outColor", "shader"+obj_name+".surfaceShader")
		pm.sets("shader"+obj_name, edit=True, forceElement=obj_name)		
	return
	
def keypress1(obj_name,fnum,vel):

    filename = "D:/My MAYA Files/midi piano Maya/midi_anim.txt"
    print "Writing to file: %s" % filename
    file = open(filename, 'w')
    file.write(obj_name+str(fnum)+str(vel))
    file.close()
    return    
    
def parse_midi(fname):
    midi = midiparser.File(fname)
    tempo=0
    timebase=midi.division
    frameno=0
    maxtime=0
    global cur_channel
    cur_channel=-2
    for track in midi.tracks:
	for event in track.events:
	    if event.type == midiparser.voice.NoteOn:
		if event.absolute>maxtime:
		    maxtime=event.absolute
	    if event.type == midiparser.meta.SetTempo: 
		tempo=event.detail.tempo			    
		    
	numframes=int(math.ceil(maxtime * tempo*fps / timebase / 1000000) + offset)+10
	pm.playbackOptions( minTime='1', maxTime=numframes, animationEndTime=numframes, animationStartTime='1')
	print numframes
    for track in midi.tracks:
	for event in track.events:
	    if event.type == midiparser.voice.NoteOn:
	        frameno=int(math.ceil(event.absolute * tempo*fps / timebase / 1000000) + offset)
		keypress(getNote(event.detail.note_no),frameno,event.detail.velocity)
	    if event.type == midiparser.meta.TrackName: 
	    #if event.detail.text.strip!='untitled':
		cur_channel=cur_channel+1#print event.detail.text.strip()
	
    return     

enum_keys()	#Assign Materials to the Keys

fname=pm.fileDialog( directoryMask='*.mid' )

if fname == None :
    print 'No file choosen'
else :
    parse_midi(fname)	

# Error: MayaNodeError: Maya Node does not exist (or is not unique):: u'C_1' # 

How do I rectify the code?

 

Can anyone please post the correct script.

The script is designed to animate a piano using a midi file 

 

See this video for more details https://www.youtube.com/watch?v=9Cpn2_n3qoQ

 

Thanks and regards

0 Likes
4,305 Views
11 Replies
Replies (11)
Message 2 of 12

TwiliChaos
Participant
Participant

Before coming across this thread I spent a few hours trying to fix the code. In every case I tried to test it  I imported the scene when I SHOULD have opened it normally. The reason there's an error thrown is because there is no node called u'C_1' but there is more certainly a node called u'Grand_LR_C_1' There is nothing wrong with the code, just user error.


So either you open it normally and it'll work just fine, or if you want/need to import the file instead, go to Import Options > Namespace Options > Resolve [Clashing Nodes]. The other options do not matter for this.

 

Anyways I'm happy that I got it to work, so I can get to MIDI animating this thing like I've wanted to do for a while.

 

Tested using Maya 2016

0 Likes
Message 3 of 12

Anonymous
Not applicable

Please HELP, after opening the file instead of importing, I get this error:

 

# Error: TypeError: file /Users/Shared/Autodesk/ApplicationAddins/MayaBonusTools-2015-2018/Contents/python-2018/midiparser.py line 303: exceptions must be old-style classes or derived from BaseException, not str # 

0 Likes
Message 4 of 12

haggi_master
Advocate
Advocate

Well, the problem lies in the midiparser.py. You will need to correct this file near line 303. Or show the file here an we can help.

0 Likes
Message 5 of 12

haggi_master
Advocate
Advocate

Okay, I found a version of the midiparser.py which shows exactly at line 303 this line:

 

raise "Unknown event."

If you replace it with:

 

raise BaseException, "Something's wrong"

It should work better.

0 Likes
Message 6 of 12

Anonymous
Not applicable

Hi,

I am still getting the typeError...

Maya 2018 uses Python 2.7.

0 Likes
Message 7 of 12

haggi_master
Advocate
Advocate

Did you reload the file or restart maya after the change? Maybe you could post the file here so we can have a look at it.

0 Likes
Message 8 of 12

Anonymous
Not applicable

Hi There, I have uploaded the files in a zip.

I had restarted Maya and my machine after changing the code.

 

 

_________

 

MacBook Pro (Retina, 13-inch, Early 2015)

0 Likes
Message 9 of 12

haggi_master
Advocate
Advocate

In the midiparster.py file a single line around line 10 or so has the content "String". That's nonsense. Delete it.

On my machine I was able to load both scripts and execute them without problems after this change. Well I have no midifile so I could not check it completetly. (Maya2015/Maya2018)

 

0 Likes
Message 10 of 12

Anonymous
Not applicable

Hi There, 

 

I did delete the file, but I think i forgot to mention that the error comes after I load a midi file.


I have will attach a midi file now so you can try on your side.

 

 Thanks

0 Likes
Message 11 of 12

haggi_master
Advocate
Advocate

I'll have a look as soon as possible.

0 Likes
Message 12 of 12

haggi_master
Advocate
Advocate

Sorry, there seems to be a script error which needs more investigation. It would need a deeper understanding what's going on here, sorry I cannot help here. The error occurs at the end of the file loading where a function tries to parse a string at a wrong index.

0 Likes