FORUMS: list search recent posts

Error in python script for maya

COW Forums : Autodesk Maya

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
Salil Gokhale
Error in python script for maya
on Sep 10, 2017 at 11:23:58 am

There are three files on this link.-- https://drive.google.com/drive/folders/0B2647et2Dal9VEduRmJzTld2RTA?usp=sha...
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





Thanks and regards


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]