def povrayExportMesh2(obj, camera, path, settings, progressCallback=None): """ This function exports data in the form of a mesh2 humanoid object. The POV-Ray file generated is fairly inflexible, but is highly efficient. Parameters ---------- obj: *3D object*. The object to export. This should be the humanoid object with uv-mapping data and Face Groups defined. camera: *Camera object*. The camera to render from. path: *string*. The file system path to the output files that need to be generated. settings: *dictionary*. Settings passed from the GUI. """ progbase = 0 def progress(prog, desc): if progressCallback == None: gui3d.app.progress(prog, desc) else: progressCallback(prog, desc) # Certain blocks of SDL are mostly static and can be copied directly from reference # files into the output files. nextpb = 0.6 - 0.4 * bool(settings['SSS']) progress(progbase, "Parsing data") staticFile = 'data/povray/staticcontent_mesh2only_fsss.inc' if settings[ 'SSS'] else 'data/povray/staticcontent_mesh2only_tl.inc' # Define some additional file locations outputSceneFile = path.replace('.inc', '.pov') baseName = os.path.basename(path) underScores = ''.ljust(len(baseName), '-') outputDirectory = os.path.dirname(path) # Make sure the directory exists if not os.path.isdir(outputDirectory): try: os.makedirs(outputDirectory) except: log.error('Error creating export directory.') return 0 # Open the output file in Write mode try: outputFileDescriptor = open(path, 'w') except: log.error('Error opening file to write data: %s', path) return 0 # Write position and dimension constants. writeConstants(outputFileDescriptor, settings) # Collect and prepare all objects. stuffs = collect.setupObjects( settings['name'], gui3d.app.selectedHuman, helpers=False, hidden=False, eyebrows=False, lashes=True, subdivide=settings['subdivide'], progressCallback=lambda p: progress(progbase + p * (nextpb - progbase), "Analyzing objects")) progbase = nextpb # If SSS is enabled, render the lightmaps. if settings['SSS'] == True: povrayProcessSSS( stuffs, outputDirectory, settings, lambda p: progress( progbase + p * (0.6 - progbase), "Processing Subsurface Scattering")) progbase = 0.6 # Write mesh data for the object. povrayWriteMesh2( outputFileDescriptor, stuffs, lambda p: progress(progbase + p * (0.9 - progbase), "Writing Objects")) progbase = 0.9 nextpb = 0.95 # Copy texture definitions to the output file. progress(progbase, "Writing Materials") writeLights(settings['scene'], outputFileDescriptor) hmatfile = open(staticFile, 'r') matlines = hmatfile.read() hmatfile.close() matlines = matlines.replace('%%spec%%', str(settings['skinoil'] * settings['moist'])) matlines = matlines.replace( '%%edss%%', str(settings['skinoil'] * (1 - settings['moist']))) matlines = matlines.replace('%%rough%%', str(settings['rough'])) if settings['usebump']: _, imgtype = getChannelData(stuffs[0].bump) matlines = matlines.replace( '%%normal%%', 'bump_map {%s "%s_bump.%s" bump_size %s interpolate 2}' % (imgtype, stuffs[0].name, (stuffs[0].bump[1].split("."))[1], str(settings['wrinkles']))) if settings['SSS']: matlines = matlines.replace( '%%bluenormal%%', 'bump_map {%s "%s_bump.%s" bump_size 3*%s interpolate 2}' % (imgtype, stuffs[0].name, (stuffs[0].bump[1].split("."))[1], str(settings['wrinkles']))) matlines = matlines.replace( '%%greennormal%%', 'bump_map {png "%s_sss_greenbump.png" bump_size 3*%s interpolate 2}' % (stuffs[0].name, str(settings['wrinkles']))) matlines = matlines.replace( '%%rednormal%%', 'bump_map {png "%s_sss_redbump.png" bump_size 3*%s interpolate 2}' % (stuffs[0].name, str(settings['wrinkles']))) else: matlines = matlines.replace( '%%normal%%', 'wrinkles %s scale 0.0002' % str(settings['wrinkles'])) if settings['SSS']: matlines = matlines.replace( '%%bluenormal%%', 'wrinkles 3*%s scale 0.0002' % str(settings['wrinkles'])) matlines = matlines.replace( '%%greennormal%%', 'wrinkles 1.5*%s scale 0.0004' % str(settings['wrinkles'])) matlines = matlines.replace( '%%rednormal%%', 'wrinkles 0.75*%s scale 0.0006' % str(settings['wrinkles'])) matlines = matlines.replace('%%name%%', stuffs[0].name) matlines = matlines.replace( '%%ambience%%', '<%f,%f,%f>' % settings['scene'].environment.ambience) outputFileDescriptor.write(matlines) outputFileDescriptor.write('\n') progress(progbase + 0.65 * (nextpb - progbase), "Writing Materials") writeItemsMaterials(outputFileDescriptor, stuffs, settings, outputDirectory) outputFileDescriptor.close() # Write .pov scene file. writeScene(outputSceneFile, stuffs, settings) progbase = nextpb nextpb = 1.0 writeTextures( stuffs, outputDirectory, lambda p: progress(progbase + p * (nextpb - progbase), "Writing Textures")) progress( 1, "Finished. Pov-Ray project exported successfully at %s" % outputDirectory)
def povrayExportMesh2(obj, camera, path, settings, progressCallback = None): """ This function exports data in the form of a mesh2 humanoid object. The POV-Ray file generated is fairly inflexible, but is highly efficient. Parameters ---------- obj: *3D object*. The object to export. This should be the humanoid object with uv-mapping data and Face Groups defined. camera: *Camera object*. The camera to render from. path: *string*. The file system path to the output files that need to be generated. settings: *dictionary*. Settings passed from the GUI. """ progbase = 0 def progress (prog, desc): if progressCallback == None: gui3d.app.progress(prog, desc) else: progressCallback(prog, desc) # Certain blocks of SDL are mostly static and can be copied directly from reference # files into the output files. nextpb = 0.6 - 0.4 * bool(settings['SSS']) progress (progbase,"Parsing data") staticFile = mh.getSysDataPath('povray/staticcontent_mesh2only_fsss.inc') if settings['SSS'] else mh.getSysDataPath('povray/staticcontent_mesh2only_tl.inc') # Define some additional file locations outputSceneFile = path.replace('.inc', '.pov') baseName = os.path.basename(path) underScores = ''.ljust(len(baseName), '-') outputDirectory = os.path.dirname(path) # Make sure the directory exists if not os.path.isdir(outputDirectory): try: os.makedirs(outputDirectory) except: log.error('Error creating export directory.') return 0 # Open the output file in Write mode try: outputFileDescriptor = open(path, 'w') except: log.error('Error opening file to write data: %s', path) return 0 # Write position and dimension constants. writeConstants(outputFileDescriptor, settings) # Collect and prepare all objects. rmeshes,_amt = collect.setupObjects(settings['name'], gui3d.app.selectedHuman, helpers=False, hidden=False, eyebrows=False, lashes=True, subdivide = settings['subdivide'], progressCallback = lambda p: progress(progbase+p*(nextpb-progbase),"Analyzing objects")) progbase = nextpb # If SSS is enabled, render the lightmaps. if settings['SSS'] == True: povrayProcessSSS(rmeshes, outputDirectory, settings, lambda p: progress(progbase+p*(0.6-progbase),"Processing Subsurface Scattering")) progbase = 0.6 # Write mesh data for the object. povrayWriteMesh2(outputFileDescriptor, rmeshes, lambda p: progress(progbase+p*(0.9-progbase),"Writing Objects")) progbase = 0.9 nextpb = 0.95 # Copy texture definitions to the output file. progress(progbase,"Writing Materials") writeLights(settings['scene'], outputFileDescriptor) hmatfile = open(staticFile, 'r') matlines = hmatfile.read() hmatfile.close() matlines = matlines.replace('%%spec%%', str(settings['skinoil']*settings['moist'])) matlines = matlines.replace('%%edss%%', str(settings['skinoil']*(1-settings['moist']))) matlines = matlines.replace('%%rough%%', str(settings['rough'])) if settings['usebump'] and rmeshes[0].material.bumpMapTexture: bumpDef = getImageDef(rmeshes[0].name, rmeshes[0].material.bumpMapTexture, 'bump') matlines = matlines.replace( '%%normal%%','bump_map {%s bump_size %s interpolate 2}' % ( bumpDef, str(settings['wrinkles']))) if settings['SSS']: matlines = matlines.replace( '%%bluenormal%%','bump_map {%s bump_size 3*%s interpolate 2}' % ( bumpDef, str(settings['wrinkles']))) matlines = matlines.replace( '%%greennormal%%','bump_map {png "%s_sss_greenbump.png" bump_size 3*%s interpolate 2}' % ( rmeshes[0].name, str(settings['wrinkles']))) matlines = matlines.replace( '%%rednormal%%','bump_map {png "%s_sss_redbump.png" bump_size 3*%s interpolate 2}' % ( rmeshes[0].name, str(settings['wrinkles']))) else: matlines = matlines.replace( '%%normal%%','wrinkles %s scale 0.0002' % str(settings['wrinkles'])) if settings['SSS']: matlines = matlines.replace( '%%bluenormal%%','wrinkles 3*%s scale 0.0002' % str(settings['wrinkles'])) matlines = matlines.replace( '%%greennormal%%','wrinkles 1.5*%s scale 0.0004' % str(settings['wrinkles'])) matlines = matlines.replace( '%%rednormal%%','wrinkles 0.75*%s scale 0.0006' % str(settings['wrinkles'])) matlines = matlines.replace('%%name%%', rmeshes[0].name) matlines = matlines.replace( '%%ambience%%','<%f,%f,%f>' % settings['scene'].environment.ambience) outputFileDescriptor.write(matlines) outputFileDescriptor.write('\n') progress(progbase+0.65*(nextpb-progbase),"Writing Materials") writeItemsMaterials(outputFileDescriptor, rmeshes, settings, outputDirectory) outputFileDescriptor.close() # Write .pov scene file. writeScene(outputSceneFile, rmeshes, settings) progbase = nextpb nextpb = 1.0 writeTextures(rmeshes, settings, outputDirectory, lambda p: progress(progbase+p*(nextpb-progbase),"Writing Textures")) progress(1,"Finished. Pov-Ray project exported successfully at %s" % outputDirectory)
def povrayExportArray(obj, camera, path, settings): """ This function exports data in the form of arrays of data the can be used to reconstruct a humanoid object using some very simple POV-Ray macros. These macros can build this data into a variety of different POV-Ray objects, including a mesh2 object that represents the human figure much as it was displayed in MakeHuman. These macros can also generate a union of spheres at the vertices and a union of cylinders that follow the edges of the mesh. A parameter on the mesh2 macro can be used to generate a slightly inflated or deflated mesh. The generated output file always starts with a standard header, is followed by a set of array definitions containing the object data and is ended by a standard set of POV-Ray object definitions. Parameters ---------- obj: *3D object*. The object to export. This should be the humanoid object with uv-mapping data and Face Groups defined. camera: *Camera object*. The camera to render from. path: *string*. The file system path to the output files that need to be generated. """ # Certain files and blocks of SDL are mostly static and can be copied directly # from reference files into the generated output directories or files. headerFile = 'data/povray/headercontent.inc' staticFile = 'data/povray/staticcontent.inc' sceneFile = 'data/povray/makehuman.pov' groupingsFile = 'data/povray/makehuman_groupings.inc' pigmentMap = gui3d.app.selectedHuman.mesh.texture # Define some additional file related strings outputSceneFile = path.replace('.inc', '.pov') baseName = os.path.basename(path) nameOnly = string.replace(baseName, '.inc', '') underScores = ''.ljust(len(baseName), '-') outputDirectory = os.path.dirname(path) # Make sure the directory exists if not os.path.isdir(outputDirectory): try: os.makedirs(outputDirectory) except: log.error('Error creating export directory.') return 0 # Open the output file in Write mode try: outputFileDescriptor = open(path, 'w') except: log.error('Error opening file to write data.') return 0 # Write the file name into the top of the comment block that starts the file. outputFileDescriptor.write('// %s\n' % baseName) outputFileDescriptor.write('// %s\n' % underScores) # Copy the header file SDL straight across to the output file try: headerFileDescriptor = open(headerFile, 'r') except: log.error('Error opening file to read standard headers.') return 0 headerLines = headerFileDescriptor.read() outputFileDescriptor.write(headerLines) outputFileDescriptor.write(''' ''') headerFileDescriptor.close() # Declare POV_Ray variables containing the current makehuman camera. povrayCameraData(camera, resolution, outputFileDescriptor) outputFileDescriptor.write('#declare MakeHuman_TranslateX = %s;\n' % -obj.x) outputFileDescriptor.write('#declare MakeHuman_TranslateY = %s;\n' % obj.y) outputFileDescriptor.write('#declare MakeHuman_TranslateZ = %s;\n\n' % obj.z) outputFileDescriptor.write('#declare MakeHuman_RotateX = %s;\n' % obj.rx) outputFileDescriptor.write('#declare MakeHuman_RotateY = %s;\n' % -obj.ry) outputFileDescriptor.write('#declare MakeHuman_RotateZ = %s;\n\n' % obj.rz) # Calculate some useful values and add them to the output as POV-Ray variable # declarations so they can be readily accessed from a POV-Ray scene file. povraySizeData(obj, outputFileDescriptor) # Collect and prepare all objects. stuffs = collect.setupObjects(settings['name'], gui3d.app.selectedHuman, helpers=False, hidden=False, eyebrows=False, lashes=False, subdivide=settings['subdivide']) # Write array data for the object. povrayWriteArray(outputFileDescriptor, stuffs) # Copy macro and texture definitions straight across to the output file. try: staticContentFileDescriptor = open(staticFile, 'r') except: log.error('Error opening file to read static content.') return 0 staticContentLines = staticContentFileDescriptor.read() outputFileDescriptor.write(staticContentLines) outputFileDescriptor.write('\n') staticContentFileDescriptor.close() # The POV-Ray include file is complete outputFileDescriptor.close() log.message("POV-Ray '#include' file generated.") # Copy a sample scene file across to the output directory try: sceneFileDescriptor = open(sceneFile, 'r') except: log.error('Error opening file to read standard scene file.') return 0 try: outputSceneFileDescriptor = open(outputSceneFile, 'w') except: log.error('Error opening file to write standard scene file.') return 0 sceneLines = sceneFileDescriptor.read() sceneLines = string.replace(sceneLines, 'xxFileNamexx', nameOnly) sceneLines = string.replace(sceneLines, 'xxUnderScoresxx', underScores) sceneLines = string.replace(sceneLines, 'xxLowercaseFileNamexx', nameOnly.lower()) outputSceneFileDescriptor.write(sceneLines) # Copy the skin texture file into the output directory collect.copy(pigmentMap, os.path.join(outputDirectory, "texture.png")) # Copy the makehuman_groupings.inc file into the output directory try: shutil.copy(groupingsFile, outputDirectory) except (IOError, os.error), why: log.error("Can't copy %s" % str(why))
def povrayExportArray(obj, camera, path, settings): """ This function exports data in the form of arrays of data the can be used to reconstruct a humanoid object using some very simple POV-Ray macros. These macros can build this data into a variety of different POV-Ray objects, including a mesh2 object that represents the human figure much as it was displayed in MakeHuman. These macros can also generate a union of spheres at the vertices and a union of cylinders that follow the edges of the mesh. A parameter on the mesh2 macro can be used to generate a slightly inflated or deflated mesh. The generated output file always starts with a standard header, is followed by a set of array definitions containing the object data and is ended by a standard set of POV-Ray object definitions. Parameters ---------- obj: *3D object*. The object to export. This should be the humanoid object with uv-mapping data and Face Groups defined. camera: *Camera object*. The camera to render from. path: *string*. The file system path to the output files that need to be generated. """ # Certain files and blocks of SDL are mostly static and can be copied directly # from reference files into the generated output directories or files. headerFile = mh.getSysDataPath('povray/headercontent.inc') staticFile = mh.getSysDataPath('povray/staticcontent.inc') sceneFile = mh.getSysDataPath('povray/makehuman.pov') groupingsFile = mh.getSysDataPath('povray/makehuman_groupings.inc') pigmentMap = gui3d.app.selectedHuman.mesh.texture # Define some additional file related strings outputSceneFile = path.replace('.inc', '.pov') baseName = os.path.basename(path) nameOnly = string.replace(baseName, '.inc', '') underScores = ''.ljust(len(baseName), '-') outputDirectory = os.path.dirname(path) # Make sure the directory exists if not os.path.isdir(outputDirectory): try: os.makedirs(outputDirectory) except: log.error('Error creating export directory.') return 0 # Open the output file in Write mode try: outputFileDescriptor = open(path, 'w') except: log.error('Error opening file to write data.') return 0 # Write the file name into the top of the comment block that starts the file. outputFileDescriptor.write('// %s\n' % baseName) outputFileDescriptor.write('// %s\n' % underScores) # Copy the header file SDL straight across to the output file try: headerFileDescriptor = open(headerFile, 'r') except: log.error('Error opening file to read standard headers.') return 0 headerLines = headerFileDescriptor.read() outputFileDescriptor.write(headerLines) outputFileDescriptor.write(''' ''') headerFileDescriptor.close() # Declare POV_Ray variables containing the current makehuman camera. povrayCameraData(camera, resolution, outputFileDescriptor) outputFileDescriptor.write('#declare MakeHuman_TranslateX = %s;\n' % -obj.x) outputFileDescriptor.write('#declare MakeHuman_TranslateY = %s;\n' % obj.y) outputFileDescriptor.write('#declare MakeHuman_TranslateZ = %s;\n\n' % obj.z) outputFileDescriptor.write('#declare MakeHuman_RotateX = %s;\n' % obj.rx) outputFileDescriptor.write('#declare MakeHuman_RotateY = %s;\n' % -obj.ry) outputFileDescriptor.write('#declare MakeHuman_RotateZ = %s;\n\n' % obj.rz) # Calculate some useful values and add them to the output as POV-Ray variable # declarations so they can be readily accessed from a POV-Ray scene file. povraySizeData(obj, outputFileDescriptor) # Collect and prepare all objects. rmeshes,_amt = collect.setupObjects(settings['name'], gui3d.app.selectedHuman, helpers=False, hidden=False, eyebrows=False, lashes=False, subdivide = settings['subdivide']) # Write array data for the object. povrayWriteArray(outputFileDescriptor, rmeshes) # Copy macro and texture definitions straight across to the output file. try: staticContentFileDescriptor = open(staticFile, 'r') except: log.error('Error opening file to read static content.') return 0 staticContentLines = staticContentFileDescriptor.read() outputFileDescriptor.write(staticContentLines) outputFileDescriptor.write('\n') staticContentFileDescriptor.close() # The POV-Ray include file is complete outputFileDescriptor.close() log.message("POV-Ray '#include' file generated.") # Copy a sample scene file across to the output directory try: sceneFileDescriptor = open(sceneFile, 'r') except: log.error('Error opening file to read standard scene file.') return 0 try: outputSceneFileDescriptor = open(outputSceneFile, 'w') except: log.error('Error opening file to write standard scene file.') return 0 sceneLines = sceneFileDescriptor.read() sceneLines = string.replace(sceneLines, 'xxFileNamexx', nameOnly) sceneLines = string.replace(sceneLines, 'xxUnderScoresxx', underScores) sceneLines = string.replace(sceneLines, 'xxLowercaseFileNamexx', nameOnly.lower()) outputSceneFileDescriptor.write(sceneLines) # Copy the skin texture file into the output directory collect.copy(pigmentMap, os.path.join(outputDirectory, "texture.png")) # Copy the makehuman_groupings.inc file into the output directory try: shutil.copy(groupingsFile, outputDirectory) except (IOError, os.error) as why: log.error("Can't copy %s" % str(why)) # Job done outputSceneFileDescriptor.close() sceneFileDescriptor.close() log.message('Sample POV-Ray scene file generated.')