12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816 |
- bl_info = {
- 'name': 'Babylon.js',
- 'author': 'David Catuhe, Jeff Palmer',
- 'version': (4, 6, 1),
- 'blender': (2, 75, 0),
- 'location': 'File > Export > Babylon.js (.babylon)',
- 'description': 'Export Babylon.js scenes (.babylon)',
- 'wiki_url': 'https://github.com/BabylonJS/Babylon.js/tree/master/Exporters/Blender',
- 'tracker_url': '',
- 'category': 'Import-Export'}
- import base64
- import bpy
- import bpy_extras.io_utils
- import time
- import io
- import math
- import mathutils
- import os
- import shutil
- import sys, traceback # for writing errors to log file
- #===============================================================================
- # Registration the calling of the INFO_MT_file_export file selector
- def menu_func(self, context):
- self.layout.operator(Main.bl_idname, text = 'Babylon.js [.babylon] ver ' + format_version())
- def register():
- bpy.utils.register_module(__name__)
- bpy.types.INFO_MT_file_export.append(menu_func)
- def unregister():
- bpy.utils.unregister_module(__name__)
- bpy.types.INFO_MT_file_export.remove(menu_func)
- if __name__ == '__main__':
- register()
- #===============================================================================
- def format_version():
- version = bl_info['version']
- return str(version[0]) + '.' + str(version[1]) + '.' + str(version[2])
- # output related constants
- MAX_VERTEX_ELEMENTS = 65535
- MAX_VERTEX_ELEMENTS_32Bit = 16777216
- VERTEX_OUTPUT_PER_LINE = 100
- MAX_FLOAT_PRECISION_INT = 4
- MAX_FLOAT_PRECISION = '%.' + str(MAX_FLOAT_PRECISION_INT) + 'f'
- COMPRESS_MATRIX_INDICES = True # this is True for .babylon exporter & False for TOB
- FRAME_BASED_ANIMATION = True # this is only able to be turned off by the TOB exporter right now
- # used in World constructor, defined in BABYLON.Scene
- #FOGMODE_NONE = 0
- #FOGMODE_EXP = 1
- #FOGMODE_EXP2 = 2
- FOGMODE_LINEAR = 3
- # used in Mesh & Node constructors, defined in BABYLON.AbstractMesh
- BILLBOARDMODE_NONE = 0
- #BILLBOARDMODE_X = 1
- #BILLBOARDMODE_Y = 2
- #BILLBOARDMODE_Z = 4
- BILLBOARDMODE_ALL = 7
- # used in Mesh constructor, defined in BABYLON.PhysicsEngine
- SPHERE_IMPOSTER = 1
- BOX_IMPOSTER = 2
- #PLANE_IMPOSTER = 3
- MESH_IMPOSTER = 4
- CAPSULE_IMPOSTER = 5
- CONE_IMPOSTER = 6
- CYLINDER_IMPOSTER = 7
- CONVEX_HULL_IMPOSTER = 8
- # camera class names, never formally defined in Babylon, but used in babylonFileLoader
- ARC_ROTATE_CAM = 'ArcRotateCamera'
- DEV_ORIENT_CAM = 'DeviceOrientationCamera'
- FOLLOW_CAM = 'FollowCamera'
- FREE_CAM = 'FreeCamera'
- GAMEPAD_CAM = 'GamepadCamera'
- TOUCH_CAM = 'TouchCamera'
- V_JOYSTICKS_CAM = 'VirtualJoysticksCamera'
- VR_DEV_ORIENT_FREE_CAM ='VRDeviceOrientationFreeCamera'
- WEB_VR_FREE_CAM = 'WebVRFreeCamera'
- # 3D camera rigs, defined in BABYLON.Camera, must be strings to be in 'dropdown'
- RIG_MODE_NONE = '0'
- RIG_MODE_STEREOSCOPIC_ANAGLYPH = '10'
- RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL = '11'
- RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED = '12'
- RIG_MODE_STEREOSCOPIC_OVERUNDER = '13'
- RIG_MODE_VR = '20'
- # used in Light constructor, never formally defined in Babylon, but used in babylonFileLoader
- POINT_LIGHT = 0
- DIRECTIONAL_LIGHT = 1
- SPOT_LIGHT = 2
- HEMI_LIGHT = 3
- #used in ShadowGenerators
- NO_SHADOWS = 'NONE'
- STD_SHADOWS = 'STD'
- POISSON_SHADOWS = 'POISSON'
- ESM_SHADOWS = 'ESM'
- BLUR_ESM_SHADOWS = 'BLUR_ESM'
- # used in Texture constructor, defined in BABYLON.Texture
- CLAMP_ADDRESSMODE = 0
- WRAP_ADDRESSMODE = 1
- MIRROR_ADDRESSMODE = 2
- # used in Texture constructor, defined in BABYLON.Texture
- EXPLICIT_MODE = 0
- SPHERICAL_MODE = 1
- #PLANAR_MODE = 2
- CUBIC_MODE = 3
- #PROJECTION_MODE = 4
- #SKYBOX_MODE = 5
- DEFAULT_MATERIAL_NAMESPACE = 'Same as Filename'
- # passed to Animation constructor from animatable objects, defined in BABYLON.Animation
- #ANIMATIONTYPE_FLOAT = 0
- ANIMATIONTYPE_VECTOR3 = 1
- ANIMATIONTYPE_QUATERNION = 2
- ANIMATIONTYPE_MATRIX = 3
- #ANIMATIONTYPE_COLOR3 = 4
- # passed to Animation constructor from animatable objects, defined in BABYLON.Animation
- #ANIMATIONLOOPMODE_RELATIVE = 0
- ANIMATIONLOOPMODE_CYCLE = 1
- #ANIMATIONLOOPMODE_CONSTANT = 2
- #===============================================================================
- # Panel displayed in Scene Tab of properties, so settings can be saved in a .blend file
- class ExporterSettingsPanel(bpy.types.Panel):
- bl_label = 'Exporter Settings ' + format_version()
- bl_space_type = 'PROPERTIES'
- bl_region_type = 'WINDOW'
- bl_context = 'scene'
- bpy.types.Scene.export_onlySelectedLayer = bpy.props.BoolProperty(
- name="Export only selected layers",
- description="Export only selected layers",
- default = False,
- )
- bpy.types.Scene.export_flatshadeScene = bpy.props.BoolProperty(
- name="Flat shade entire scene",
- description="Use face normals on all meshes. Increases vertices.",
- default = False,
- )
- bpy.types.Scene.attachedSound = bpy.props.StringProperty(
- name='Sound',
- description='',
- default = ''
- )
- bpy.types.Scene.loopSound = bpy.props.BoolProperty(
- name='Loop sound',
- description='',
- default = True
- )
- bpy.types.Scene.autoPlaySound = bpy.props.BoolProperty(
- name='Auto play sound',
- description='',
- default = True
- )
- bpy.types.Scene.inlineTextures = bpy.props.BoolProperty(
- name="inline",
- description="turn textures into encoded strings, for direct inclusion into source code",
- default = False,
- )
- bpy.types.Scene.textureDir = bpy.props.StringProperty(
- name='sub-directory',
- description='The path below the output directory to write texture files (any separators OS dependent)',
- default = ''
- )
- bpy.types.Scene.ignoreIKBones = bpy.props.BoolProperty(
- name="Ignore IK Bones",
- description="Do not export bones with either '.ik' or 'ik.'(not case sensitive) in the name",
- default = False,
- )
- def draw(self, context):
- layout = self.layout
- scene = context.scene
- layout.prop(scene, "export_onlySelectedLayer")
- layout.prop(scene, "export_flatshadeScene")
- layout.prop(scene, "ignoreIKBones")
- box = layout.box()
- box.label(text='Texture Location:')
- box.prop(scene, "inlineTextures")
- row = box.row()
- row.enabled = not scene.inlineTextures
- row.prop(scene, "textureDir")
- box = layout.box()
- box.prop(scene, 'attachedSound')
- box.prop(scene, 'autoPlaySound')
- box.prop(scene, 'loopSound')
- #===============================================================================
- class Main(bpy.types.Operator, bpy_extras.io_utils.ExportHelper):
- bl_idname = 'scene.babylon' # module will not load with out it, also must have a dot
- bl_label = 'Export Babylon.js scene' # used on the label of the actual 'save' button
- filename_ext = '.babylon' # required to have one, although not really used
- filepath = bpy.props.StringProperty(subtype = 'FILE_PATH') # assigned once the file selector returns
- log_handler = None # assigned in execute
- nameSpace = None # assigned in execute
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- nWarnings = 0
- @staticmethod
- def warn(msg, numTabIndent = 1, noNewLine = False):
- Main.log('WARNING: ' + msg, numTabIndent, noNewLine)
- Main.nWarnings += 1
- @staticmethod
- def log(msg, numTabIndent = 1, noNewLine = False):
- for i in range(numTabIndent):
- Main.log_handler.write('\t')
- Main.log_handler.write(msg)
- if not noNewLine: Main.log_handler.write('\n')
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def getMaterial(self, baseMaterialId):
- fullName = Main.nameSpace + '.' + baseMaterialId
- for material in self.materials:
- if material.name == fullName:
- return material
- return None
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def getSourceMeshInstance(self, dataName):
- for mesh in self.meshesAndNodes:
- # nodes have no 'dataName', cannot be instanced in any case
- if hasattr(mesh, 'dataName') and mesh.dataName == dataName:
- return mesh
- return None
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def execute(self, context):
- scene = context.scene
- self.scene = scene # reference for passing
- try:
- start_time = time.time()
- filepathDotExtension = self.filepath.rpartition('.')
- self.filepathMinusExtension = filepathDotExtension[0]
- # assign nameSpace, based on OS
- if self.filepathMinusExtension.find('\\') != -1:
- Main.nameSpace = legal_js_identifier(self.filepathMinusExtension.rpartition('\\')[2])
- else:
- Main.nameSpace = legal_js_identifier(self.filepathMinusExtension.rpartition('/')[2])
- # explicitly reset globals, in case there was an earlier export this session
- Main.nWarnings = 0
- Main.log_handler = io.open(self.filepathMinusExtension + '.log', 'w', encoding='utf8')
- version = bl_info['version']
- Main.log('Exporter version: ' + format_version() + ', Blender version: ' + bpy.app.version_string)
- if bpy.ops.object.mode_set.poll():
- bpy.ops.object.mode_set(mode = 'OBJECT')
- # assign texture location, purely temporary if inlining
- self.textureDir = os.path.dirname(self.filepath)
- if not scene.inlineTextures:
- self.textureDir = os.path.join(self.textureDir, scene.textureDir)
- if not os.path.isdir(self.textureDir):
- os.makedirs(self.textureDir)
- Main.warn("Texture sub-directory did not already exist, created: " + self.textureDir)
- Main.log('========= Conversion from Blender to Babylon.js =========', 0)
- Main.log('Scene settings used:', 1)
- Main.log('selected layers only: ' + format_bool(scene.export_onlySelectedLayer), 2)
- Main.log('flat shading entire scene: ' + format_bool(scene.export_flatshadeScene), 2)
- Main.log('inline textures: ' + format_bool(scene.inlineTextures), 2)
- if not scene.inlineTextures:
- Main.log('texture directory: ' + self.textureDir, 2)
- self.world = World(scene)
- bpy.ops.screen.animation_cancel()
- currentFrame = bpy.context.scene.frame_current
- # Active camera
- if scene.camera != None:
- self.activeCamera = scene.camera.name
- else:
- Main.warn('No active camera has been assigned, or is not in a currently selected Blender layer')
- self.cameras = []
- self.lights = []
- self.shadowGenerators = []
- self.skeletons = []
- skeletonId = 0
- self.meshesAndNodes = []
- self.materials = []
- self.multiMaterials = []
- self.sounds = []
- # Scene level sound
- if scene.attachedSound != '':
- self.sounds.append(Sound(scene.attachedSound, scene.autoPlaySound, scene.loopSound))
- # separate loop doing all skeletons, so available in Mesh to make skipping IK bones possible
- for object in [object for object in scene.objects]:
- scene.frame_set(currentFrame)
- if object.type == 'ARMATURE': #skeleton.pose.bones
- if object.is_visible(scene):
- self.skeletons.append(Skeleton(object, scene, skeletonId))
- skeletonId += 1
- else:
- Main.warn('The following armature not visible in scene thus ignored: ' + object.name)
- # exclude lamps in this pass, so ShadowGenerator constructor can be passed meshesAnNodes
- for object in [object for object in scene.objects]:
- scene.frame_set(currentFrame)
- if object.type == 'CAMERA':
- if object.is_visible(scene): # no isInSelectedLayer() required, is_visible() handles this for them
- self.cameras.append(Camera(object))
- else:
- Main.warn('The following camera not visible in scene thus ignored: ' + object.name)
- elif object.type == 'MESH':
- forcedParent = None
- nameID = ''
- nextStartFace = 0
- while True and self.isInSelectedLayer(object, scene):
- mesh = Mesh(object, scene, nextStartFace, forcedParent, nameID, self)
- if hasattr(mesh, 'instances'):
- self.meshesAndNodes.append(mesh)
- else:
- break
- if object.data.attachedSound != '':
- self.sounds.append(Sound(object.data.attachedSound, object.data.autoPlaySound, object.data.loopSound, object))
- nextStartFace = mesh.offsetFace
- if nextStartFace == 0:
- break
- if forcedParent is None:
- nameID = 0
- forcedParent = object
- Main.warn('The following mesh has exceeded the maximum # of vertex elements & will be broken into multiple Babylon meshes: ' + object.name)
- nameID = nameID + 1
- elif object.type == 'EMPTY':
- self.meshesAndNodes.append(Node(object))
- elif object.type != 'LAMP' and object.type != 'ARMATURE':
- Main.warn('The following object (type - ' + object.type + ') is not currently exportable thus ignored: ' + object.name)
- # Lamp / shadow Generator pass; meshesAnNodes complete & forceParents included
- for object in [object for object in scene.objects]:
- scene.frame_set(currentFrame)
- if object.type == 'LAMP':
- if object.is_visible(scene): # no isInSelectedLayer() required, is_visible() handles this for them
- bulb = Light(object)
- self.lights.append(bulb)
- if object.data.shadowMap != 'NONE':
- if bulb.light_type == DIRECTIONAL_LIGHT or bulb.light_type == SPOT_LIGHT:
- self.shadowGenerators.append(ShadowGenerator(object, self.meshesAndNodes, scene))
- else:
- Main.warn('Only directional (sun) and spot types of lamp are valid for shadows thus ignored: ' + object.name)
- else:
- Main.warn('The following lamp not visible in scene thus ignored: ' + object.name)
- bpy.context.scene.frame_set(currentFrame)
- # output file
- self.to_scene_file ()
- except:# catch *all* exceptions
- ex = sys.exc_info()
- Main.log('========= An error was encountered =========', 0)
- stack = traceback.format_tb(ex[2])
- for line in stack:
- Main.log_handler.write(line) # avoid tabs & extra newlines by not calling log() inside catch
- Main.log_handler.write('ERROR: ' + str(ex[1]) + '\n')
- raise
- finally:
- Main.log('========= end of processing =========', 0)
- elapsed_time = time.time() - start_time
- minutes = math.floor(elapsed_time / 60)
- seconds = elapsed_time - (minutes * 60)
- Main.log('elapsed time: ' + str(minutes) + ' min, ' + format_f(seconds) + ' secs', 0)
- Main.log_handler.close()
- if (Main.nWarnings > 0):
- self.report({'WARNING'}, 'Processing completed, but ' + str(Main.nWarnings) + ' WARNINGS were raised, see log file.')
- return {'FINISHED'}
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self):
- Main.log('========= Writing of scene file started =========', 0)
- # Open file
- file_handler = io.open(self.filepathMinusExtension + '.babylon', 'w', encoding='utf8')
- file_handler.write('{')
- file_handler.write('"producer":{"name":"Blender","version":"' + bpy.app.version_string + '","exporter_version":"' + format_version() + '","file":"' + Main.nameSpace + '.babylon"},\n')
- self.world.to_scene_file(file_handler)
- # Materials
- file_handler.write(',\n"materials":[')
- first = True
- for material in self.materials:
- if first != True:
- file_handler.write(',\n')
- first = False
- material.to_scene_file(file_handler)
- file_handler.write(']')
- # Multi-materials
- file_handler.write(',\n"multiMaterials":[')
- first = True
- for multimaterial in self.multiMaterials:
- if first != True:
- file_handler.write(',')
- first = False
- multimaterial.to_scene_file(file_handler)
- file_handler.write(']')
- # Armatures/Bones
- file_handler.write(',\n"skeletons":[')
- first = True
- for skeleton in self.skeletons:
- if first != True:
- file_handler.write(',')
- first = False
- skeleton.to_scene_file(file_handler)
- file_handler.write(']')
- # Meshes
- file_handler.write(',\n"meshes":[')
- first = True
- for m in range(0, len(self.meshesAndNodes)):
- mesh = self.meshesAndNodes[m]
- if first != True:
- file_handler.write(',')
- first = False
- mesh.to_scene_file(file_handler)
- file_handler.write(']')
- # Cameras
- file_handler.write(',\n"cameras":[')
- first = True
- for camera in self.cameras:
- if hasattr(camera, 'fatalProblem'): continue
- if first != True:
- file_handler.write(',')
- first = False
- camera.update_for_target_attributes(self.meshesAndNodes)
- camera.to_scene_file(file_handler)
- file_handler.write(']')
- # Active camera
- if hasattr(self, 'activeCamera'):
- write_string(file_handler, 'activeCamera', self.activeCamera)
- # Lights
- file_handler.write(',\n"lights":[')
- first = True
- for light in self.lights:
- if first != True:
- file_handler.write(',')
- first = False
- light.to_scene_file(file_handler)
- file_handler.write(']')
- # Shadow generators
- file_handler.write(',\n"shadowGenerators":[')
- first = True
- for shadowGen in self.shadowGenerators:
- if first != True:
- file_handler.write(',')
- first = False
- shadowGen.to_scene_file(file_handler)
- file_handler.write(']')
- # Sounds
- if len(self.sounds) > 0:
- file_handler.write('\n,"sounds":[')
- first = True
- for sound in self.sounds:
- if first != True:
- file_handler.write(',')
- first = False
- sound.to_scene_file(file_handler)
- file_handler.write(']')
- # Closing
- file_handler.write('\n}')
- file_handler.close()
- Main.log('========= Writing of scene file completed =========', 0)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def isInSelectedLayer(self, obj, scene):
- if not scene.export_onlySelectedLayer:
- return True
- for l in range(0, len(scene.layers)):
- if obj.layers[l] and scene.layers[l]:
- return True
- return False
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def get_skeleton(self, name):
- for skeleton in self.skeletons:
- if skeleton.name == name:
- return skeleton
- #really cannot happen, will cause exception in caller
- return None
- #===============================================================================
- class World:
- def __init__(self, scene):
- self.autoClear = True
- world = scene.world
- if world:
- self.ambient_color = world.ambient_color
- self.clear_color = world.horizon_color
- else:
- self.ambient_color = mathutils.Color((0.2, 0.2, 0.3))
- self.clear_color = mathutils.Color((0.0, 0.0, 0.0))
- self.gravity = scene.gravity
- if world and world.mist_settings.use_mist:
- self.fogMode = FOGMODE_LINEAR
- self.fogColor = world.horizon_color
- self.fogStart = world.mist_settings.start
- self.fogEnd = world.mist_settings.depth
- self.fogDensity = 0.1
- Main.log('Python World class constructor completed')
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- write_bool(file_handler, 'autoClear', self.autoClear, True)
- write_color(file_handler, 'clearColor', self.clear_color)
- write_color(file_handler, 'ambientColor', self.ambient_color)
- write_vector(file_handler, 'gravity', self.gravity)
- if hasattr(self, 'fogMode'):
- write_int(file_handler, 'fogMode', self.fogMode)
- write_color(file_handler, 'fogColor', self.fogColor)
- write_float(file_handler, 'fogStart', self.fogStart)
- write_float(file_handler, 'fogEnd', self.fogEnd)
- write_float(file_handler, 'fogDensity', self.fogDensity)
- #===============================================================================
- class Sound:
- def __init__(self, name, autoplay, loop, connectedMesh = None):
- self.name = name;
- self.autoplay = autoplay
- self.loop = loop
- if connectedMesh != None:
- self.connectedMeshId = connectedMesh.name
- self.maxDistance = connectedMesh.data.maxSoundDistance
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_bool(file_handler, 'autoplay', self.autoplay)
- write_bool(file_handler, 'loop', self.loop)
- if hasattr(self, 'connectedMeshId'):
- write_string(file_handler, 'connectedMeshId', self.connectedMeshId)
- write_float(file_handler, 'maxDistance', self.maxDistance)
- file_handler.write('}')
- #===============================================================================
- class FCurveAnimatable:
- def define_animations(self, object, supportsRotation, supportsPosition, supportsScaling, xOffsetForRotation = 0):
- # just because a sub-class can be animatable does not mean it is
- self.animationsPresent = object.animation_data and object.animation_data.action
- if (self.animationsPresent):
- Main.log('animation processing begun', 2)
- # instance each type of animation support regardless of whether there is any data for it
- if supportsRotation:
- if object.rotation_mode == 'QUATERNION':
- if object.type == 'CAMERA':
- # if it's a camera, convert quaternions to euler XYZ
- rotAnimation = QuaternionToEulerAnimation(object, 'rotation', 'rotation_quaternion', -1, xOffsetForRotation)
- else:
- rotAnimation = QuaternionAnimation(object, 'rotationQuaternion', 'rotation_quaternion', 1, xOffsetForRotation)
- else:
- rotAnimation = VectorAnimation(object, 'rotation', 'rotation_euler', -1, xOffsetForRotation)
- if supportsPosition:
- posAnimation = VectorAnimation(object, 'position', 'location')
- if supportsScaling:
- scaleAnimation = VectorAnimation(object, 'scaling', 'scale')
- self.ranges = []
- frameOffset = 0
- for action in bpy.data.actions:
- # get the range / assigning the action to the object
- animationRange = AnimationRange.actionPrep(object, action, False, frameOffset)
- if animationRange is None:
- continue
- if supportsRotation:
- hasData = rotAnimation.append_range(object, animationRange)
- if supportsPosition:
- hasData |= posAnimation.append_range(object, animationRange)
- if supportsScaling:
- hasData |= scaleAnimation.append_range(object, animationRange)
- if hasData:
- Main.log('processing action ' + animationRange.to_string(), 3)
- self.ranges.append(animationRange)
- frameOffset = animationRange.frame_end
- #Set Animations
- self.animations = []
- if supportsRotation and len(rotAnimation.frames) > 0:
- self.animations.append(rotAnimation)
- if supportsPosition and len(posAnimation.frames) > 0:
- self.animations.append(posAnimation)
- if supportsScaling and len(scaleAnimation.frames) > 0:
- self.animations.append(scaleAnimation)
- if (hasattr(object.data, "autoAnimate") and object.data.autoAnimate):
- self.autoAnimate = True
- self.autoAnimateFrom = bpy.context.scene.frame_end
- self.autoAnimateTo = 0
- for animation in self.animations:
- if self.autoAnimateFrom > animation.get_first_frame():
- self.autoAnimateFrom = animation.get_first_frame()
- if self.autoAnimateTo < animation.get_last_frame():
- self.autoAnimateTo = animation.get_last_frame()
- self.autoAnimateLoop = True
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- if (self.animationsPresent):
- file_handler.write('\n,"animations":[')
- first = True
- for animation in self.animations:
- if first == False:
- file_handler.write(',')
- animation.to_scene_file(file_handler)
- first = False
- file_handler.write(']')
- file_handler.write(',"ranges":[')
- first = True
- for range in self.ranges:
- if first != True:
- file_handler.write(',')
- first = False
- range.to_scene_file(file_handler)
- file_handler.write(']')
- if (hasattr(self, "autoAnimate") and self.autoAnimate):
- write_bool(file_handler, 'autoAnimate', self.autoAnimate)
- write_int(file_handler, 'autoAnimateFrom', self.autoAnimateFrom)
- write_int(file_handler, 'autoAnimateTo', self.autoAnimateTo)
- write_bool(file_handler, 'autoAnimateLoop', self.autoAnimateLoop)
- #===============================================================================
- class Mesh(FCurveAnimatable):
- def __init__(self, object, scene, startFace, forcedParent, nameID, exporter):
- self.name = object.name + str(nameID)
- Main.log('processing begun of mesh: ' + self.name)
- self.define_animations(object, True, True, True) #Should animations be done when forcedParent
- self.isVisible = not object.hide_render
- self.isEnabled = not object.data.loadDisabled
- useFlatShading = scene.export_flatshadeScene or object.data.useFlatShading
- self.checkCollisions = object.data.checkCollisions
- self.receiveShadows = object.data.receiveShadows
- self.castShadows = object.data.castShadows
- self.freezeWorldMatrix = object.data.freezeWorldMatrix
- # hasSkeleton detection & skeletonID determination
- hasSkeleton = False
- objArmature = None # if there's an armature, this will be the one!
- if len(object.vertex_groups) > 0:
- objArmature = object.find_armature()
- if objArmature != None:
- hasSkeleton = True
- # used to get bone index, since could be skipping IK bones
- skeleton = exporter.get_skeleton(objArmature.name)
- i = 0
- for obj in scene.objects:
- if obj.type == "ARMATURE":
- if obj == objArmature:
- self.skeletonId = i
- break
- else:
- i += 1
- # determine Position, rotation, & scaling
- if forcedParent is None:
- # Use local matrix
- locMatrix = object.matrix_local
- if objArmature != None:
- # unless the armature is the parent
- if object.parent and object.parent == objArmature:
- locMatrix = object.matrix_world * object.parent.matrix_world.inverted()
- loc, rot, scale = locMatrix.decompose()
- self.position = loc
- if object.rotation_mode == 'QUATERNION':
- self.rotationQuaternion = rot
- else:
- self.rotation = scale_vector(rot.to_euler('XYZ'), -1)
- self.scaling = scale
- else:
- # use defaults when not None
- self.position = mathutils.Vector((0, 0, 0))
- self.rotation = scale_vector(mathutils.Vector((0, 0, 0)), 1) # isn't scaling 0's by 1 same as 0?
- self.scaling = mathutils.Vector((1, 1, 1))
- # determine parent & dataName
- if forcedParent is None:
- self.dataName = object.data.name # used to support shared vertex instances in later passed
- if object.parent and object.parent.type != 'ARMATURE':
- self.parentId = object.parent.name
- else:
- self.dataName = self.name
- self.parentId = forcedParent.name
- # Get if this will be an instance of another, before processing materials, to avoid multi-bakes
- sourceMesh = exporter.getSourceMeshInstance(self.dataName)
- if sourceMesh is not None:
- #need to make sure rotation mode matches, since value initially copied in InstancedMesh constructor
- if hasattr(sourceMesh, 'rotationQuaternion'):
- instRot = None
- instRotq = rot
- else:
- instRot = scale_vector(rot.to_euler('XYZ'), -1)
- instRotq = None
- instance = MeshInstance(self.name, self.position, instRot, instRotq, self.scaling, self.freezeWorldMatrix)
- sourceMesh.instances.append(instance)
- Main.log('mesh is an instance of : ' + sourceMesh.name + '. Processing halted.', 2)
- return
- else:
- self.instances = []
- # Physics
- if object.rigid_body != None:
- shape_items = {'SPHERE' : SPHERE_IMPOSTER,
- 'BOX' : BOX_IMPOSTER,
- 'MESH' : MESH_IMPOSTER,
- 'CAPSULE' : CAPSULE_IMPOSTER,
- 'CONE' : CONE_IMPOSTER,
- 'CYLINDER' : CYLINDER_IMPOSTER,
- 'CONVEX_HULL': CONVEX_HULL_IMPOSTER}
- shape_type = shape_items[object.rigid_body.collision_shape]
- self.physicsImpostor = shape_type
- mass = object.rigid_body.mass
- if mass < 0.005:
- mass = 0
- self.physicsMass = mass
- self.physicsFriction = object.rigid_body.friction
- self.physicsRestitution = object.rigid_body.restitution
- # process all of the materials required
- maxVerts = MAX_VERTEX_ELEMENTS # change for multi-materials
- recipe = BakingRecipe(object)
- self.billboardMode = recipe.billboardMode
- if recipe.needsBaking:
- if recipe.multipleRenders:
- Main.warn('Mixing of Cycles & Blender Render in same mesh not supported. No materials exported.', 2)
- else:
- bakedMat = BakedMaterial(exporter, object, recipe)
- exporter.materials.append(bakedMat)
- self.materialId = bakedMat.name
- else:
- bjs_material_slots = []
- for slot in object.material_slots:
- # None will be returned when either the first encounter or must be unique due to baked textures
- material = exporter.getMaterial(slot.name)
- if (material != None):
- Main.log('registered as also a user of material: ' + slot.name, 2)
- else:
- material = StdMaterial(slot, exporter, object)
- exporter.materials.append(material)
- bjs_material_slots.append(material)
- if len(bjs_material_slots) == 1:
- self.materialId = bjs_material_slots[0].name
- elif len(bjs_material_slots) > 1:
- multimat = MultiMaterial(bjs_material_slots, len(exporter.multiMaterials))
- self.materialId = multimat.name
- exporter.multiMaterials.append(multimat)
- maxVerts = MAX_VERTEX_ELEMENTS_32Bit
- else:
- Main.warn('No materials have been assigned: ', 2)
- # Get mesh
- mesh = object.to_mesh(scene, True, 'PREVIEW')
- # Triangulate mesh if required
- Mesh.mesh_triangulate(mesh)
- # Getting vertices and indices
- self.positions = []
- self.normals = []
- self.uvs = [] # not always used
- self.uvs2 = [] # not always used
- self.colors = [] # not always used
- self.indices = []
- self.subMeshes = []
- hasUV = len(mesh.tessface_uv_textures) > 0
- if hasUV:
- which = len(mesh.tessface_uv_textures) - 1 if recipe.needsBaking else 0
- UVmap = mesh.tessface_uv_textures[which].data
- hasUV2 = len(mesh.tessface_uv_textures) > 1 and not recipe.needsBaking
- if hasUV2:
- UV2map = mesh.tessface_uv_textures[1].data
- hasVertexColor = len(mesh.vertex_colors) > 0
- if hasVertexColor:
- Colormap = mesh.tessface_vertex_colors.active.data
- if hasSkeleton:
- weightsPerVertex = []
- indicesPerVertex = []
- influenceCounts = [0, 0, 0, 0, 0, 0, 0, 0, 0] # 9, so accessed orign 1; 0 used for all those greater than 8
- totalInfluencers = 0
- highestInfluenceObserved = 0
- # used tracking of vertices as they are received
- alreadySavedVertices = []
- vertices_Normals = []
- vertices_UVs = []
- vertices_UV2s = []
- vertices_Colors = []
- vertices_indices = []
- vertices_sk_weights = []
- vertices_sk_indices = []
- self.offsetFace = 0
- for v in range(0, len(mesh.vertices)):
- alreadySavedVertices.append(False)
- vertices_Normals.append([])
- vertices_UVs.append([])
- vertices_UV2s.append([])
- vertices_Colors.append([])
- vertices_indices.append([])
- vertices_sk_weights.append([])
- vertices_sk_indices.append([])
- materialsCount = 1 if recipe.needsBaking else max(1, len(object.material_slots))
- verticesCount = 0
- indicesCount = 0
- for materialIndex in range(materialsCount):
- if self.offsetFace != 0:
- break
- subMeshVerticesStart = verticesCount
- subMeshIndexStart = indicesCount
- for faceIndex in range(startFace, len(mesh.tessfaces)): # For each face
- face = mesh.tessfaces[faceIndex]
- if face.material_index != materialIndex and not recipe.needsBaking:
- continue
- if verticesCount + 3 > maxVerts:
- self.offsetFace = faceIndex
- break
- for v in range(3): # For each vertex in face
- vertex_index = face.vertices[v]
- vertex = mesh.vertices[vertex_index]
- position = vertex.co
- normal = face.normal if useFlatShading else vertex.normal
- #skeletons
- if hasSkeleton:
- matricesWeights = []
- matricesIndices = []
- # Getting influences
- for group in vertex.groups:
- index = group.group
- weight = group.weight
- # do not need boneIndex; using skeleton.get_index_of_bone()
- for boneIndex, bone in enumerate(objArmature.pose.bones):
- if object.vertex_groups[index].name == bone.name:
- matricesWeights.append(weight)
- matricesIndices.append(skeleton.get_index_of_bone(bone.name))
- # Texture coordinates
- if hasUV:
- vertex_UV = UVmap[face.index].uv[v]
- if hasUV2:
- vertex_UV2 = UV2map[face.index].uv[v]
- # Vertex color
- if hasVertexColor:
- if v == 0:
- vertex_Color = Colormap[face.index].color1
- if v == 1:
- vertex_Color = Colormap[face.index].color2
- if v == 2:
- vertex_Color = Colormap[face.index].color3
- # Check if the current vertex is already saved
- alreadySaved = alreadySavedVertices[vertex_index] and not useFlatShading
- if alreadySaved:
- alreadySaved = False
- # UV
- index_UV = 0
- for savedIndex in vertices_indices[vertex_index]:
- vNormal = vertices_Normals[vertex_index][index_UV]
- if (normal.x != vNormal.x or normal.y != vNormal.y or normal.z != vNormal.z):
- continue;
- if hasUV:
- vUV = vertices_UVs[vertex_index][index_UV]
- if (vUV[0] != vertex_UV[0] or vUV[1] != vertex_UV[1]):
- continue
- if hasUV2:
- vUV2 = vertices_UV2s[vertex_index][index_UV]
- if (vUV2[0] != vertex_UV2[0] or vUV2[1] != vertex_UV2[1]):
- continue
- if hasVertexColor:
- vColor = vertices_Colors[vertex_index][index_UV]
- if (vColor.r != vertex_Color.r or vColor.g != vertex_Color.g or vColor.b != vertex_Color.b):
- continue
- if hasSkeleton:
- vSkWeight = vertices_sk_weights[vertex_index]
- vSkIndices = vertices_sk_indices[vertex_index]
- if not same_array(vSkWeight[index_UV], matricesWeights) or not same_array(vSkIndices[index_UV], matricesIndices):
- continue
- if vertices_indices[vertex_index][index_UV] >= subMeshVerticesStart:
- alreadySaved = True
- break
- index_UV += 1
- if (alreadySaved):
- # Reuse vertex
- index = vertices_indices[vertex_index][index_UV]
- else:
- # Export new one
- index = verticesCount
- alreadySavedVertices[vertex_index] = True
- vertices_Normals[vertex_index].append(normal)
- self.normals.append(normal)
- if hasUV:
- vertices_UVs[vertex_index].append(vertex_UV)
- self.uvs.append(vertex_UV[0])
- self.uvs.append(vertex_UV[1])
- if hasUV2:
- vertices_UV2s[vertex_index].append(vertex_UV2)
- self.uvs2.append(vertex_UV2[0])
- self.uvs2.append(vertex_UV2[1])
- if hasVertexColor:
- vertices_Colors[vertex_index].append(vertex_Color)
- self.colors.append(vertex_Color.r)
- self.colors.append(vertex_Color.g)
- self.colors.append(vertex_Color.b)
- self.colors.append(1.0)
- if hasSkeleton:
- vertices_sk_weights[vertex_index].append(matricesWeights)
- vertices_sk_indices[vertex_index].append(matricesIndices)
- nInfluencers = len(matricesWeights)
- totalInfluencers += nInfluencers
- if nInfluencers <= 8:
- influenceCounts[nInfluencers] += 1
- else:
- influenceCounts[0] += 1
- highestInfluenceObserved = nInfluencers if nInfluencers > highestInfluenceObserved else highestInfluenceObserved
- weightsPerVertex.append(matricesWeights)
- indicesPerVertex.append(matricesIndices)
- vertices_indices[vertex_index].append(index)
- self.positions.append(position)
- verticesCount += 1
- self.indices.append(index)
- indicesCount += 1
- self.subMeshes.append(SubMesh(materialIndex, subMeshVerticesStart, subMeshIndexStart, verticesCount - subMeshVerticesStart, indicesCount - subMeshIndexStart))
- if verticesCount > MAX_VERTEX_ELEMENTS:
- Main.warn('Due to multi-materials / Shapekeys & this meshes size, 32bit indices must be used. This may not run on all hardware.', 2)
- BakedMaterial.meshBakingClean(object)
- Main.log('num positions : ' + str(len(self.positions)), 2)
- Main.log('num normals : ' + str(len(self.normals )), 2)
- Main.log('num uvs : ' + str(len(self.uvs )), 2)
- Main.log('num uvs2 : ' + str(len(self.uvs2 )), 2)
- Main.log('num colors : ' + str(len(self.colors )), 2)
- Main.log('num indices : ' + str(len(self.indices )), 2)
- if hasSkeleton:
- Main.log('Skeleton stats: ', 2)
- self.toFixedInfluencers(weightsPerVertex, indicesPerVertex, object.data.maxInfluencers, highestInfluenceObserved)
- if (COMPRESS_MATRIX_INDICES):
- self.skeletonIndices = Mesh.packSkeletonIndices(self.skeletonIndices)
- if (self.numBoneInfluencers > 4):
- self.skeletonIndicesExtra = Mesh.packSkeletonIndices(self.skeletonIndicesExtra)
- Main.log('Total Influencers: ' + format_f(totalInfluencers), 3)
- Main.log('Avg # of influencers per vertex: ' + format_f(totalInfluencers / len(self.positions)), 3)
- Main.log('Highest # of influencers observed: ' + str(highestInfluenceObserved) + ', num vertices with this: ' + format_int(influenceCounts[highestInfluenceObserved if highestInfluenceObserved < 9 else 0]), 3)
- Main.log('exported as ' + str(self.numBoneInfluencers) + ' influencers', 3)
- nWeights = len(self.skeletonWeights) + (len(self.skeletonWeightsExtra) if hasattr(self, 'skeletonWeightsExtra') else 0)
- Main.log('num skeletonWeights and skeletonIndices: ' + str(nWeights), 3)
- numZeroAreaFaces = self.find_zero_area_faces()
- if numZeroAreaFaces > 0:
- Main.warn('# of 0 area faces found: ' + str(numZeroAreaFaces), 2)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def find_zero_area_faces(self):
- nFaces = int(len(self.indices) / 3)
- nZeroAreaFaces = 0
- for f in range(0, nFaces):
- faceOffset = f * 3
- p1 = self.positions[self.indices[faceOffset ]]
- p2 = self.positions[self.indices[faceOffset + 1]]
- p3 = self.positions[self.indices[faceOffset + 2]]
- if same_vertex(p1, p2) or same_vertex(p1, p3) or same_vertex(p2, p3): nZeroAreaFaces += 1
- return nZeroAreaFaces
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- @staticmethod
- def mesh_triangulate(mesh):
- try:
- import bmesh
- bm = bmesh.new()
- bm.from_mesh(mesh)
- bmesh.ops.triangulate(bm, faces = bm.faces)
- bm.to_mesh(mesh)
- mesh.calc_tessface()
- bm.free()
- except:
- pass
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def toFixedInfluencers(self, weightsPerVertex, indicesPerVertex, maxInfluencers, highestObserved):
- if (maxInfluencers > 8 or maxInfluencers < 1):
- maxInfluencers = 8
- Main.warn('Maximum # of influencers invalid, set to 8', 3)
- self.numBoneInfluencers = maxInfluencers if maxInfluencers < highestObserved else highestObserved
- needExtras = self.numBoneInfluencers > 4
- maxInfluencersExceeded = 0
- fixedWeights = []
- fixedIndices = []
- fixedWeightsExtra = []
- fixedIndicesExtra = []
- for i in range(len(weightsPerVertex)):
- weights = weightsPerVertex[i]
- indices = indicesPerVertex[i]
- nInfluencers = len(weights)
- if (nInfluencers > self.numBoneInfluencers):
- maxInfluencersExceeded += 1
- Mesh.sortByDescendingInfluence(weights, indices)
- for j in range(4):
- fixedWeights.append(weights[j] if nInfluencers > j else 0.0)
- fixedIndices.append(indices[j] if nInfluencers > j else 0 )
- if needExtras:
- for j in range(4, 8):
- fixedWeightsExtra.append(weights[j] if nInfluencers > j else 0.0)
- fixedIndicesExtra.append(indices[j] if nInfluencers > j else 0 )
- self.skeletonWeights = fixedWeights
- self.skeletonIndices = fixedIndices
- if needExtras:
- self.skeletonWeightsExtra = fixedWeightsExtra
- self.skeletonIndicesExtra = fixedIndicesExtra
- if maxInfluencersExceeded > 0:
- Main.warn('Maximum # of influencers exceeded for ' + format_int(maxInfluencersExceeded) + ' vertices, extras ignored', 3)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # sorts one set of weights & indices by descending weight, by reference
- # not shown to help with MakeHuman, but did not hurt. In just so it is not lost for future.
- @staticmethod
- def sortByDescendingInfluence(weights, indices):
- notSorted = True
- while(notSorted):
- notSorted = False
- for idx in range(1, len(weights)):
- if weights[idx - 1] < weights[idx]:
- tmp = weights[idx]
- weights[idx ] = weights[idx - 1]
- weights[idx - 1] = tmp
- tmp = indices[idx]
- indices[idx ] = indices[idx - 1]
- indices[idx - 1] = tmp
- notSorted = True
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # assume that toFixedInfluencers has already run, which ensures indices length is a multiple of 4
- @staticmethod
- def packSkeletonIndices(indices):
- compressedIndices = []
- for i in range(math.floor(len(indices) / 4)):
- idx = i * 4
- matricesIndicesCompressed = indices[idx ]
- matricesIndicesCompressed += indices[idx + 1] << 8
- matricesIndicesCompressed += indices[idx + 2] << 16
- matricesIndicesCompressed += indices[idx + 3] << 24
- compressedIndices.append(matricesIndicesCompressed)
- return compressedIndices
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_string(file_handler, 'id', self.name)
- if hasattr(self, 'parentId'): write_string(file_handler, 'parentId', self.parentId)
- if hasattr(self, 'materialId'): write_string(file_handler, 'materialId', self.materialId)
- write_int(file_handler, 'billboardMode', self.billboardMode)
- write_vector(file_handler, 'position', self.position)
- if hasattr(self, "rotationQuaternion"):
- write_quaternion(file_handler, 'rotationQuaternion', self.rotationQuaternion)
- else:
- write_vector(file_handler, 'rotation', self.rotation)
- write_vector(file_handler, 'scaling', self.scaling)
- write_bool(file_handler, 'isVisible', self.isVisible)
- write_bool(file_handler, 'freezeWorldMatrix', self.freezeWorldMatrix)
- write_bool(file_handler, 'isEnabled', self.isEnabled)
- write_bool(file_handler, 'checkCollisions', self.checkCollisions)
- write_bool(file_handler, 'receiveShadows', self.receiveShadows)
- if hasattr(self, 'physicsImpostor'):
- write_int(file_handler, 'physicsImpostor', self.physicsImpostor)
- write_float(file_handler, 'physicsMass', self.physicsMass)
- write_float(file_handler, 'physicsFriction', self.physicsFriction)
- write_float(file_handler, 'physicsRestitution', self.physicsRestitution)
- # Geometry
- if hasattr(self, 'skeletonId'):
- write_int(file_handler, 'skeletonId', self.skeletonId)
- write_int(file_handler, 'numBoneInfluencers', self.numBoneInfluencers)
- write_vector_array(file_handler, 'positions', self.positions)
- write_vector_array(file_handler, 'normals' , self.normals )
- if len(self.uvs) > 0:
- write_array(file_handler, 'uvs', self.uvs)
- if len(self.uvs2) > 0:
- write_array(file_handler, 'uvs2', self.uvs2)
- if len(self.colors) > 0:
- write_array(file_handler, 'colors', self.colors)
- if hasattr(self, 'skeletonWeights'):
- write_array(file_handler, 'matricesWeights', self.skeletonWeights)
- write_array(file_handler, 'matricesIndices', self.skeletonIndices)
- if hasattr(self, 'skeletonWeightsExtra'):
- write_array(file_handler, 'matricesWeightsExtra', self.skeletonWeightsExtra)
- write_array(file_handler, 'matricesIndicesExtra', self.skeletonIndicesExtra)
- write_array(file_handler, 'indices', self.indices)
- # Sub meshes
- file_handler.write('\n,"subMeshes":[')
- first = True
- for subMesh in self.subMeshes:
- if first == False:
- file_handler.write(',')
- subMesh.to_scene_file(file_handler)
- first = False
- file_handler.write(']')
- super().to_scene_file(file_handler) # Animations
- # Instances
- first = True
- file_handler.write('\n,"instances":[')
- for instance in self.instances:
- if first == False:
- file_handler.write(',')
- instance.to_scene_file(file_handler)
- first = False
- file_handler.write(']')
- # Close mesh
- file_handler.write('}\n')
- self.alreadyExported = True
- #===============================================================================
- class MeshInstance:
- def __init__(self, name, position, rotation, rotationQuaternion, scaling, freezeWorldMatrix):
- self.name = name
- self.position = position
- if rotation is not None:
- self.rotation = rotation
- if rotationQuaternion is not None:
- self.rotationQuaternion = rotationQuaternion
- self.scaling = scaling
- self.freezeWorldMatrix = freezeWorldMatrix
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_vector(file_handler, 'position', self.position)
- if hasattr(self, 'rotation'):
- write_vector(file_handler, 'rotation', self.rotation)
- else:
- write_quaternion(file_handler, 'rotationQuaternion', self.rotationQuaternion)
- write_vector(file_handler, 'scaling', self.scaling)
- # freeze World Matrix currently ignored for instances
- write_bool(file_handler, 'freezeWorldMatrix', self.freezeWorldMatrix)
- file_handler.write('}')
- #===============================================================================
- class Node(FCurveAnimatable):
- def __init__(self, node):
- Main.log('processing begun of node: ' + node.name)
- self.define_animations(node, True, True, True) #Should animations be done when forcedParent
- self.name = node.name
- if node.parent and node.parent.type != 'ARMATURE':
- self.parentId = node.parent.name
- loc, rot, scale = node.matrix_local.decompose()
- self.position = loc
- if node.rotation_mode == 'QUATERNION':
- self.rotationQuaternion = rot
- else:
- self.rotation = scale_vector(rot.to_euler('XYZ'), -1)
- self.scaling = scale
- self.isVisible = False
- self.isEnabled = True
- self.checkCollisions = False
- self.billboardMode = BILLBOARDMODE_NONE
- self.castShadows = False
- self.receiveShadows = False
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_string(file_handler, 'id', self.name)
- if hasattr(self, 'parentId'): write_string(file_handler, 'parentId', self.parentId)
- write_vector(file_handler, 'position', self.position)
- if hasattr(self, "rotationQuaternion"):
- write_quaternion(file_handler, "rotationQuaternion", self.rotationQuaternion)
- else:
- write_vector(file_handler, 'rotation', self.rotation)
- write_vector(file_handler, 'scaling', self.scaling)
- write_bool(file_handler, 'isVisible', self.isVisible)
- write_bool(file_handler, 'isEnabled', self.isEnabled)
- write_bool(file_handler, 'checkCollisions', self.checkCollisions)
- write_int(file_handler, 'billboardMode', self.billboardMode)
- write_bool(file_handler, 'receiveShadows', self.receiveShadows)
- super().to_scene_file(file_handler) # Animations
- file_handler.write('}')
- #===============================================================================
- class SubMesh:
- def __init__(self, materialIndex, verticesStart, indexStart, verticesCount, indexCount):
- self.materialIndex = materialIndex
- self.verticesStart = verticesStart
- self.indexStart = indexStart
- self.verticesCount = verticesCount
- self.indexCount = indexCount
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_int(file_handler, 'materialIndex', self.materialIndex, True)
- write_int(file_handler, 'verticesStart', self.verticesStart)
- write_int(file_handler, 'verticesCount', self.verticesCount)
- write_int(file_handler, 'indexStart' , self.indexStart)
- write_int(file_handler, 'indexCount' , self.indexCount)
- file_handler.write('}')
- #===============================================================================
- class Bone:
- def __init__(self, bone, skeleton, bonesSoFar):
- self.index = len(bonesSoFar)
- Main.log('processing begun of bone: ' + bone.name + ', index: '+ str(self.index), 2)
- self.name = bone.name
- self.length = bone.length
- self.posedBone = bone # record so can be used by get_matrix, called by append_animation_pose
- self.parentBone = bone.parent
- self.matrix_world = skeleton.matrix_world
- self.matrix = self.get_bone_matrix(True)
- self.parentBoneIndex = Skeleton.get_bone(bone.parent.name, bonesSoFar).index if bone.parent else -1
- #animation
- if (skeleton.animation_data):
- self.animation = Animation(ANIMATIONTYPE_MATRIX, ANIMATIONLOOPMODE_CYCLE, 'anim', '_matrix')
- self.previousBoneMatrix = None
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def append_animation_pose(self, frame, force = False):
- currentBoneMatrix = self.get_bone_matrix(True)
- if (force or not same_matrix4(currentBoneMatrix, self.previousBoneMatrix)):
- self.animation.frames.append(frame)
- self.animation.values.append(currentBoneMatrix)
- self.previousBoneMatrix = currentBoneMatrix
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def set_rest_pose(self, editBone):
- self.rest = Bone.get_matrix(editBone, self.matrix_world, True)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def get_bone_matrix(self, doParentMult):
- return Bone.get_matrix(self.posedBone, self.matrix_world, doParentMult)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- @staticmethod
- def get_matrix(bone, matrix_world, doParentMult):
- SystemMatrix = mathutils.Matrix.Scale(-1, 4, mathutils.Vector((0, 0, 1))) * mathutils.Matrix.Rotation(math.radians(-90), 4, 'X')
- if (bone.parent and doParentMult):
- return (SystemMatrix * matrix_world * bone.parent.matrix).inverted() * (SystemMatrix * matrix_world * bone.matrix)
- else:
- return SystemMatrix * matrix_world * bone.matrix
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('\n{')
- write_string(file_handler, 'name', self.name, True)
- write_int(file_handler, 'index', self.index)
- write_matrix4(file_handler, 'matrix', self.matrix)
- write_matrix4(file_handler, 'rest', self.rest)
- write_int(file_handler, 'parentBoneIndex', self.parentBoneIndex)
- write_float(file_handler, 'length', self.length)
- #animation
- if hasattr(self, 'animation'):
- file_handler.write('\n,"animation":')
- self.animation.to_scene_file(file_handler)
- file_handler.write('}')
- #===============================================================================
- class Skeleton:
- def __init__(self, skeleton, scene, id):
- Main.log('processing begun of skeleton: ' + skeleton.name + ', id: '+ str(id))
- self.name = skeleton.name
- self.id = id
- self.bones = []
- for bone in skeleton.pose.bones:
- if scene.ignoreIKBones and ('.ik' in bone.name.lower() or 'ik.' in bone.name.lower() ):
- Main.log('Ignoring IK bone: ' + bone.name, 2)
- continue
- self.bones.append(Bone(bone, skeleton, self.bones))
- if (skeleton.animation_data):
- self.ranges = []
- frameOffset = 0
- for action in bpy.data.actions:
- # get the range / assigning the action to the object
- animationRange = AnimationRange.actionPrep(skeleton, action, FRAME_BASED_ANIMATION, frameOffset)
- if animationRange is None:
- continue
- Main.log('processing action ' + animationRange.to_string(), 2)
- self.ranges.append(animationRange)
- nFrames = len(animationRange.frames_in)
- for idx in range(nFrames):
- bpy.context.scene.frame_set(animationRange.frames_in[idx])
- firstOrLast = idx == 0 or idx == nFrames - 1
- for bone in self.bones:
- bone.append_animation_pose(animationRange.frames_out[idx], firstOrLast)
- frameOffset = animationRange.frame_end
- # mode_set's only work when there is an active object, switch bones to edit mode to rest position
- scene.objects.active = skeleton
- bpy.ops.object.mode_set(mode='EDIT')
- # dimensions when in edit mode, are those at rest
- self.dimensions = skeleton.dimensions
- # you need to access edit_bones from skeleton.data not skeleton.pose when in edit mode
- for editBone in skeleton.data.edit_bones:
- for myBoneObj in self.bones:
- if editBone.name == myBoneObj.name:
- myBoneObj.set_rest_pose(editBone)
- break
- bpy.ops.object.mode_set(mode='OBJECT')
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Since IK bones could be being skipped, looking up index of bone in second pass of mesh required
- def get_index_of_bone(self, boneName):
- return Skeleton.get_bone(boneName, self.bones).index
-
- @staticmethod
- def get_bone(boneName, bones):
- for bone in bones:
- if boneName == bone.name:
- return bone
- # should not happen, but if it does clearly a bug, so terminate
- raise Exception('bone name "' + boneName + '" not found in skeleton')
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_int(file_handler, 'id', self.id) # keep int for legacy of original exporter
- write_vector(file_handler, 'dimensionsAtRest', self.dimensions)
- file_handler.write(',"bones":[')
- first = True
- for bone in self.bones:
- if first != True:
- file_handler.write(',')
- first = False
- bone.to_scene_file(file_handler)
- file_handler.write(']')
- if hasattr(self, 'ranges'):
- file_handler.write('\n,"ranges":[')
- first = True
- for range in self.ranges:
- if first != True:
- file_handler.write(',')
- first = False
- range.to_scene_file(file_handler)
- file_handler.write(']')
- file_handler.write('}')
- #===============================================================================
- class Camera(FCurveAnimatable):
- def __init__(self, camera):
- if camera.parent and camera.parent.type != 'ARMATURE':
- self.parentId = camera.parent.name
- self.CameraType = camera.data.CameraType
- self.name = camera.name
- Main.log('processing begun of camera (' + self.CameraType + '): ' + self.name)
- self.define_animations(camera, True, True, False, math.pi / 2)
- self.position = camera.location
- # for quaternions, convert to euler XYZ, otherwise, use the default rotation_euler
- eul = camera.rotation_quaternion.to_euler("XYZ") if camera.rotation_mode == 'QUATERNION' else camera.rotation_euler
- self.rotation = mathutils.Vector((-eul[0] + math.pi / 2, eul[1], -eul[2]))
- self.fov = camera.data.angle
- self.minZ = camera.data.clip_start
- self.maxZ = camera.data.clip_end
- self.speed = 1.0
- self.inertia = 0.9
- self.checkCollisions = camera.data.checkCollisions
- self.applyGravity = camera.data.applyGravity
- self.ellipsoid = camera.data.ellipsoid
- self.Camera3DRig = camera.data.Camera3DRig
- self.interaxialDistance = camera.data.interaxialDistance
- for constraint in camera.constraints:
- if constraint.type == 'TRACK_TO':
- self.lockedTargetId = constraint.target.name
- break
- if self.CameraType == ARC_ROTATE_CAM or self.CameraType == FOLLOW_CAM:
- if not hasattr(self, 'lockedTargetId'):
- Main.warn('Camera type with manditory target specified, but no target to track set. Ignored', 2)
- self.fatalProblem = True
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def update_for_target_attributes(self, meshesAndNodes):
- if not hasattr(self, 'lockedTargetId'): return
- # find the actual mesh tracking, so properties can be derrived
- targetFound = False
- for mesh in meshesAndNodes:
- if mesh.name == self.lockedTargetId:
- targetMesh = mesh
- targetFound = True
- break;
- xApart = 3 if not targetFound else self.position.x - targetMesh.position.x
- yApart = 3 if not targetFound else self.position.y - targetMesh.position.y
- zApart = 3 if not targetFound else self.position.z - targetMesh.position.z
- distance3D = math.sqrt(xApart * xApart + yApart * yApart + zApart * zApart)
- alpha = math.atan2(yApart, xApart);
- beta = math.atan2(yApart, zApart);
- if self.CameraType == FOLLOW_CAM:
- self.followHeight = zApart
- self.followDistance = distance3D
- self.followRotation = 90 + (alpha * 180 / math.pi)
- elif self.CameraType == self.CameraType == ARC_ROTATE_CAM:
- self.arcRotAlpha = alpha
- self.arcRotBeta = beta
- self.arcRotRadius = distance3D
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_string(file_handler, 'id', self.name)
- write_vector(file_handler, 'position', self.position)
- write_vector(file_handler, 'rotation', self.rotation)
- write_float(file_handler, 'fov', self.fov)
- write_float(file_handler, 'minZ', self.minZ)
- write_float(file_handler, 'maxZ', self.maxZ)
- write_float(file_handler, 'speed', self.speed)
- write_float(file_handler, 'inertia', self.inertia)
- write_bool(file_handler, 'checkCollisions', self.checkCollisions)
- write_bool(file_handler, 'applyGravity', self.applyGravity)
- write_array3(file_handler, 'ellipsoid', self.ellipsoid)
- # always assign rig, even when none, Reason: Could have VR camera with different Rig than default
- write_int(file_handler, 'cameraRigMode', self.Camera3DRig)
- write_float(file_handler, 'interaxial_distance', self.interaxialDistance)
- write_string(file_handler, 'type', self.CameraType)
- if hasattr(self, 'parentId'): write_string(file_handler, 'parentId', self.parentId)
- if self.CameraType == FOLLOW_CAM:
- write_float(file_handler, 'heightOffset', self.followHeight)
- write_float(file_handler, 'radius', self.followDistance)
- write_float(file_handler, 'rotationOffset', self.followRotation)
- elif self.CameraType == ARC_ROTATE_CAM:
- write_float(file_handler, 'alpha', self.arcRotAlpha)
- write_float(file_handler, 'beta', self.arcRotBeta)
- write_float(file_handler, 'radius', self.arcRotRadius)
- if hasattr(self, 'lockedTargetId'):
- write_string(file_handler, 'lockedTargetId', self.lockedTargetId)
- super().to_scene_file(file_handler) # Animations
- file_handler.write('}')
- #===============================================================================
- class Light(FCurveAnimatable):
- def __init__(self, light):
- if light.parent and light.parent.type != 'ARMATURE':
- self.parentId = light.parent.name
- self.name = light.name
- Main.log('processing begun of light (' + light.data.type + '): ' + self.name)
- self.define_animations(light, False, True, False)
- light_type_items = {'POINT': POINT_LIGHT, 'SUN': DIRECTIONAL_LIGHT, 'SPOT': SPOT_LIGHT, 'HEMI': HEMI_LIGHT, 'AREA': POINT_LIGHT}
- self.light_type = light_type_items[light.data.type]
- if self.light_type == POINT_LIGHT:
- self.position = light.location
- if hasattr(light.data, 'use_sphere'):
- if light.data.use_sphere:
- self.range = light.data.distance
- elif self.light_type == DIRECTIONAL_LIGHT:
- self.position = light.location
- self.direction = Light.get_direction(light.matrix_local)
- elif self.light_type == SPOT_LIGHT:
- self.position = light.location
- self.direction = Light.get_direction(light.matrix_local)
- self.angle = light.data.spot_size
- self.exponent = light.data.spot_blend * 2
- if light.data.use_sphere:
- self.range = light.data.distance
- else:
- # Hemi
- matrix_local = light.matrix_local.copy()
- matrix_local.translation = mathutils.Vector((0, 0, 0))
- self.direction = (mathutils.Vector((0, 0, -1)) * matrix_local)
- self.direction = scale_vector(self.direction, -1)
- self.groundColor = mathutils.Color((0, 0, 0))
- self.intensity = light.data.energy
- self.diffuse = light.data.color if light.data.use_diffuse else mathutils.Color((0, 0, 0))
- self.specular = light.data.color if light.data.use_specular else mathutils.Color((0, 0, 0))
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_string(file_handler, 'id', self.name)
- write_float(file_handler, 'type', self.light_type)
- if hasattr(self, 'parentId' ): write_string(file_handler, 'parentId' , self.parentId )
- if hasattr(self, 'position' ): write_vector(file_handler, 'position' , self.position )
- if hasattr(self, 'direction' ): write_vector(file_handler, 'direction' , self.direction )
- if hasattr(self, 'angle' ): write_float (file_handler, 'angle' , self.angle )
- if hasattr(self, 'exponent' ): write_float (file_handler, 'exponent' , self.exponent )
- if hasattr(self, 'groundColor'): write_color (file_handler, 'groundColor', self.groundColor)
- if hasattr(self, 'range' ): write_float (file_handler, 'range' , self.range )
- write_float(file_handler, 'intensity', self.intensity)
- write_color(file_handler, 'diffuse', self.diffuse)
- write_color(file_handler, 'specular', self.specular)
- super().to_scene_file(file_handler) # Animations
- file_handler.write('}')
- @staticmethod
- def get_direction(matrix):
- return (matrix.to_3x3() * mathutils.Vector((0.0, 0.0, -1.0))).normalized()
- #===============================================================================
- class ShadowGenerator:
- def __init__(self, lamp, meshesAndNodes, scene):
- Main.log('processing begun of shadows for light: ' + lamp.name)
- self.lightId = lamp.name
- self.mapSize = lamp.data.shadowMapSize
- self.shadowBias = lamp.data.shadowBias
- if lamp.data.shadowMap == ESM_SHADOWS:
- self.useExponentialShadowMap = True
- elif lamp.data.shadowMap == POISSON_SHADOWS:
- self.usePoissonSampling = True
- elif lamp.data.shadowMap == BLUR_ESM_SHADOWS:
- self.useBlurExponentialShadowMap = True
- self.shadowBlurScale = lamp.data.shadowBlurScale
- self.shadowBlurBoxOffset = lamp.data.shadowBlurBoxOffset
- # .babylon specific section
- self.shadowCasters = []
- for mesh in meshesAndNodes:
- if (mesh.castShadows):
- self.shadowCasters.append(mesh.name)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_int(file_handler, 'mapSize', self.mapSize, True)
- write_string(file_handler, 'lightId', self.lightId)
- write_float(file_handler, 'bias', self.shadowBias)
- if hasattr(self, 'useExponentialShadowMap') :
- write_bool(file_handler, 'useExponentialShadowMap', self.useExponentialShadowMap)
- elif hasattr(self, 'usePoissonSampling'):
- write_bool(file_handler, 'usePoissonSampling', self.usePoissonSampling)
- elif hasattr(self, 'useBlurExponentialShadowMap'):
- write_bool(file_handler, 'useBlurExponentialShadowMap', self.useBlurExponentialShadowMap)
- write_int(file_handler, 'blurScale', self.shadowBlurScale)
- write_int(file_handler, 'blurBoxOffset', self.shadowBlurBoxOffset)
- file_handler.write(',"renderList":[')
- first = True
- for caster in self.shadowCasters:
- if first != True:
- file_handler.write(',')
- first = False
- file_handler.write('"' + caster + '"')
- file_handler.write(']')
- file_handler.write('}')
- #===============================================================================
- class MultiMaterial:
- def __init__(self, material_slots, idx):
- self.name = Main.nameSpace + '.' + 'Multimaterial#' + str(idx)
- Main.log('processing begun of multimaterial: ' + self.name, 2)
- self.material_slots = material_slots
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_string(file_handler, 'id', self.name)
- file_handler.write(',"materials":[')
- first = True
- for material in self.material_slots:
- if first != True:
- file_handler.write(',')
- file_handler.write('"' + material.name +'"')
- first = False
- file_handler.write(']')
- file_handler.write('}')
- #===============================================================================
- class Texture:
- def __init__(self, slot, level, textureOrImage, mesh, exporter):
- wasBaked = not hasattr(textureOrImage, 'uv_layer')
- if wasBaked:
- image = textureOrImage
- texture = None
- repeat = False
- self.hasAlpha = False
- self.coordinatesIndex = 0
- else:
- texture = textureOrImage
- image = texture.texture.image
- repeat = texture.texture.extension == 'REPEAT'
- self.hasAlpha = texture.texture.use_alpha
- usingMap = texture.uv_layer
- if len(usingMap) == 0:
- usingMap = mesh.data.uv_textures[0].name
- Main.log('Image texture found, type: ' + slot + ', mapped using: "' + usingMap + '"', 4)
- if mesh.data.uv_textures[0].name == usingMap:
- self.coordinatesIndex = 0
- elif mesh.data.uv_textures[1].name == usingMap:
- self.coordinatesIndex = 1
- else:
- Main.warn('Texture is not mapped as UV or UV2, assigned 1', 5)
- self.coordinatesIndex = 0
- # always write the file out, since base64 encoding is easiest from a file
- try:
- imageFilepath = os.path.normpath(bpy.path.abspath(image.filepath))
- basename = os.path.basename(imageFilepath)
- internalImage = image.packed_file or wasBaked
- # when coming from either a packed image or a baked image, then save_render
- if internalImage:
- if exporter.scene.inlineTextures:
- textureFile = os.path.join(exporter.textureDir, basename + "temp")
- else:
- textureFile = os.path.join(exporter.textureDir, basename)
- image.save_render(textureFile)
- # when backed by an actual file, copy to target dir, unless inlining
- else:
- textureFile = bpy.path.abspath(image.filepath)
- if not exporter.scene.inlineTextures:
- shutil.copy(textureFile, exporter.textureDir)
- except:
- ex = sys.exc_info()
- Main.warn('Error encountered processing image file: ' + ', Error: '+ str(ex[1]))
- if exporter.scene.inlineTextures:
- # base64 is easiest from a file, so sometimes a temp file was made above; need to delete those
- with open(textureFile, "rb") as image_file:
- asString = base64.b64encode(image_file.read()).decode()
- self.encoded_URI = 'data:image/' + image.file_format + ';base64,' + asString
- if internalImage:
- os.remove(textureFile)
- # capture texture attributes
- self.slot = slot
- self.name = basename
- self.level = level
- if (texture and texture.mapping == 'CUBE'):
- self.coordinatesMode = CUBIC_MODE
- if (texture and texture.mapping == 'SPHERE'):
- self.coordinatesMode = SPHERICAL_MODE
- else:
- self.coordinatesMode = EXPLICIT_MODE
- self.uOffset = texture.offset.x if texture else 0.0
- self.vOffset = texture.offset.y if texture else 0.0
- self.uScale = texture.scale.x if texture else 1.0
- self.vScale = texture.scale.y if texture else 1.0
- self.uAng = 0
- self.vAng = 0
- self.wAng = 0
- if (repeat):
- if (texture.texture.use_mirror_x):
- self.wrapU = MIRROR_ADDRESSMODE
- else:
- self.wrapU = WRAP_ADDRESSMODE
- if (texture.texture.use_mirror_y):
- self.wrapV = MIRROR_ADDRESSMODE
- else:
- self.wrapV = WRAP_ADDRESSMODE
- else:
- self.wrapU = CLAMP_ADDRESSMODE
- self.wrapV = CLAMP_ADDRESSMODE
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write(', \n"' + self.slot + '":{')
- write_string(file_handler, 'name', self.name, True)
- write_float(file_handler, 'level', self.level)
- write_float(file_handler, 'hasAlpha', self.hasAlpha)
- write_int(file_handler, 'coordinatesMode', self.coordinatesMode)
- write_float(file_handler, 'uOffset', self.uOffset)
- write_float(file_handler, 'vOffset', self.vOffset)
- write_float(file_handler, 'uScale', self.uScale)
- write_float(file_handler, 'vScale', self.vScale)
- write_float(file_handler, 'uAng', self.uAng)
- write_float(file_handler, 'vAng', self.vAng)
- write_float(file_handler, 'wAng', self.wAng)
- write_int(file_handler, 'wrapU', self.wrapU)
- write_int(file_handler, 'wrapV', self.wrapV)
- write_int(file_handler, 'coordinatesIndex', self.coordinatesIndex)
- if hasattr(self,'encoded_URI'):
- write_string(file_handler, 'base64String', self.encoded_URI)
- file_handler.write('}')
- #===============================================================================
- # need to evaluate the need to bake a mesh before even starting; class also stores specific types of bakes
- class BakingRecipe:
- def __init__(self, mesh, forceBaking = False):
- # initialize all members
- self.needsBaking = forceBaking
- self.diffuseBaking = forceBaking
- self.ambientBaking = False
- self.opacityBaking = False
- self.reflectionBaking = False
- self.emissiveBaking = False
- self.bumpBaking = False
- self.specularBaking = False
- # need to make sure a single render
- self.cyclesRender = False
- blenderRender = False
- # transfer from Mesh custom properties
- self.bakeSize = mesh.data.bakeSize
- self.bakeQuality = mesh.data.bakeQuality # for lossy compression formats
- # accumulators set by Blender Game
- self.backFaceCulling = True # used only when baking
- self.billboardMode = BILLBOARDMODE_ALL if len(mesh.material_slots) == 1 and mesh.material_slots[0].material.game_settings.face_orientation == 'BILLBOARD' else BILLBOARDMODE_NONE
- # Cycles specific, need to get the node trees of each material
- self.nodeTrees = []
- for material_slot in mesh.material_slots:
- # a material slot is not a reference to an actual material; need to look up
- material = material_slot.material
- self.backFaceCulling &= material.game_settings.use_backface_culling
- # testing for Cycles renderer has to be different
- if material.use_nodes == True:
- self.needsBaking = True
- self.cyclesRender = True
- self.nodeTrees.append(material.node_tree)
- for node in material.node_tree.nodes:
- id = node.bl_idname
- if id == 'ShaderNodeBsdfDiffuse':
- self.diffuseBaking = True
- if id == 'ShaderNodeAmbientOcclusion':
- self.ambientBaking = True
- # there is no opacity baking for Cycles AFAIK
- if id == '':
- self.opacityBaking = True
- if id == 'ShaderNodeEmission':
- self.emissiveBaking = True
- if id == 'ShaderNodeNormal' or id == 'ShaderNodeNormalMap':
- self.bumpBaking = True
- if id == '':
- self.specularBaking = True
- else:
- blenderRender = True
- nDiffuseImages = 0
- nReflectionImages = 0
- nAmbientImages = 0
- nOpacityImages = 0
- nEmissiveImages = 0
- nBumpImages = 0
- nSpecularImages = 0
- textures = [mtex for mtex in material.texture_slots if mtex and mtex.texture]
- for mtex in textures:
- # ignore empty slots
- if mtex.texture.type == 'NONE':
- continue
- # for images, just need to make sure there is only 1 per type
- if mtex.texture.type == 'IMAGE' and not forceBaking:
- if mtex.use_map_diffuse or mtex.use_map_color_diffuse:
- if mtex.texture_coords == 'REFLECTION':
- nReflectionImages += 1
- else:
- nDiffuseImages += 1
- if mtex.use_map_ambient:
- nAmbientImages += 1
- if mtex.use_map_alpha:
- nOpacityImages += 1
- if mtex.use_map_emit:
- nEmissiveImages += 1
- if mtex.use_map_normal:
- nBumpImages += 1
- if mtex.use_map_color_spec:
- nSpecularImages += 1
- else:
- self.needsBaking = True
- if mtex.use_map_diffuse or mtex.use_map_color_diffuse:
- if mtex.texture_coords == 'REFLECTION':
- self.reflectionBaking = True
- else:
- self.diffuseBaking = True
- if mtex.use_map_ambient:
- self.ambientBaking = True
- if mtex.use_map_alpha:
- self.opacityBaking = True
- if mtex.use_map_emit:
- self.emissiveBaking = True
- if mtex.use_map_normal:
- self.bumpBaking = True
- if mtex.use_map_color_spec:
- self.specularBaking = True
- # 2nd pass 2 check for multiples of a given image type
- if nDiffuseImages > 1:
- self.needsBaking = self.diffuseBaking = True
- if nReflectionImages > 1:
- self.needsBaking = self.nReflectionImages = True
- if nAmbientImages > 1:
- self.needsBaking = self.ambientBaking = True
- if nOpacityImages > 1:
- self.needsBaking = self.opacityBaking = True
- if nEmissiveImages > 1:
- self.needsBaking = self.emissiveBaking = True
- if nBumpImages > 1:
- self.needsBaking = self.bumpBaking = True
- if nSpecularImages > 1:
- self.needsBaking = self.specularBaking = True
- self.multipleRenders = blenderRender and self.cyclesRender
- # check for really old .blend file, eg. 2.49, to ensure that everything requires exists
- if self.needsBaking and bpy.data.screens.find('UV Editing') == -1:
- Main.warn('Contains material requiring baking, but resources not available. Probably .blend very old', 2)
- self.needsBaking = False
- #===============================================================================
- # Not intended to be instanced directly
- class Material:
- def __init__(self, checkReadyOnlyOnce):
- self.checkReadyOnlyOnce = checkReadyOnlyOnce
- # first pass of textures, either appending image type or recording types of bakes to do
- self.textures = []
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_string(file_handler, 'id', self.name)
- write_color(file_handler, 'ambient', self.ambient)
- write_color(file_handler, 'diffuse', self.diffuse)
- write_color(file_handler, 'specular', self.specular)
- write_color(file_handler, 'emissive', self.emissive)
- write_float(file_handler, 'specularPower', self.specularPower)
- write_float(file_handler, 'alpha', self.alpha)
- write_bool(file_handler, 'backFaceCulling', self.backFaceCulling)
- write_bool(file_handler, 'checkReadyOnlyOnce', self.checkReadyOnlyOnce)
- for texSlot in self.textures:
- texSlot.to_scene_file(file_handler)
- file_handler.write('}')
- #===============================================================================
- class StdMaterial(Material):
- def __init__(self, material_slot, exporter, mesh):
- super().__init__(mesh.data.checkReadyOnlyOnce)
- nameSpace = Main.nameSpace if mesh.data.materialNameSpace == DEFAULT_MATERIAL_NAMESPACE else mesh.data.materialNameSpace
- self.name = nameSpace + '.' + material_slot.name
- Main.log('processing begun of Standard material: ' + material_slot.name, 2)
- # a material slot is not a reference to an actual material; need to look up
- material = material_slot.material
- self.ambient = material.ambient * material.diffuse_color
- self.diffuse = material.diffuse_intensity * material.diffuse_color
- self.specular = material.specular_intensity * material.specular_color
- self.emissive = material.emit * material.diffuse_color
- self.specularPower = material.specular_hardness
- self.alpha = material.alpha
- self.backFaceCulling = material.game_settings.use_backface_culling
- textures = [mtex for mtex in material.texture_slots if mtex and mtex.texture]
- for mtex in textures:
- # test should be un-neccessary, since should be a BakedMaterial; just for completeness
- if (mtex.texture.type != 'IMAGE'):
- continue
- elif not mtex.texture.image:
- Main.warn('Material has un-assigned image texture: "' + mtex.name + '" ignored', 3)
- continue
- elif len(mesh.data.uv_textures) == 0:
- Main.warn('Mesh has no UV maps, material: "' + mtex.name + '" ignored', 3)
- continue
- if mtex.use_map_diffuse or mtex.use_map_color_diffuse:
- if mtex.texture_coords == 'REFLECTION':
- Main.log('Reflection texture found "' + mtex.name + '"', 3)
- self.textures.append(Texture('reflectionTexture', mtex.diffuse_color_factor, mtex, mesh, exporter))
- else:
- Main.log('Diffuse texture found "' + mtex.name + '"', 3)
- self.textures.append(Texture('diffuseTexture', mtex.diffuse_color_factor, mtex, mesh, exporter))
- if mtex.use_map_ambient:
- Main.log('Ambient texture found "' + mtex.name + '"', 3)
- self.textures.append(Texture('ambientTexture', mtex.ambient_factor, mtex, mesh, exporter))
- if mtex.use_map_alpha:
- if self.alpha > 0:
- Main.log('Opacity texture found "' + mtex.name + '"', 3)
- self.textures.append(Texture('opacityTexture', mtex.alpha_factor, mtex, mesh, exporter))
- else:
- Main.warn('Opacity non-std way to indicate opacity, use material alpha to also use Opacity texture', 4)
- self.alpha = 1
- if mtex.use_map_emit:
- Main.log('Emissive texture found "' + mtex.name + '"', 3)
- self.textures.append(Texture('emissiveTexture', mtex.emit_factor, mtex, mesh, exporter))
- if mtex.use_map_normal:
- Main.log('Bump texture found "' + mtex.name + '"', 3)
- self.textures.append(Texture('bumpTexture', 1.0 / mtex.normal_factor, mtex, mesh, exporter))
- if mtex.use_map_color_spec:
- Main.log('Specular texture found "' + mtex.name + '"', 3)
- self.textures.append(Texture('specularTexture', mtex.specular_color_factor, mtex, mesh, exporter))
- #===============================================================================
- class BakedMaterial(Material):
- def __init__(self, exporter, mesh, recipe):
- super().__init__(mesh.data.checkReadyOnlyOnce)
- nameSpace = Main.nameSpace if mesh.data.materialNameSpace == DEFAULT_MATERIAL_NAMESPACE else mesh.data.materialNameSpace
- self.name = nameSpace + '.' + mesh.name
- Main.log('processing begun of baked material: ' + mesh.name, 2)
- # any baking already took in the values. Do not want to apply them again, but want shadows to show.
- # These are the default values from StandardMaterials
- self.ambient = mathutils.Color((0, 0, 0))
- self.diffuse = mathutils.Color((0.8, 0.8, 0.8)) # needed for shadows, but not change anything else
- self.specular = mathutils.Color((1, 1, 1))
- self.emissive = mathutils.Color((0, 0, 0))
- self.specularPower = 64
- self.alpha = 1.0
- self.backFaceCulling = recipe.backFaceCulling
- # texture is baked from selected mesh(es), need to insure this mesh is only one selected
- bpy.ops.object.select_all(action='DESELECT')
- mesh.select = True
- # store setting to restore
- engine = exporter.scene.render.engine
- # mode_set's only work when there is an active object
- exporter.scene.objects.active = mesh
- # UV unwrap operates on mesh in only edit mode, procedurals can also give error of 'no images to be found' when not done
- # select all verticies of mesh, since smart_project works only with selected verticies
- bpy.ops.object.mode_set(mode='EDIT')
- bpy.ops.mesh.select_all(action='SELECT')
- # you need UV on a mesh in order to bake image. This is not reqd for procedural textures, so may not exist
- # need to look if it might already be created, if so use the first one
- uv = mesh.data.uv_textures[0] if len(mesh.data.uv_textures) > 0 else None
- if uv == None:
- mesh.data.uv_textures.new('BakingUV')
- uv = mesh.data.uv_textures['BakingUV']
- uv.active = True
- uv.active_render = True
- bpy.ops.uv.smart_project(angle_limit = 66.0, island_margin = 0.0, user_area_weight = 1.0, use_aspect = True)
- uvName = 'BakingUV' # issues with cycles when not done this way
- else:
- uvName = uv.name
- # create a temporary image & link it to the UV/Image Editor so bake_image works
- bpy.data.images.new(name = mesh.name + '_BJS_BAKE', width = recipe.bakeSize, height = recipe.bakeSize, alpha = False, float_buffer = False)
- image = bpy.data.images[mesh.name + '_BJS_BAKE']
- image.file_format = 'JPEG'
- image.mapping = 'UV' # default value
- image_settings = exporter.scene.render.image_settings
- image_settings.file_format = 'JPEG'
- image_settings.quality = recipe.bakeQuality # for lossy compression formats
- # image_settings.compression = 100 # Amount of time to determine best compression: 0 = no compression with fast file output, 100 = maximum lossless compression with slow file output
- # now go thru all the textures that need to be baked
- if recipe.diffuseBaking:
- self.bake('diffuseTexture', 'DIFFUSE_COLOR', 'TEXTURE', image, mesh, uvName, exporter, recipe)
- if recipe.ambientBaking:
- self.bake('ambientTexture', 'AO', 'AO', image, mesh, uvName, exporter, recipe)
- if recipe.opacityBaking: # no eqivalent found for cycles
- self.bake('opacityTexture', None, 'ALPHA', image, mesh, uvName, exporter, recipe)
- if recipe.reflectionBaking:
- self.bake('reflectionTexture', 'REFLECTION', 'MIRROR_COLOR', image, mesh, uvName, exporter, recipe)
- if recipe.emissiveBaking:
- self.bake('emissiveTexture', 'EMIT', 'EMIT', image, mesh, uvName, exporter, recipe)
- if recipe.bumpBaking:
- self.bake('bumpTexture', 'NORMAL', 'NORMALS', image, mesh, uvName, exporter, recipe)
- if recipe.specularBaking:
- self.bake('specularTexture', 'SPECULAR', 'SPEC_COLOR', image, mesh, uvName, exporter, recipe)
- # Toggle vertex selection & mode, if setting changed their value
- bpy.ops.mesh.select_all(action='TOGGLE') # still in edit mode toggle select back to previous
- bpy.ops.object.mode_set(toggle=True) # change back to Object
- bpy.ops.object.select_all(action='TOGGLE') # change scene selection back, not seeming to work
- exporter.scene.render.engine = engine
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def bake(self, bjs_type, cycles_type, internal_type, image, mesh, uvName, exporter, recipe):
- if recipe.cyclesRender:
- if cycles_type is None:
- return
- self.bakeCycles(cycles_type, image, uvName, recipe.nodeTrees)
- else:
- self.bakeInternal(internal_type, image, uvName)
- self.textures.append(Texture(bjs_type, 1.0, image, mesh, exporter))
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def bakeInternal(self, bake_type, image, uvName):
- Main.log('Internal baking texture, type: ' + bake_type + ', mapped using: ' + uvName, 3)
- # need to use the legal name, since this will become the file name, chars like ':' not legal
- legalName = legal_js_identifier(self.name)
- image.filepath = legalName + '_' + bake_type + '.jpg'
- scene = bpy.context.scene
- scene.render.engine = 'BLENDER_RENDER'
- scene.render.bake_type = bake_type
- # assign the image to the UV Editor, which does not have to shown
- bpy.data.screens['UV Editing'].areas[1].spaces[0].image = image
- renderer = scene.render
- renderer.use_bake_selected_to_active = False
- renderer.use_bake_to_vertex_color = False
- renderer.use_bake_clear = True
- renderer.bake_quad_split = 'AUTO'
- renderer.bake_margin = 5
- renderer.use_file_extension = True
- renderer.use_bake_normalize = True
- renderer.use_bake_antialiasing = True
- bpy.ops.object.bake_image()
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def bakeCycles(self, bake_type, image, uvName, nodeTrees):
- Main.log('Cycles baking texture, type: ' + bake_type + ', mapped using: ' + uvName, 3)
- legalName = legal_js_identifier(self.name)
- image.filepath = legalName + '_' + bake_type + '.jpg'
- scene = bpy.context.scene
- scene.render.engine = 'CYCLES'
- # create an unlinked temporary node to bake to for each material
- for tree in nodeTrees:
- bakeNode = tree.nodes.new(type='ShaderNodeTexImage')
- bakeNode.image = image
- bakeNode.select = True
- tree.nodes.active = bakeNode
- bpy.ops.object.bake(type = bake_type, use_clear = True, margin = 5, use_selected_to_active = False)
- for tree in nodeTrees:
- tree.nodes.remove(tree.nodes.active)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- @staticmethod
- def meshBakingClean(mesh):
- for uvMap in mesh.data.uv_textures:
- if uvMap.name == 'BakingUV':
- mesh.data.uv_textures.remove(uvMap)
- break
- # remove an image if it was baked
- for image in bpy.data.images:
- if image.name == mesh.name + '_BJS_BAKE':
- image.user_clear() # cannot remove image unless 0 references
- bpy.data.images.remove(image)
- break
- #===============================================================================
- class AnimationRange:
- # constructor called by the static actionPrep method
- def __init__(self, name, frames, frameOffset):
- # process input args to members
- self.name = name
- self.frames_in = frames
- self.frame_start = AnimationRange.nextStartingFrame(frameOffset)
-
- self.frames_out = []
- for frame in self.frames_in:
- self.frames_out.append(self.frame_start + frame)
-
- highest_idx = len(self.frames_in) - 1
- self.highest_frame_in = self.frames_in [highest_idx]
- self.frame_end = self.frames_out[highest_idx]
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_string(self):
- return self.name + ': ' + ' in[' + format_int(self.frames_in[0]) + ' - ' + format_int(self.highest_frame_in) + '], out[' + format_int(self.frame_start) + ' - ' + format_int(self.frame_end) + ']'
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_string(file_handler, 'name', self.name, True)
- write_int(file_handler, 'from', self.frame_start)
- write_int(file_handler, 'to', self.frame_end)
- file_handler.write('}')
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- @staticmethod
- def actionPrep(object, action, includeAllFrames, frameOffset):
- # assign the action & test if there is any data for that action for this object
- object.animation_data.action = action
- if len(object.animation_data.action.fcurves) == 0:
- return None
- if includeAllFrames:
- frame_start = int(action.frame_range[0])
- frame_end = int(action.frame_range[1])
- frames = range(frame_start, frame_end + 1) # range is not inclusive with 2nd arg
- else:
- # capture built up from fcurves
- frames = dict()
- for fcurve in object.animation_data.action.fcurves:
- for key in fcurve.keyframe_points:
- frame = key.co.x
- frames[frame] = True
- frames = sorted(frames)
- return AnimationRange(action.name, frames, frameOffset)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- @staticmethod
- def nextStartingFrame(frameOffset):
- if frameOffset == 0: return 0
-
- # ensure a gap of at least 5 frames, starting on an even multiple of 10
- frameOffset += 4
- remainder = frameOffset % 10
- return frameOffset + 10 - remainder
-
- #===============================================================================
- class Animation:
- def __init__(self, dataType, loopBehavior, name, propertyInBabylon, attrInBlender = None, mult = 1, xOffset = 0):
- self.dataType = dataType
- self.framePerSecond = bpy.context.scene.render.fps
- self.loopBehavior = loopBehavior
- self.name = name
- self.propertyInBabylon = propertyInBabylon
- # these never get used by Bones, so optional in contructor args
- self.attrInBlender = attrInBlender
- self.mult = mult
- self.xOffset = xOffset
- #keys
- self.frames = []
- self.values = [] # vector3 for ANIMATIONTYPE_VECTOR3 & matrices for ANIMATIONTYPE_MATRIX
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # a separate method outside of constructor, so can be called once for each Blender Action object participates in
- def append_range(self, object, animationRange):
- # action already assigned, always using poses, not every frame, build up again filtering by attrInBlender
- for idx in range(len(animationRange.frames_in)):
- bpy.context.scene.frame_set(animationRange.frames_in[idx])
- self.frames.append(animationRange.frames_out[idx])
- self.values.append(self.get_attr(object))
- return len(animationRange.frames_in) > 0
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # for auto animate
- def get_first_frame(self):
- return self.frames[0] if len(self.frames) > 0 else -1
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # for auto animate
- def get_last_frame(self):
- return self.frames[len(self.frames) - 1] if len(self.frames) > 0 else -1
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def to_scene_file(self, file_handler):
- file_handler.write('{')
- write_int(file_handler, 'dataType', self.dataType, True)
- write_int(file_handler, 'framePerSecond', self.framePerSecond)
- file_handler.write(',"keys":[')
- first = True
- for frame_idx in range(len(self.frames)):
- if first != True:
- file_handler.write(',')
- first = False
- file_handler.write('\n{')
- write_int(file_handler, 'frame', self.frames[frame_idx], True)
- value_idx = self.values[frame_idx]
- if self.dataType == ANIMATIONTYPE_MATRIX:
- write_matrix4(file_handler, 'values', value_idx)
- elif self.dataType == ANIMATIONTYPE_QUATERNION:
- write_quaternion(file_handler, 'values', value_idx)
- else:
- write_vector(file_handler, 'values', value_idx)
- file_handler.write('}')
- file_handler.write(']') # close keys
- # put this at the end to make less crazy looking ]}]]]}}}}}}}]]]],
- # since animation is also at the end of the bone, mesh, camera, or light
- write_int(file_handler, 'loopBehavior', self.loopBehavior)
- write_string(file_handler, 'name', self.name)
- write_string(file_handler, 'property', self.propertyInBabylon)
- file_handler.write('}')
- #===============================================================================
- class VectorAnimation(Animation):
- def __init__(self, object, propertyInBabylon, attrInBlender, mult = 1, xOffset = 0):
- super().__init__(ANIMATIONTYPE_VECTOR3, ANIMATIONLOOPMODE_CYCLE, propertyInBabylon + ' animation', propertyInBabylon, attrInBlender, mult, xOffset)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def get_attr(self, object):
- return scale_vector(getattr(object, self.attrInBlender), self.mult, self.xOffset)
- #===============================================================================
- class QuaternionAnimation(Animation):
- def __init__(self, object, propertyInBabylon, attrInBlender, mult = 1, xOffset = 0):
- super().__init__(ANIMATIONTYPE_QUATERNION, ANIMATIONLOOPMODE_CYCLE, propertyInBabylon + ' animation', propertyInBabylon, attrInBlender, mult, xOffset)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def get_attr(self, object):
- return post_rotate_quaternion(getattr(object, self.attrInBlender), self.xOffset)
- #===============================================================================
- class QuaternionToEulerAnimation(Animation):
- def __init__(self, propertyInBabylon, attrInBlender, mult = 1, xOffset = 0):
- super().__init__(ANIMATIONTYPE_VECTOR3, ANIMATIONLOOPMODE_CYCLE, propertyInBabylon + ' animation', propertyInBabylon, attrInBlender, mult, Offset)
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- def get_attr(self, object):
- quat = getattr(object, self.attrInBlender)
- eul = quat.to_euler("XYZ")
- return scale_vector(eul, self.mult, self.xOffset)
- #===============================================================================
- # module level formatting methods, called from multiple classes
- #===============================================================================
- def legal_js_identifier(input):
- out = ''
- prefix = ''
- for char in input:
- if len(out) == 0:
- if char in '0123456789':
- # cannot take the chance that leading numbers being chopped of cause name conflicts, e.g (01.R & 02.R)
- prefix += char
- continue
- elif char.upper() not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
- continue
- legal = char if char.upper() in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_' else '_'
- out += legal
- if len(prefix) > 0:
- out += '_' + prefix
- return out
- def format_f(num):
- s = MAX_FLOAT_PRECISION % num # rounds to N decimal places while changing to string
- s = s.rstrip('0') # ignore trailing zeroes
- s = s.rstrip('.') # ignore trailing .
- return '0' if s == '-0' else s
-
- def format_matrix4(matrix):
- tempMatrix = matrix.copy()
- tempMatrix.transpose()
- ret = ''
- first = True
- for vect in tempMatrix:
- if (first != True):
- ret +=','
- first = False;
- ret += format_f(vect[0]) + ',' + format_f(vect[1]) + ',' + format_f(vect[2]) + ',' + format_f(vect[3])
- return ret
- def format_array3(array):
- return format_f(array[0]) + ',' + format_f(array[1]) + ',' + format_f(array[2])
- def format_array(array, max_per_line = MAX_VERTEX_ELEMENTS, indent = ''):
- ret = ''
- first = True
- nOnLine = 0
- for element in array:
- if (first != True):
- ret +=','
- first = False;
- ret += format_f(element)
- nOnLine += 1
- if nOnLine >= max_per_line:
- ret += '\n' + indent
- nOnLine = 0
- return ret
- def format_color(color):
- return format_f(color.r) + ',' + format_f(color.g) + ',' + format_f(color.b)
- def format_vector(vector):
- return format_f(vector.x) + ',' + format_f(vector.z) + ',' + format_f(vector.y)
- def format_vector_array(vectorArray, max_per_line = MAX_VERTEX_ELEMENTS, indent = ''):
- ret = ''
- first = True
- nOnLine = 0
- for vector in vectorArray:
- if (first != True):
- ret +=','
- first = False;
- ret += format_vector(vector)
- nOnLine += 3
- if nOnLine >= max_per_line:
- ret += '\n' + indent
- nOnLine = 0
- return ret
- def format_quaternion(quaternion):
- return format_f(quaternion.x) + ',' + format_f(quaternion.z) + ',' + format_f(quaternion.y) + ',' + format_f(-quaternion.w)
- def format_int(int):
- candidate = str(int) # when int string of an int
- if '.' in candidate:
- return format_f(math.floor(int)) # format_f removes un-neccessary precision
- else:
- return candidate
- def format_bool(bool):
- if bool:
- return 'true'
- else:
- return 'false'
- def post_rotate_quaternion(quat, angle):
- post = mathutils.Euler((angle, 0.0, 0.0)).to_matrix()
- mqtn = quat.to_matrix()
- quat = (mqtn*post).to_quaternion()
- return quat
-
- def scale_vector(vector, mult, xOffset = 0):
- ret = vector.copy()
- ret.x *= mult
- ret.x += xOffset
- ret.z *= mult
- ret.y *= mult
- return ret
- def same_matrix4(matA, matB):
- if(matA is None or matB is None): return False
- if (len(matA) != len(matB)): return False
- for i in range(len(matA)):
- if (round(matA[i][0], MAX_FLOAT_PRECISION_INT) != round(matB[i][0], MAX_FLOAT_PRECISION_INT) or
- round(matA[i][1], MAX_FLOAT_PRECISION_INT) != round(matB[i][1], MAX_FLOAT_PRECISION_INT) or
- round(matA[i][2], MAX_FLOAT_PRECISION_INT) != round(matB[i][2], MAX_FLOAT_PRECISION_INT) or
- round(matA[i][3], MAX_FLOAT_PRECISION_INT) != round(matB[i][3], MAX_FLOAT_PRECISION_INT)):
- return False
- return True
- def same_vertex(vertA, vertB):
- if(vertA is None or vertB is None): return False
- return vertA.x == vertB.x and vertA.y == vertB.y and vertA.z == vertB.z
- def same_array(arrayA, arrayB):
- if(arrayA is None or arrayB is None): return False
- if len(arrayA) != len(arrayB): return False
- for i in range(len(arrayA)):
- if arrayA[i] != arrayB[i] : return False
- return True
- #===============================================================================
- # module level methods for writing JSON (.babylon) files
- #===============================================================================
- def write_matrix4(file_handler, name, matrix):
- file_handler.write(',"' + name + '":[' + format_matrix4(matrix) + ']')
- def write_array(file_handler, name, array):
- file_handler.write('\n,"' + name + '":[' + format_array(array) + ']')
- def write_array3(file_handler, name, array):
- file_handler.write(',"' + name + '":[' + format_array3(array) + ']')
- def write_color(file_handler, name, color):
- file_handler.write(',"' + name + '":[' + format_color(color) + ']')
- def write_vector(file_handler, name, vector):
- file_handler.write(',"' + name + '":[' + format_vector(vector) + ']')
- def write_vector_array(file_handler, name, vectorArray):
- file_handler.write('\n,"' + name + '":[' + format_vector_array(vectorArray) + ']')
- def write_quaternion(file_handler, name, quaternion):
- file_handler.write(',"' + name +'":[' + format_quaternion(quaternion) + ']')
- def write_string(file_handler, name, string, noComma = False):
- if noComma == False:
- file_handler.write(',')
- file_handler.write('"' + name + '":"' + string + '"')
- def write_float(file_handler, name, float):
- file_handler.write(',"' + name + '":' + format_f(float))
- def write_int(file_handler, name, int, noComma = False):
- if noComma == False:
- file_handler.write(',')
- file_handler.write('"' + name + '":' + format_int(int))
- def write_bool(file_handler, name, bool, noComma = False):
- if noComma == False:
- file_handler.write(',')
- file_handler.write('"' + name + '":' + format_bool(bool))
- #===============================================================================
- # custom properties definition and display
- #===============================================================================
- bpy.types.Mesh.autoAnimate = bpy.props.BoolProperty(
- name='Auto launch animations',
- description='',
- default = False
- )
- bpy.types.Mesh.useFlatShading = bpy.props.BoolProperty(
- name='Use Flat Shading',
- description='Use face normals. Increases vertices.',
- default = False
- )
- bpy.types.Mesh.checkCollisions = bpy.props.BoolProperty(
- name='Check Collisions',
- description='Indicates mesh should be checked that it does not run into anything.',
- default = False
- )
- bpy.types.Mesh.castShadows = bpy.props.BoolProperty(
- name='Cast Shadows',
- description='',
- default = False
- )
- bpy.types.Mesh.receiveShadows = bpy.props.BoolProperty(
- name='Receive Shadows',
- description='',
- default = False
- )
- bpy.types.Mesh.bakeSize = bpy.props.IntProperty(
- name='Texture Size',
- description='',
- default = 1024
- )
- bpy.types.Mesh.bakeQuality = bpy.props.IntProperty(
- name='Quality 1-100',
- description='The trade-off between Quality - File size(100 highest quality)',
- default = 50, min = 1, max = 100
- )
- bpy.types.Mesh.materialNameSpace = bpy.props.StringProperty(
- name='Name Space',
- description='Prefix to use for materials for sharing across .blends.',
- default = DEFAULT_MATERIAL_NAMESPACE
- )
- bpy.types.Mesh.checkReadyOnlyOnce = bpy.props.BoolProperty(
- name='Check Ready Only Once',
- description='When checked better CPU utilization. Advanced user option.',
- default = False
- )
- bpy.types.Mesh.freezeWorldMatrix = bpy.props.BoolProperty(
- name='Freeze World Matrix',
- description='Indicate the position, rotation, & scale do not change for performance reasons',
- default = False
- )
- bpy.types.Mesh.loadDisabled = bpy.props.BoolProperty(
- name='Load Disabled',
- description='Indicate this mesh & children should not be active until enabled by code.',
- default = False
- )
- bpy.types.Mesh.attachedSound = bpy.props.StringProperty(
- name='Sound',
- description='',
- default = ''
- )
- bpy.types.Mesh.loopSound = bpy.props.BoolProperty(
- name='Loop sound',
- description='',
- default = True
- )
- bpy.types.Mesh.autoPlaySound = bpy.props.BoolProperty(
- name='Auto play sound',
- description='',
- default = True
- )
- bpy.types.Mesh.maxSoundDistance = bpy.props.FloatProperty(
- name='Max sound distance',
- description='',
- default = 100
- )
- bpy.types.Mesh.maxInfluencers = bpy.props.IntProperty(
- name='Max bone Influencers / Vertex',
- description='When fewer than this are observed, the lower value is used.',
- default = 8, min = 1, max = 8
- )
- #===============================================================================
- bpy.types.Camera.autoAnimate = bpy.props.BoolProperty(
- name='Auto launch animations',
- description='',
- default = False
- )
- bpy.types.Camera.CameraType = bpy.props.EnumProperty(
- name='Camera Type',
- description='',
- # ONLY Append, or existing .blends will have their camera changed
- items = (
- (V_JOYSTICKS_CAM , 'Virtual Joysticks' , 'Use Virtual Joysticks Camera'),
- (TOUCH_CAM , 'Touch' , 'Use Touch Camera'),
- (GAMEPAD_CAM , 'Gamepad' , 'Use Gamepad Camera'),
- (FREE_CAM , 'Free' , 'Use Free Camera'),
- (FOLLOW_CAM , 'Follow' , 'Use Follow Camera'),
- (DEV_ORIENT_CAM , 'Device Orientation' , 'Use Device Orientation Camera'),
- (ARC_ROTATE_CAM , 'Arc Rotate' , 'Use Arc Rotate Camera'),
- (VR_DEV_ORIENT_FREE_CAM , 'VR Dev Orientation Free' , 'Use VR Dev Orientation Free Camera'),
- (WEB_VR_FREE_CAM , 'Web VR Free' , 'Use Web VR Free Camera')
- ),
- default = FREE_CAM
- )
- bpy.types.Camera.checkCollisions = bpy.props.BoolProperty(
- name='Check Collisions',
- description='',
- default = False
- )
- bpy.types.Camera.applyGravity = bpy.props.BoolProperty(
- name='Apply Gravity',
- description='',
- default = False
- )
- bpy.types.Camera.ellipsoid = bpy.props.FloatVectorProperty(
- name='Ellipsoid',
- description='',
- default = mathutils.Vector((0.2, 0.9, 0.2))
- )
- bpy.types.Camera.Camera3DRig = bpy.props.EnumProperty(
- name='Rig',
- description='',
- items = (
- (RIG_MODE_NONE , 'None' , 'No 3D effects'),
- (RIG_MODE_STEREOSCOPIC_ANAGLYPH , 'Anaaglph' , 'Stereoscopic Anagylph'),
- (RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL , 'side-by-side Parallel' , 'Stereoscopic side-by-side parallel'),
- (RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED, 'side-by-side crosseyed', 'Stereoscopic side-by-side crosseyed'),
- (RIG_MODE_STEREOSCOPIC_OVERUNDER , 'over-under' , 'Stereoscopic over-under'),
- (RIG_MODE_VR , 'VR distortion' , 'Use Web VR Free Camera')
- ),
- default = RIG_MODE_NONE
- )
- bpy.types.Camera.interaxialDistance = bpy.props.FloatProperty(
- name='Interaxial Distance',
- description='Distance between cameras. Used by all but VR 3D rigs.',
- default = 0.0637
- )
- #===============================================================================
- bpy.types.Lamp.autoAnimate = bpy.props.BoolProperty(
- name='Auto launch animations',
- description='',
- default = False
- )
- bpy.types.Lamp.shadowMap = bpy.props.EnumProperty(
- name='Shadow Map',
- description='',
- items = ((NO_SHADOWS , 'None' , 'No Shadow Maps'),
- (STD_SHADOWS , 'Standard' , 'Use Standard Shadow Maps'),
- (POISSON_SHADOWS , 'Poisson' , 'Use Poisson Sampling'),
- (ESM_SHADOWS , 'ESM' , 'Use Exponential Shadow Maps'),
- (BLUR_ESM_SHADOWS , 'Blur ESM' , 'Use Blur Exponential Shadow Maps')
- ),
- default = NO_SHADOWS
- )
- bpy.types.Lamp.shadowMapSize = bpy.props.IntProperty(
- name='Shadow Map Size',
- description='',
- default = 512
- )
- bpy.types.Lamp.shadowBias = bpy.props.FloatProperty(
- name='Shadow Bias',
- description='',
- default = 0.00005
- )
- bpy.types.Lamp.shadowBlurScale = bpy.props.IntProperty(
- name='Blur Scale',
- description='',
- default = 2
- )
- bpy.types.Lamp.shadowBlurBoxOffset = bpy.props.IntProperty(
- name='Blur Box Offset',
- description='',
- default = 0
- )
- class ObjectPanel(bpy.types.Panel):
- bl_label = 'Babylon.js ' + format_version()
- bl_space_type = 'PROPERTIES'
- bl_region_type = 'WINDOW'
- bl_context = 'data'
- def draw(self, context):
- ob = context.object
- if not ob or not ob.data:
- return
- layout = self.layout
- isMesh = isinstance(ob.data, bpy.types.Mesh)
- isCamera = isinstance(ob.data, bpy.types.Camera)
- isLight = isinstance(ob.data, bpy.types.Lamp)
- if isMesh:
- row = layout.row()
- row.prop(ob.data, 'useFlatShading')
- row.prop(ob.data, 'checkCollisions')
- row = layout.row()
- row.prop(ob.data, 'castShadows')
- row.prop(ob.data, 'receiveShadows')
- row = layout.row()
- row.prop(ob.data, 'freezeWorldMatrix')
- row.prop(ob.data, 'loadDisabled')
- layout.prop(ob.data, 'autoAnimate')
- layout.prop(ob.data, 'maxInfluencers')
- box = layout.box()
- box.label('Materials')
- box.prop(ob.data, 'materialNameSpace')
- box.prop(ob.data, 'checkReadyOnlyOnce')
- box = layout.box()
- box.label(text='Procedural Texture / Cycles Baking')
- box.prop(ob.data, 'bakeSize')
- box.prop(ob.data, 'bakeQuality')
- box = layout.box()
- box.prop(ob.data, 'attachedSound')
- box.prop(ob.data, 'autoPlaySound')
- box.prop(ob.data, 'loopSound')
- box.prop(ob.data, 'maxSoundDistance')
- elif isCamera:
- layout.prop(ob.data, 'CameraType')
- layout.prop(ob.data, 'checkCollisions')
- layout.prop(ob.data, 'applyGravity')
- layout.prop(ob.data, 'ellipsoid')
- box = layout.box()
- box.label(text="3D Camera Rigs")
- box.prop(ob.data, 'Camera3DRig')
- box.prop(ob.data, 'interaxialDistance')
- layout.prop(ob.data, 'autoAnimate')
- elif isLight:
- layout.prop(ob.data, 'shadowMap')
- layout.prop(ob.data, 'shadowMapSize')
- layout.prop(ob.data, 'shadowBias')
- box = layout.box()
- box.label(text="Blur ESM Shadows")
- box.prop(ob.data, 'shadowBlurScale')
- box.prop(ob.data, 'shadowBlurBoxOffset')
- layout.prop(ob.data, 'autoAnimate')
|