def execute(self, context): messages = {} root = sUtils.getRoot(context.selected_objects[0]) model, objectlist = models.buildModelDictionary(root) vUtils.check_dict(model, defs.definitions['model'], messages) vUtils.checkMessages = messages if len(list( messages.keys())) > 0 else { "NoObject": [] } for entry in messages: log("Errors in object " + entry + ":", 'INFO') for error in messages[entry]: log(error, 'INFO') return {'FINISHED'}
def exportModel(root, export_path, entitytypes=None, model=None): # derive model model = models.buildModelDictionary(root) if not model: model = models.buildModelDictionary(root) # export model in selected formats if entitytypes is None: entitytypes = entities.entity_types for entitytype in entitytypes: typename = "export_entity_" + entitytype # check if format exists and should be exported if not getattr(bpy.data.worlds[0], typename, False): continue # format exists and is exported: if ioUtils.getExpSettings().structureExport: model_path = os.path.join(export_path, entitytype) else: model_path = export_path securepath(model_path) try: entities.entity_types[entitytype]['export'](model, model_path) log( "Export model: " + model['name'] + ' as ' + entitytype + " to " + model_path, "DEBUG") except KeyError: log( "No export function available for selected model type: " + entitytype, "ERROR") continue # TODO: Move mesh export to individual formats? This is practically SMURF # export meshes in selected formats i = 1 mt = len([ m for m in meshes.mesh_types if getattr(bpy.data.worlds[0], "export_mesh_" + m) ]) mc = len(model['meshes']) n = mt * mc for meshtype in meshes.mesh_types: mesh_path = ioUtils.getOutputMeshpath(export_path, meshtype) try: typename = "export_mesh_" + meshtype if getattr(bpy.data.worlds[0], typename): securepath(mesh_path) for meshname in model['meshes']: meshes.mesh_types[meshtype]['export']( model['meshes'][meshname], mesh_path) display.setProgress( i / n, 'Exporting ' + meshname + '.' + meshtype + '...') i += 1 except KeyError: log( "No export function available for selected mesh function: " + meshtype, "ERROR") print(sys.exc_info()[0]) display.setProgress(0) # TODO: Move texture export to individual formats? This is practically SMURF # TODO: Also, this does not properly take care of textures embedded in a .blend file # export textures if ioUtils.getExpSettings().exportTextures: for materialname in model['materials']: mat = model['materials'][materialname] for texturetype in [ 'diffuseTexture', 'normalTexture', 'displacementTexture' ]: if texturetype in mat: sourcepath = os.path.join( os.path.expanduser(bpy.path.abspath('//')), mat[texturetype]) if os.path.isfile(sourcepath): texture_path = securepath( os.path.join(export_path, 'textures')) log("Exporting textures to " + texture_path, "INFO") try: shutil.copy( sourcepath, os.path.join( texture_path, os.path.basename(mat[texturetype]))) except shutil.SameFileError: log("{} already in place".format(texturetype), "INFO")
def deriveEntity(entity, outpath, savetosubfolder): """Derives the dictionary for a SMURF entity from the phobos model dictionary. :param entity: The smurf root object. :type entity: bpy.types.Object :param outpath: The path to export the smurf to. :type outpath: str :param savetosubfolder: If True the export path has a subfolder for this smurf entity. :type savetosubfolder: bool :return: dict - An entry for the scenes entitiesList """ smurf = entity # determine outpath for the smurf export # differentiate between full model and baked reference if "entity/isReference" in smurf: bpy.ops.scene.reload_models_and_poses_operator() modelsPosesColl = bpy.context.user_preferences.addons[ "phobos"].preferences.models_poses for robot_model in modelsPosesColl: if (smurf["modelname"] == robot_model.robot_name) and (smurf["entity/pose"] == robot_model.label): entitypose = models.deriveObjectPose(smurf) entry = models.initObjectProperties(smurf, 'entity', ['link', 'joint', 'motor']) entry.pop("isReference") entry['file'] = os.path.join( os.path.relpath(robot_model.path, outpath), smurf["modelname"] + ".smurf") if 'parent' not in entry and 'joint/type' in smurf and smurf[ 'joint/type'] == 'fixed': entry['parent'] = 'world' entry["position"] = entitypose["translation"] entry["rotation"] = entitypose["rotation_quaternion"] ''' with open(os.path.join(os.path.dirname(defs.__file__), "RobotLib.yml"), "r") as f: robots = yaml.load(f.read()) sourcepath = robots[smurf["modelname"]] for filename in os.listdir(sourcepath): fullpath = os.path.join(sourcepath, filename) if os.path.isfile(fullpath): shutil.copy2(fullpath, os.path.join(smurf_outpath, filename)) else: # remove old folders to prevent errors in copytree shutil.rmtree(os.path.join(smurf_outpath, filename), True) shutil.copytree(fullpath, os.path.join(smurf_outpath, filename)) ''' else: smurf_outpath = securepath( os.path.join(outpath, entity["modelname"] ) if savetosubfolder else outpath) log("smurf_outpath: " + outpath, "DEBUG", "exportSMURFsScene") log( "Exporting " + smurf["entity/name"] + " as a smurf entity to " + smurf_outpath, "INFO", "deriveSMURFEntity", "\n\n") subfolder = smurf["modelname"] if savetosubfolder else "" sUtils.selectObjects(sUtils.getChildren(smurf), clear=True) # re-select for mesh export model, objectlist = models.buildModelDictionary(smurf) export( model, objectlist, smurf_outpath) # FIXME: this is the export function from entities! entitypose = models.deriveObjectPose(smurf) entry = models.initObjectProperties(smurf, 'entity', ['link', 'joint', 'motor']) entry['file'] = (os.path.join(subfolder, smurf["modelname"] + ".smurf") if os.path.isfile(smurf_outpath) else os.path.join( subfolder, "smurf", smurf["modelname"] + ".smurf")) if 'parent' not in entry and 'joint/type' in smurf and smurf[ 'joint/type'] == 'fixed': entry['parent'] = 'world' entry["position"] = entitypose["translation"] entry["rotation"] = entitypose["rotation_quaternion"] return entry