Paste Code
Paste Blends
Paste Images
bl_info = {
"name": "Music Player",
"author": "edddy <edddy74@live.fr> + nikitron.cc.ua a little",
"version": (0, 1, 120101),
"blender": (2, 59, 0),
"api": 34074,
"location": "View3D > Tool Shelf > Music Player",
"description": "A Little Music Player for Blender",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Misc"}

from bpy_extras.io_utils import ImportHelper
from bpy.props import *
import bpy, aud, time, threading
import re
import urllib.request as req

#itemdefault = req.urlopen('http://mp3vega.com/down-v1/4770v4/7a374117a2fd/u52089946/audios/testovaya_muzyka_melodiya_super_-_angel_skoro_budet_moya_1_pesnyaot_k_angel%20MP3VEGA.COM.mp3').read()
#bpy.context.window_manager.playlist.append(itemdefault)
bpy.types.WindowManager.show_names = bpy.props.BoolProperty(default=False, name='show_names', description='show playlist')
bpy.types.WindowManager.playlist_names = []

def volume_up(self, context):
try:
context.window_manager.playsound.volume=context.window_manager.volume
except:
pass

def soundIsOn(context):
try:
while context.window_manager.playsound.status:
time.sleep(0.001)
if context.window_manager.index+1 < context.window_manager.playlist.__len__():
context.window_manager.index += 1
bpy.ops.sound.play()
else:
context.window_manager.index = 0
except:
pass

def playlistprint():
pl = bpy.context.window_manager.playlist
print ('Playlist: \n')
for i, p in enumerate(pl):
print (str(i+1)+ '.', p)
return


class PlaySIC(bpy.types.Operator):
'''Play a sound File'''
bl_idname = "sound.play"
bl_label = "Play sound"

item_play = bpy.props.StringProperty(name="item", default='[False, 0]')

@classmethod
def poll(cls, context):
try:
if context.window_manager.playsound.status or not context.window_manager.playlist.__len__():
return 0
else:
return 1
except:
if context.window_manager.playlist.__len__():
return 1
else:
return 0

def execute(self, context):
check = eval(self.item_play)
if check[0]:
bpy.context.window_manager.index = check[1]
check[0] = False
bpy.types.WindowManager.f = aud.Factory(context.window_manager.playlist[context.window_manager.index])
bpy.types.WindowManager.playsound = context.window_manager.d.play(context.window_manager.f)
context.window_manager.pause=False
context.window_manager.playsound.volume=context.window_manager.volume
threading.Thread(target=soundIsOn, args=(context,)).start()
#self.report({'INFO'}, "||| %s of %s ||| %s" % (str(context.window_manager.index+1), str(len(context.window_manager.playlist)), str(context.window_manager.playlist[context.window_manager.index])))

if bpy.context.window_manager.playlist_names:
pl = bpy.context.window_manager.playlist_names
else:
pl = bpy.context.window_manager.playlist
print ("||| %s of %s ||| %s" % (str(context.window_manager.index+1), str(len(context.window_manager.playlist)), str(pl[context.window_manager.index])))
return {'FINISHED'}


class ImportSIC(bpy.types.Operator):
'''Load a sound File'''
bl_idname = "sound.import"
bl_label = "Import sound"

filename_ext = ".mp3"
filter_glob = StringProperty(default="*.mp3;*.ogg;*.wav;*.avi;*.mp4;*.wma", options={'HIDDEN'})
filepath = StringProperty(subtype="FILE_PATH")
filename = StringProperty()
files = CollectionProperty(name="File Path",type=bpy.types.OperatorFileListElement)
directory = StringProperty(subtype='DIR_PATH')


def execute(self, context):
print(self.filename)
if self.files :
for sfile in self.files:
context.window_manager.playlist.append(self.directory + sfile.name)
context.window_manager.playlist_names.append(sfile.name)
playlistprint()
else:
context.window_manager.playlist.append(self.filepath)
return {'FINISHED'}

def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}

class ImportM3U(bpy.types.Operator, ImportHelper):
'''Load a M3U File'''
bl_idname = "sound.import_m3u"
bl_label = "Import m3u"

filename_ext = ".m3u"
filter_glob = StringProperty(default="*.m3u;*.M3U", options={'HIDDEN'})

def execute(self, context):
f = open(self.filepath)
for l in f :
if not l[0]=='#':
context.window_manager.playlist.append(l[0:-1])
f.close()
del f
playlistprint()
return {'FINISHED'}

class StopSIC(bpy.types.Operator):
'''stop sound load'''
bl_idname = "sound.stop"
bl_label = "stop sound"

@classmethod
def poll(cls, context):
try:
return (context.window_manager.playsound.status)
except:
return 0

def execute(self, context):

context.window_manager.playsound.stop()
context.window_manager.index = context.window_manager.playlist.__len__()

return {'FINISHED'}

class DelList(bpy.types.Operator):
'''Delet play list'''
bl_idname = "sound.delplaylist"
bl_label = "Del play list"

@classmethod
def poll(cls, context):
return (context.window_manager.playlist.__len__())

def execute(self, context):

bpy.types.WindowManager.playlist=[]
bpy.types.WindowManager.playlist_names=[]
bpy.types.WindowManager.playlist_print=[]

return {'FINISHED'}

class NextSIC(bpy.types.Operator):
'''Next sound'''
bl_idname = "sound.next"
bl_label = "next sound"

@classmethod
def poll(cls, context):
try:
return (context.window_manager.playsound.status and context.window_manager.index+1 < context.window_manager.playlist.__len__())
except:
return 0

def execute(self, context):

context.window_manager.playsound.stop()

return {'FINISHED'}

class PrevSIC(bpy.types.Operator):
'''Previus sound load'''
bl_idname = "sound.prev"
bl_label = "previus sound"

@classmethod
def poll(cls, context):
if context.window_manager.playlist.__len__():
return (context.window_manager.index)
else:
return 0

def execute(self, context):

context.window_manager.index-=2
context.window_manager.playsound.stop()

return {'FINISHED'}

class PauseSIC(bpy.types.Operator):
'''pause sound load'''
bl_idname = "sound.pause"
bl_label = "pause sound"

@classmethod
def poll(cls, context):
try:
return (context.window_manager.playsound.status)
except:
return 0

def execute(self, context):

context.window_manager.playsound.pause()
context.window_manager.pause=True
return {'FINISHED'}

class ResumeSIC(bpy.types.Operator):
'''resume sound load'''
bl_idname = "sound.resume"
bl_label = "resume sound"

def execute(self, context):

context.window_manager.playsound.resume()
context.window_manager.pause=False
return {'FINISHED'}

class PrintPlaylist(bpy.types.Operator):
'''Print playlist'''
bl_idname = "sound.printplaylist"
bl_label = "print playlist"

def execute(self, context):
pl = context.window_manager.playlist
print ('Playlist: \n')
for i, p in enumerate(pl):
print (str(i+1)+ '.', p)
return {'FINISHED'}


class VIEW3D_PT_Musicplayer(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_label = "Music Player"
bl_category = 'NT'



def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.operator("sound.import", text="Import sound", icon='FILE_SOUND')
col.operator("sound.import_m3u", text="Import m3u", icon='ZOOMIN')
row = col.row()
if context.window_manager.pause:
row.operator("sound.resume", text="Resume", icon='PLAY')
else:
row.operator("sound.pause", text="Pause", icon='PAUSE')
row.operator("sound.stop", text="Stop", icon='CANCEL')
row = col.row()
row.operator("sound.prev", text="Previus", icon='REW')
row.operator("sound.next", text="Next", icon='FF')
col.operator("sound.play", text="Play", icon='PLAY')
col.operator("sound.delplaylist", text="Clear play list", icon='CANCEL')
#col.operator("sound.printplaylist", text="Print playlist", icon='TEXT')
col.prop(context.window_manager, "volume", slider=True)
col.prop(bpy.context.window_manager, 'show_names', text='show playlist')
#col.prop(context.window_manager.f.limit)
#plaingnow = context.window_manager.playlist[context.window_manager.index]
#playitemgroup = re.match(r'(\w+)', plaingnow) # - need to be pattern for filename for linux/windows exception /\ in full path. So all after /\/ needed to be in group(1) matching
#playitem = playitemgroup.group(1)
plaingindex = 'Song index: '+str(context.window_manager.index+1)+'/'+str(len(context.window_manager.playlist))
col.label(text=plaingindex)


if bpy.context.window_manager.playlist_names:
playlist_print=context.window_manager.playlist_names
else:
playlist_print=context.window_manager.playlist
if bpy.context.window_manager.show_names:
box = layout.box()
i=0
for p in playlist_print:
i+=1
#col.operator('sound.play', bpy.context.window_manager.index=i-1, text=str(i)+' '+str(p))
col = box.column(align=True)
col.operator("sound.play", text=str(i)+' | '+str(p)).item_play=str([True, i-1])
#a = context.window_manager
#a.progress_begin(0,1)
#a.progress_update(0.5)
#a.progress_end()


# define classes for registration
classes = [PlaySIC,
ImportSIC,
ImportM3U,
StopSIC,
DelList,
NextSIC,
PrevSIC,
PauseSIC,
ResumeSIC,
VIEW3D_PT_Musicplayer,
PrintPlaylist]


# registering
def register():
for c in classes:
bpy.utils.register_class(c)
bpy.types.WindowManager.playlist=[]
bpy.types.WindowManager.index=bpy.props.IntProperty()
bpy.types.WindowManager.pause = bpy.props.BoolProperty(False)
bpy.types.WindowManager.volume = bpy.props.FloatProperty(name="Volume",default=1.0, min=0.0, max=1.0, update=volume_up)
bpy.types.WindowManager.d = aud.device()


# unregistering
def unregister():
for c in classes:
bpy.utils.unregister_class(c)
try:
del bpy.types.WindowManager.index
del bpy.types.WindowManager.playlist
del bpy.types.WindowManager.pause
del bpy.types.WindowManager.volume
del bpy.types.WindowManager.d
except:
pass
try:
del bpy.types.WindowManager.f
del bpy.types.WindowManager.playsound
del bpy.types.WindowManager.show_names
except:
pass

if __name__ == "__main__":
register()

  1. bl_info = {
  2.     "name": "Music Player",
  3.     "author": "edddy <edddy74@live.fr> + nikitron.cc.ua a little",
  4.     "version": (0, 1, 120101),
  5.     "blender": (2, 59, 0),
  6.     "api": 34074,
  7.     "location": "View3D > Tool Shelf > Music Player",
  8.     "description": "A Little Music Player for Blender",
  9.     "warning": "",
  10.     "wiki_url": "",
  11.     "tracker_url": "",
  12.     "category": "Misc"}
  13.  
  14. from bpy_extras.io_utils import ImportHelper
  15. from bpy.props import *
  16. import bpy, aud, time, threading
  17. import re
  18. import urllib.request as req
  19.  
  20. #itemdefault = req.urlopen('http://mp3vega.com/down-v1/4770v4/7a374117a2fd/u52089946/audios/testovaya_muzyka_melodiya_super_-_angel_skoro_budet_moya_1_pesnyaot_k_angel%20MP3VEGA.COM.mp3').read()
  21. #bpy.context.window_manager.playlist.append(itemdefault)
  22. bpy.types.WindowManager.show_names = bpy.props.BoolProperty(default=False, name='show_names', description='show playlist')
  23. bpy.types.WindowManager.playlist_names = []
  24.  
  25. def volume_up(self, context):
  26.     try:
  27.         context.window_manager.playsound.volume=context.window_manager.volume
  28.     except:
  29.         pass
  30.  
  31. def soundIsOn(context):
  32.     try:
  33.         while context.window_manager.playsound.status:
  34.             time.sleep(0.001)
  35.         if context.window_manager.index+1 < context.window_manager.playlist.__len__():
  36.             context.window_manager.index += 1
  37.             bpy.ops.sound.play()
  38.         else:
  39.             context.window_manager.index = 0
  40.     except:
  41.         pass
  42.  
  43. def playlistprint():
  44.     pl = bpy.context.window_manager.playlist
  45.     print ('Playlist: \n')
  46.     for i, p in enumerate(pl):
  47.         print (str(i+1)+ '.', p)
  48.     return
  49.  
  50.  
  51. class PlaySIC(bpy.types.Operator):
  52.     '''Play a sound File'''
  53.     bl_idname = "sound.play"
  54.     bl_label = "Play sound"
  55.    
  56.     item_play = bpy.props.StringProperty(name="item", default='[False, 0]')
  57.    
  58.     @classmethod
  59.     def poll(cls, context):
  60.         try:
  61.             if context.window_manager.playsound.status or not context.window_manager.playlist.__len__():
  62.                 return 0
  63.             else:
  64.                 return 1
  65.         except:
  66.             if context.window_manager.playlist.__len__():
  67.                 return 1
  68.             else:
  69.                 return 0
  70.  
  71.     def execute(self, context):
  72.         check = eval(self.item_play)
  73.         if check[0]:
  74.             bpy.context.window_manager.index = check[1]
  75.             check[0] = False
  76.         bpy.types.WindowManager.f = aud.Factory(context.window_manager.playlist[context.window_manager.index])
  77.         bpy.types.WindowManager.playsound = context.window_manager.d.play(context.window_manager.f)
  78.         context.window_manager.pause=False
  79.         context.window_manager.playsound.volume=context.window_manager.volume
  80.         threading.Thread(target=soundIsOn, args=(context,)).start()
  81.         #self.report({'INFO'}, "||| %s of %s ||| %s" % (str(context.window_manager.index+1), str(len(context.window_manager.playlist)), str(context.window_manager.playlist[context.window_manager.index])))
  82.        
  83.         if bpy.context.window_manager.playlist_names:
  84.             pl = bpy.context.window_manager.playlist_names
  85.         else:
  86.             pl = bpy.context.window_manager.playlist
  87.         print ("||| %s of %s ||| %s" % (str(context.window_manager.index+1), str(len(context.window_manager.playlist)), str(pl[context.window_manager.index])))
  88.         return {'FINISHED'}
  89.  
  90.  
  91. class ImportSIC(bpy.types.Operator):
  92.     '''Load a sound File'''
  93.     bl_idname = "sound.import"
  94.     bl_label = "Import sound"
  95.  
  96.     filename_ext = ".mp3"
  97.     filter_glob = StringProperty(default="*.mp3;*.ogg;*.wav;*.avi;*.mp4;*.wma", options={'HIDDEN'})
  98.     filepath = StringProperty(subtype="FILE_PATH")
  99.     filename = StringProperty()
  100.     files = CollectionProperty(name="File Path",type=bpy.types.OperatorFileListElement)
  101.     directory = StringProperty(subtype='DIR_PATH')
  102.  
  103.    
  104.     def execute(self, context):
  105.         print(self.filename)
  106.         if self.files :
  107.             for sfile in self.files:
  108.                 context.window_manager.playlist.append(self.directory + sfile.name)
  109.                 context.window_manager.playlist_names.append(sfile.name)
  110.             playlistprint()
  111.         else:
  112.             context.window_manager.playlist.append(self.filepath)
  113.         return {'FINISHED'}
  114.  
  115.     def invoke(self, context, event):
  116.         context.window_manager.fileselect_add(self)
  117.         return {'RUNNING_MODAL'}
  118.  
  119. class ImportM3U(bpy.types.Operator, ImportHelper):
  120.     '''Load a M3U File'''
  121.     bl_idname = "sound.import_m3u"
  122.     bl_label = "Import m3u"
  123.    
  124.     filename_ext = ".m3u"
  125.     filter_glob = StringProperty(default="*.m3u;*.M3U", options={'HIDDEN'})
  126.    
  127.     def execute(self, context):
  128.         f = open(self.filepath)
  129.         for l in f :
  130.             if not l[0]=='#':
  131.                 context.window_manager.playlist.append(l[0:-1])
  132.         f.close()
  133.         del f
  134.         playlistprint()
  135.         return {'FINISHED'}
  136.  
  137. class StopSIC(bpy.types.Operator):
  138.     '''stop sound load'''
  139.     bl_idname = "sound.stop"
  140.     bl_label = "stop sound"
  141.  
  142.     @classmethod
  143.     def poll(cls, context):
  144.         try:
  145.             return (context.window_manager.playsound.status)
  146.         except:
  147.             return 0
  148.  
  149.     def execute(self, context):
  150.        
  151.         context.window_manager.playsound.stop()
  152.         context.window_manager.index = context.window_manager.playlist.__len__()
  153.        
  154.         return {'FINISHED'}
  155.  
  156. class DelList(bpy.types.Operator):
  157.     '''Delet play list'''
  158.     bl_idname = "sound.delplaylist"
  159.     bl_label = "Del play list"
  160.  
  161.     @classmethod
  162.     def poll(cls, context):
  163.         return (context.window_manager.playlist.__len__())
  164.  
  165.     def execute(self, context):
  166.        
  167.         bpy.types.WindowManager.playlist=[]
  168.         bpy.types.WindowManager.playlist_names=[]
  169.         bpy.types.WindowManager.playlist_print=[]
  170.        
  171.         return {'FINISHED'}
  172.  
  173. class NextSIC(bpy.types.Operator):
  174.     '''Next sound'''
  175.     bl_idname = "sound.next"
  176.     bl_label = "next sound"
  177.  
  178.     @classmethod
  179.     def poll(cls, context):
  180.         try:
  181.             return (context.window_manager.playsound.status and context.window_manager.index+1 < context.window_manager.playlist.__len__())
  182.         except:
  183.             return 0
  184.  
  185.     def execute(self, context):
  186.        
  187.         context.window_manager.playsound.stop()
  188.        
  189.         return {'FINISHED'}
  190.  
  191. class PrevSIC(bpy.types.Operator):
  192.     '''Previus sound load'''
  193.     bl_idname = "sound.prev"
  194.     bl_label = "previus sound"
  195.  
  196.     @classmethod
  197.     def poll(cls, context):
  198.         if context.window_manager.playlist.__len__():
  199.             return (context.window_manager.index)
  200.         else:
  201.             return 0
  202.  
  203.     def execute(self, context):
  204.        
  205.         context.window_manager.index-=2
  206.         context.window_manager.playsound.stop()
  207.        
  208.         return {'FINISHED'}
  209.  
  210. class PauseSIC(bpy.types.Operator):
  211.     '''pause sound load'''
  212.     bl_idname = "sound.pause"
  213.     bl_label = "pause sound"
  214.  
  215.     @classmethod
  216.     def poll(cls, context):
  217.         try:
  218.             return (context.window_manager.playsound.status)
  219.         except:
  220.             return 0
  221.  
  222.     def execute(self, context):
  223.        
  224.         context.window_manager.playsound.pause()
  225.         context.window_manager.pause=True
  226.         return {'FINISHED'}      
  227.  
  228. class ResumeSIC(bpy.types.Operator):
  229.     '''resume sound load'''
  230.     bl_idname = "sound.resume"
  231.     bl_label = "resume sound"
  232.  
  233.     def execute(self, context):
  234.        
  235.         context.window_manager.playsound.resume()
  236.         context.window_manager.pause=False
  237.         return {'FINISHED'}
  238.  
  239. class PrintPlaylist(bpy.types.Operator):
  240.     '''Print playlist'''
  241.     bl_idname = "sound.printplaylist"
  242.     bl_label = "print playlist"
  243.  
  244.     def execute(self, context):
  245.         pl = context.window_manager.playlist
  246.         print ('Playlist: \n')
  247.         for i, p in enumerate(pl):
  248.             print (str(i+1)+ '.', p)
  249.         return {'FINISHED'}
  250.  
  251.  
  252. class VIEW3D_PT_Musicplayer(bpy.types.Panel):
  253.     bl_space_type = 'VIEW_3D'
  254.     bl_region_type = 'TOOLS'
  255.     bl_label = "Music Player"
  256.     bl_category = 'NT'
  257.    
  258.    
  259.    
  260.     def draw(self, context):
  261.         layout = self.layout
  262.         col = layout.column(align=True)
  263.         col.operator("sound.import", text="Import sound", icon='FILE_SOUND')
  264.         col.operator("sound.import_m3u", text="Import m3u", icon='ZOOMIN')
  265.         row = col.row()
  266.         if context.window_manager.pause:
  267.             row.operator("sound.resume", text="Resume", icon='PLAY')
  268.         else:
  269.             row.operator("sound.pause", text="Pause", icon='PAUSE')
  270.         row.operator("sound.stop", text="Stop", icon='CANCEL')
  271.         row = col.row()
  272.         row.operator("sound.prev", text="Previus", icon='REW')
  273.         row.operator("sound.next", text="Next", icon='FF')
  274.         col.operator("sound.play", text="Play", icon='PLAY')
  275.         col.operator("sound.delplaylist", text="Clear play list", icon='CANCEL')
  276.         #col.operator("sound.printplaylist", text="Print playlist", icon='TEXT')
  277.         col.prop(context.window_manager, "volume", slider=True)
  278.         col.prop(bpy.context.window_manager, 'show_names', text='show playlist')
  279.         #col.prop(context.window_manager.f.limit)
  280.         #plaingnow = context.window_manager.playlist[context.window_manager.index]
  281.         #playitemgroup = re.match(r'(\w+)', plaingnow) # - need to be pattern for filename for linux/windows exception /\ in full path. So all after /\/ needed to be in group(1) matching
  282.         #playitem = playitemgroup.group(1)
  283.         plaingindex = 'Song index: '+str(context.window_manager.index+1)+'/'+str(len(context.window_manager.playlist))
  284.         col.label(text=plaingindex)
  285.        
  286.        
  287.         if bpy.context.window_manager.playlist_names:
  288.             playlist_print=context.window_manager.playlist_names
  289.         else:
  290.             playlist_print=context.window_manager.playlist
  291.         if bpy.context.window_manager.show_names:
  292.             box = layout.box()
  293.             i=0
  294.             for p in playlist_print:
  295.                 i+=1
  296.                 #col.operator('sound.play', bpy.context.window_manager.index=i-1, text=str(i)+' '+str(p))
  297.                 col = box.column(align=True)
  298.                 col.operator("sound.play", text=str(i)+' | '+str(p)).item_play=str([True, i-1])
  299.         #a = context.window_manager
  300.         #a.progress_begin(0,1)
  301.         #a.progress_update(0.5)
  302.         #a.progress_end()
  303.  
  304.  
  305. # define classes for registration
  306. classes = [PlaySIC,
  307.             ImportSIC,
  308.             ImportM3U,
  309.             StopSIC,
  310.             DelList,
  311.             NextSIC,
  312.             PrevSIC,
  313.             PauseSIC,
  314.             ResumeSIC,
  315.             VIEW3D_PT_Musicplayer,
  316.             PrintPlaylist]
  317.  
  318.  
  319. # registering
  320. def register():
  321.     for c in classes:
  322.         bpy.utils.register_class(c)
  323.     bpy.types.WindowManager.playlist=[]
  324.     bpy.types.WindowManager.index=bpy.props.IntProperty()
  325.     bpy.types.WindowManager.pause = bpy.props.BoolProperty(False)
  326.     bpy.types.WindowManager.volume = bpy.props.FloatProperty(name="Volume",default=1.0, min=0.0, max=1.0, update=volume_up)    
  327.     bpy.types.WindowManager.d = aud.device()
  328.  
  329.  
  330. # unregistering
  331. def unregister():
  332.     for c in classes:
  333.         bpy.utils.unregister_class(c)
  334.     try:
  335.         del bpy.types.WindowManager.index
  336.         del bpy.types.WindowManager.playlist
  337.         del bpy.types.WindowManager.pause
  338.         del bpy.types.WindowManager.volume
  339.         del bpy.types.WindowManager.d
  340.     except:
  341.         pass
  342.     try:
  343.         del bpy.types.WindowManager.f
  344.         del bpy.types.WindowManager.playsound
  345.         del bpy.types.WindowManager.show_names
  346.     except:
  347.         pass
  348.  
  349. if __name__ == "__main__":
  350.     register()
  351.    
  352.  
go to heaven