def callBackSourcesMenu(event,val): source=event&EventsMask if event&Events['export']==0: Acoustic.getSources()[source].properties['WAV_SOURCE']=Blender.Sound.Get()[val-1].getFilename() return #else: sources=Acoustic.getSources()[source].sel=val return
def wavsExport(scene): buffer="" for source in Acoustic.getSources(): wavFilename=Acoustic.getWav(source) if wavFilename==0 or source.sel==0: continue objectName=source.name buffer+=WavLineTemplate % vars() return buffer
def wavsExport(scene): buffer = "" for source in Acoustic.getSources(): wavFilename = Acoustic.getWav(source) if wavFilename == 0 or source.sel == 0: continue objectName = source.name buffer += WavLineTemplate % vars() return buffer
def choreoExport(scene, normalize=True): SelectedObjects = Blender.Object.GetSelected() listeners = Acoustic.getListeners() sources = [] target = None buffer = "" for source in Acoustic.getSources(): if source.sel == 1: sources.append(source) for listener in listeners: if listener.sel == 1: target = listener break if target == None or not sources: Blender.Draw.PupMenu( 'You have to select one listener and at least one source objects!') return allAcousticObjects = Acoustic.getAcousticObjects(scene) # print list(allAcousticObjects) #TODO: refactor this (checked three times!) if normalize == True: normalizationOffset, normalizationScale = getNormalizationParameters( allAcousticObjects) for frame in range(Blender.Get('staframe'), Blender.Get('endframe')): Blender.Set('curframe', frame) sourceOrientationMatrix = target.mat if target.type == 'Camera': # if the listener is a camera, do a proper rotation acording to conventions: azimuth 0: seeing at x+ rotationMatrix = Blender.Mathutils.Euler( 90, 0, -90).toMatrix().invert().resize4x4() sourceOrientationMatrix = rotationMatrix * sourceOrientationMatrix roll, descention, azimuth = sourceOrientationMatrix.toEuler() targetElevation = (-descention) % 360 if targetElevation > 180: targetElevation = targetElevation - 360 targetRoll = (roll) % 360 targetAzimuth = (azimuth) % 360 if normalize == True: targetX, targetY, targetZ = normalizePosition( listener.mat.translationPart(), normalizationOffset, normalizationScale, False) else: targetX, targetY, targetZ = listener.mat.translationPart() sourcesPositions = "" for source in sources: if source.sel == 1: if normalize == True: sourceX, sourceY, sourceZ = normalizePosition( source.mat.translationPart(), normalizationOffset, normalizationScale, False) else: sourceX, sourceY, sourceZ = source.mat.translationPart() sourcesPositions += " %f\t%f\t%f" % (sourceX, sourceY, sourceZ) buffer += ChoreoLineTemplate % vars() return buffer
def drawCallback(): size=Blender.Window.GetAreaSize() xPos=0 yPos=size[1] sources=Acoustic.getSources() yPos=layoutSources(sources,"Audio sources:",(xPos,yPos))-40 materials=Acoustic.getMaterials() yPos=layoutMaterials(materials,"Acoustic materials:",(xPos,yPos))-40 listeners=Acoustic.getListeners() yPos=layoutListeners(listeners,"Select listener:",(xPos,yPos))-10 layoutExporter((xPos,yPos)) return
def choreoExport (scene,normalize=True): SelectedObjects = Blender.Object.GetSelected() listeners=Acoustic.getListeners() sources=[] target=None buffer="" for source in Acoustic.getSources(): if source.sel==1: sources.append(source) for listener in listeners: if listener.sel==1: target=listener break if target==None or not sources: Blender.Draw.PupMenu('You have to select one listener and at least one source objects!') return allAcousticObjects=Acoustic.getAcousticObjects(scene) # print list(allAcousticObjects) #TODO: refactor this (checked three times!) if normalize==True: normalizationOffset,normalizationScale=getNormalizationParameters(allAcousticObjects) for frame in range(Blender.Get('staframe'),Blender.Get('endframe')): Blender.Set('curframe',frame) sourceOrientationMatrix=target.mat if target.type=='Camera': # if the listener is a camera, do a proper rotation acording to conventions: azimuth 0: seeing at x+ rotationMatrix=Blender.Mathutils.Euler(90,0,-90).toMatrix().invert().resize4x4() sourceOrientationMatrix = rotationMatrix * sourceOrientationMatrix roll,descention,azimuth=sourceOrientationMatrix.toEuler() targetElevation=(-descention)%360 if targetElevation>180: targetElevation=targetElevation-360 targetRoll=(roll)%360 targetAzimuth=(azimuth)%360 if normalize==True: targetX,targetY,targetZ=normalizePosition(listener.mat.translationPart(),normalizationOffset,normalizationScale,False) else: targetX,targetY,targetZ=listener.mat.translationPart() sourcesPositions="" for source in sources: if source.sel==1: if normalize==True: sourceX,sourceY,sourceZ=normalizePosition(source.mat.translationPart(),normalizationOffset,normalizationScale,False) else: sourceX,sourceY,sourceZ=source.mat.translationPart() sourcesPositions+=" %f\t%f\t%f" % (sourceX,sourceY,sourceZ) buffer+=ChoreoLineTemplate % vars() return buffer
def drawCallback(): size=Blender.Window.GetAreaSize() xPos=100 yPos=size[1]-100 listeners=Acoustic.getListeners() yPos=layoutListeners(listeners,"Geometries to export:",(xPos,yPos))-100 layoutExporter((xPos,yPos)) return
def drawCallback(): size = Blender.Window.GetAreaSize() xPos = 100 yPos = size[1] - 100 listeners = Acoustic.getListeners() yPos = layoutListeners(listeners, "Geometries to export:", (xPos, yPos)) - 100 layoutExporter((xPos, yPos)) return
def layoutMaterials(materials,label="acoustic materials",offset=(0,0)): xPosition=offset[0]+xLayoutMarks['labels'] yPosition=offset[1] Blender.Draw.Label(label,xPosition,yPosition,120,30) counter=0 for material in materials: xPosition=offset[0]+xLayoutMarks['sublabels'] Blender.Draw.Label(material.name,xPosition,yPosition,120,30) impedance=Acoustic.getImpedance(material) diffusion=Acoustic.getDiffusion(material) xPosition=offset[0]+xLayoutMarks['setters'] number=Blender.Draw.String("imp. real\t\t",counter|Events['impedance_real'],xPosition,yPosition,200,20,str(impedance.real),30,"set impedance (real)",callBackMaterialButtons) yPosition-=30 number=Blender.Draw.String("imp. imag\t\t",counter|Events['impedance_imaginary'],xPosition,yPosition,200,20,str(impedance.imag),30,"set impedance (imaginary)",callBackMaterialButtons) yPosition-=30 number=Blender.Draw.String("diff.\t\t\t\t",counter|Events['diffusion'],xPosition,yPosition,200,20,str(diffusion),30,"set diffusion",callBackMaterialButtons) yPosition-=40 counter+=1 return yPosition
def callBackListener(event,val): print "callBackListener" print "event: %s\t-\tval: %s" % (event,val) listeners=Acoustic.getListeners() if val>0: newListener=listeners[val-1] for listener in listeners: if listener==newListener: listener.sel=1 else: listener.sel=0 return
def callBackMaterialButtons(event,val): try: val=float(val) except: return material=Acoustic.getMaterials()[event&EventsMask] if event&Events['impedance']: # print "material: %s" % material impedance=Acoustic.getImpedance(material) if (event&Events['impedance_imaginary'])==Events['impedance_imaginary']: Acoustic.setImpedance(material,complex(impedance.real,val)) # print "setting imag to %s" % val return else: Acoustic.setImpedance(material,complex(val,impedance.imag)) # print "setting real to %s" % val return else: # print "setting diffusion to %s" % val Acoustic.setDiffusion(material,val) return
def layoutListeners(listeners,label="listeners",offset=(0,0)): xPosition=offset[0]+xLayoutMarks['sublabels'] yPosition=offset[1] listeners=Acoustic.getListeners() selectedListener=0 # get first selected listener (or 0) for listener in listeners: if list(Blender.Scene.GetCurrent().objects.selected).count(listener)!=0: selectedListener=listeners.index(listener)+1 break Blender.Draw.Label(label,xPosition,yPosition,120,30) menu="Listeners %t" counter=1 xPosition=offset[0]+xLayoutMarks['setters'] for listener in listeners: menu+="|"+listener.name+"%x"+str(counter) counter+=1 menu=Blender.Draw.Menu(menu,0,xPosition,yPosition,200,20,selectedListener,"select listener to export",callBackListener) return yPosition
def layoutSources(sources,label="audio sources",offset=(0,0)): yPosition=offset[1]+yLayoutMarks['sources'] xPosition=offset[0]+xLayoutMarks['labels'] Blender.Draw.Label(label,xPosition,yPosition,120,20) for object in sources: objectIndex=sources.index(object) xPosition=offset[0]+xLayoutMarks['sublabels'] Blender.Draw.Label(object.name,xPosition,yPosition,150,20) xPosition=offset[0]+xLayoutMarks['setters'] menu="imported sounds %t" counter=indexFile=1 for sound in Blender.Sound.Get(): if sound.getFilename()==Acoustic.getWav(object): indexFile=counter menu+="|"+sound.getFilename()+"%x"+str(counter) counter+=1 menu=Blender.Draw.Menu(menu,objectIndex|Events['wav_source'],xPosition,yPosition,200,20,indexFile,"select sound file",callBackSourcesMenu) xPosition=offset[0]+xLayoutMarks['buttons'] selected=object.sel export=Blender.Draw.Toggle ("select",objectIndex|Events['export'],xPosition,yPosition,100,20,selected,"select to include it on exportation",callBackSourcesMenu) yPosition-=30 return yPosition
def main(): if (Acoustic.initProperties()!=-1): Blender.Draw.Register(drawCallback,eventCallback)
def geometryExport(scene,typeOfGeometry='geometry',skipOthersGeometries=False): buffer=_geometryHeader actors=[] actors=Acoustic.getActors() # print "---------- acoustic actors: %s" % list(actors) # for object in scene.objects: # print "---------- acoustic objects: %s" % list(Acoustic.getAcousticObjects(scene)) for object in Acoustic.getAcousticObjects(scene): if skipOthersGeometries and Acoustic.getObjectSoundTypeGameProperty(object) != typeOfGeometry: continue impedance=None diffusion=None materialName="" print "Reading object: %s" % object.name # data=bpy.data.meshes[object.getData().name] if type(object.getData())!=Blender.Types.NMeshType: print "no mesh, skipping..." continue if actors.count(object)!=0: print "%s is an actor, skipping" % object.name continue materials=Acoustic.getObjectMaterials(object) if (materials==None or materials==[]): print "\tdoesn't contains acoustic materials,", if Acoustic.getObjectSoundTypeGameProperty(object) != typeOfGeometry: print 'no game object property "sound_type"=="%s", skipping...' % typeOfGeometry continue #if doesnt have any acoustic (by name) material, doesnt export print 'WARNING: setting material as "%s", with default acoustic properties (impedance: %s, diff.: %s)' % (_defaultMaterialName,str(_defaultImpedance),str(_defaultDiffusion)) impedance=_defaultImpedance diffusion=_defaultDiffusion materialName=_defaultMaterialName BPyNMesh.ApplySizeAndRotation(object) data=object.getData(False,True) # get mesh instead name or NMesh (name_only=False / mesh=True) print "Exporting %s..."%object.name bufferObject= "<%s>\n" % object.name bufferObject+= "<VERTS>\n" location=object.mat.translationPart() for vert in data.verts: bufferObject+="%f %f %f\n" % (vert.co[0]+location[0],vert.co[1]+location[1],vert.co[2]+location[2]) bufferObject+="<FACES>\n" if impedance==None and diffusion==None: # it doesn't have assigned the default material # use first linked material with acoustic properties for material in materials: print material.name impedance=Acoustic.getImpedance(material) if (impedance==None): continue diffusion=Acoustic.getDiffusion(material) if (diffusion==None): continue materialName=material.name break if (impedance==None) or (diffusion==None): print "any material have acoustic parameters, skipping..." continue impedanceReal=impedance.real impedanceImag=impedance.imag if _convertToTriangles==True: print "Converting mesh to triangles..." Blender.Mesh.Mode(3) # select faces Blender.Window.EditMode(0) # non-edit mode! data.quadToTriangle(0) for face in data.faces: verts="" vertCount=0 for vert in face.verts: vertCount+=1 verts+="%s " % str(vert.index+1) if vertCount>3: print "WARNING!! Object %s have more than 3 verts (%i) per face!!!" % (object.name,vertCount) bufferObject+=FaceLineTemplate % vars() buffer+=bufferObject print "Added Object: %s" % object.name return buffer
def geometryExport(scene, typeOfGeometry='geometry', skipOthersGeometries=False): buffer = _geometryHeader actors = [] actors = Acoustic.getActors() # print "---------- acoustic actors: %s" % list(actors) # for object in scene.objects: # print "---------- acoustic objects: %s" % list(Acoustic.getAcousticObjects(scene)) for object in Acoustic.getAcousticObjects(scene): if skipOthersGeometries and Acoustic.getObjectSoundTypeGameProperty( object) != typeOfGeometry: continue impedance = None diffusion = None materialName = "" print "Reading object: %s" % object.name # data=bpy.data.meshes[object.getData().name] if type(object.getData()) != Blender.Types.NMeshType: print "no mesh, skipping..." continue if actors.count(object) != 0: print "%s is an actor, skipping" % object.name continue materials = Acoustic.getObjectMaterials(object) if (materials == None or materials == []): print "\tdoesn't contains acoustic materials,", if Acoustic.getObjectSoundTypeGameProperty( object) != typeOfGeometry: print 'no game object property "sound_type"=="%s", skipping...' % typeOfGeometry continue #if doesnt have any acoustic (by name) material, doesnt export print 'WARNING: setting material as "%s", with default acoustic properties (impedance: %s, diff.: %s)' % ( _defaultMaterialName, str(_defaultImpedance), str(_defaultDiffusion)) impedance = _defaultImpedance diffusion = _defaultDiffusion materialName = _defaultMaterialName BPyNMesh.ApplySizeAndRotation(object) data = object.getData( False, True ) # get mesh instead name or NMesh (name_only=False / mesh=True) print "Exporting %s..." % object.name bufferObject = "<%s>\n" % object.name bufferObject += "<VERTS>\n" location = object.mat.translationPart() for vert in data.verts: bufferObject += "%f %f %f\n" % (vert.co[0] + location[0], vert.co[1] + location[1], vert.co[2] + location[2]) bufferObject += "<FACES>\n" if impedance == None and diffusion == None: # it doesn't have assigned the default material # use first linked material with acoustic properties for material in materials: print material.name impedance = Acoustic.getImpedance(material) if (impedance == None): continue diffusion = Acoustic.getDiffusion(material) if (diffusion == None): continue materialName = material.name break if (impedance == None) or (diffusion == None): print "any material have acoustic parameters, skipping..." continue impedanceReal = impedance.real impedanceImag = impedance.imag if _convertToTriangles == True: print "Converting mesh to triangles..." Blender.Mesh.Mode(3) # select faces Blender.Window.EditMode(0) # non-edit mode! data.quadToTriangle(0) for face in data.faces: verts = "" vertCount = 0 for vert in face.verts: vertCount += 1 verts += "%s " % str(vert.index + 1) if vertCount > 3: print "WARNING!! Object %s have more than 3 verts (%i) per face!!!" % ( object.name, vertCount) bufferObject += FaceLineTemplate % vars() buffer += bufferObject print "Added Object: %s" % object.name return buffer