Ejemplo n.º 1
0
def deriveEntity(light, outpath):
    """This function handles a light entity in a scene to export it

    Args:
      entity(bpy.types.Object): The lights root object.
      outpath(str): The path to export to. Not used for light entity
      savetosubfolder(bool): If True data will be exported into subfolders. Not used for light entity
      light: 

    Returns:
      : dict - An entry for the scenes entitiesList

    """
    log("Exporting " + light["entity/name"] + " as a light entity", "INFO")
    entitypose = models.deriveObjectPose(light)
    lightobj = sUtils.getImmediateChildren(light)[0]
    color = lightobj.data.color
    entity = {
        "name": light["entity/name"],
        "type": "light",
        "light_type":
        "spotlight" if lightobj.data.type == "SPOT" else "omnilight",
        "anchor": light["anchor"] if "anchor" in light else "none",
        "color": {
            "diffuse": [color.r, color.g, color.b],
            "use_specular": lightobj.data.
            use_specular,  # only specular information currently available
        },
        "position": entitypose["translation"],
        "rotation": entitypose["rotation_quaternion"],
    }
    if entity["light_type"] == "spotlight":
        entity["angle"] = lightobj.data.spot_size
    return entity
Ejemplo n.º 2
0
def deriveEntity(light, outpath):
    """This function handles a light entity in a scene to export it

    Args:
      entity(bpy.types.Object): The lights root object.
      outpath(str): The path to export to. Not used for light entity
      savetosubfolder(bool): If True data will be exported into subfolders. Not used for light entity
      light: 

    Returns:
      : dict - An entry for the scenes entitiesList

    """
    log("Exporting " + light["entity/name"] + " as a light entity", "INFO")
    entitypose = models.deriveObjectPose(light)
    lightobj = sUtils.getImmediateChildren(light)[0]
    color = lightobj.data.color
    entity = {
        "name": light["entity/name"],
        "type": "light",
        "light_type": "spotlight" if lightobj.data.type == "SPOT" else "omnilight",
        "anchor": light["anchor"] if "anchor" in light else "none",
        "color": {
            "diffuse": [color.r, color.g, color.b],
            "use_specular": lightobj.data.use_specular,  # only specular information currently available
        },
        "position": entitypose["translation"],
        "rotation": entitypose["rotation_quaternion"],
    }
    if entity["light_type"] == "spotlight":
        entity["angle"] = lightobj.data.spot_size
    return entity
Ejemplo n.º 3
0
def deriveEntity(root, outpath):
    """Derives the dictionary for a SMURF entity from the phobos model dictionary.
    
    # TODO savetosubfolder is not a parameter

    Args:
      root(bpy.types.Object): The smurf root object.
      outpath(str): The path to export the smurf to.
      savetosubfolder(bool): If True the export path has a subfolder for this smurf entity.

    Returns:
      : dict - An entry for the scenes entitiesList

    """
    entitypose = models.deriveObjectPose(root)
    entity = models.initObjectProperties(root, 'entity',
                                         ['link', 'joint', 'motor'])
    if 'parent' not in entity and 'joint/type' in root and root[
            'joint/type'] == 'fixed':
        entity['parent'] = 'world'
    entity["position"] = entitypose["translation"]
    entity["rotation"] = entitypose["rotation_quaternion"]

    # check model data if entity is a reference
    # FIXME: this part is broken but not used at the moment anyways
    if "isReference" in entity:
        entity.pop("isReference")
        bpy.ops.scene.reload_models_and_poses_operator()
        modelsPosesColl = bUtils.getPhobosPreferences().models_poses
        for robot_model in modelsPosesColl:
            if (root["model/name"]
                    == robot_model.robot_name) and (root["entity/pose"]
                                                    == robot_model.label):
                pass
        entity['file'] = os.path.join(
            os.path.relpath(robot_model.path, outpath),
            root["name"] + ".smurf")
        """
        with open(os.path.join(os.path.dirname(defs.__file__), "RobotLib.yml"), "r") as f:
            robots = yaml.load(f.read())
            sourcepath = robots[smurf["model/name"]]
            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:
        modelpath = os.path.join(outpath, root['model/name'], 'smurf')
        # TODO why the spacing between the paths?
        log("Scene paths: " + outpath + ' ' + modelpath, "DEBUG")
        entity['file'] = os.path.join(
            os.path.relpath(modelpath, os.path.dirname(outpath)),
            root['model/name'] + ".smurf")
    return entity
Ejemplo n.º 4
0
def deriveEntity(root, outpath):
    """Derives the dictionary for a SMURF entity from the phobos model dictionary.
    
    # TODO savetosubfolder is not a parameter

    Args:
      root(bpy.types.Object): The smurf root object.
      outpath(str): The path to export the smurf to.
      savetosubfolder(bool): If True the export path has a subfolder for this smurf entity.

    Returns:
      : dict - An entry for the scenes entitiesList

    """
    entitypose = models.deriveObjectPose(root)
    entity = models.initObjectProperties(root, 'entity', ['link', 'joint', 'motor'])
    if 'parent' not in entity and 'joint/type' in root and root['joint/type'] == 'fixed':
        entity['parent'] = 'world'
    entity["position"] = entitypose["translation"]
    entity["rotation"] = entitypose["rotation_quaternion"]

    # check model data if entity is a reference
    # FIXME: this part is broken but not used at the moment anyways
    if "isReference" in entity:
        entity.pop("isReference")
        bpy.ops.scene.reload_models_and_poses_operator()
        modelsPosesColl = bUtils.getPhobosPreferences().models_poses
        for robot_model in modelsPosesColl:
            if (root["model/name"] == robot_model.robot_name) and (
                root["entity/pose"] == robot_model.label
            ):
                pass
        entity['file'] = os.path.join(
            os.path.relpath(robot_model.path, outpath), root["name"] + ".smurf"
        )
        """
        with open(os.path.join(os.path.dirname(defs.__file__), "RobotLib.yml"), "r") as f:
            robots = yaml.load(f.read())
            sourcepath = robots[smurf["model/name"]]
            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:
        modelpath = os.path.join(outpath, root['model/name'], 'smurf')
        # TODO why the spacing between the paths?
        log("Scene paths: " + outpath + ' ' + modelpath, "DEBUG")
        entity['file'] = os.path.join(
            os.path.relpath(modelpath, os.path.dirname(outpath)), root['model/name'] + ".smurf"
        )
    return entity
Ejemplo n.º 5
0
def deriveEntity(entity, outpath, savetosubfolder):
    """This function handles a heightmap entity in a scene to export it

    :param smurf: The heightmap root object.
    :type smurf: bpy.types.Object
    :param outpath: The path to export to.
    :type outpath: str
    :param savetosubfolder: If True data will be exported into subfolders.
    :type savetosubfolder: bool
    :return: dict - An entry for the scenes entitiesList

    """

    heightmap = entity

    # determine outpath for the heightmap export
    heightmap_outpath = securepath(
        os.path.join(outpath, structure_subfolder
                     ) if savetosubfolder else outpath)

    log("Exporting " + heightmap["entity/name"] + " as a heightmap entity",
        "INFO")
    entitypose = models.deriveObjectPose(heightmap)
    heightmapMesh = sUtils.getImmediateChildren(heightmap)[0]
    if bpy.data.worlds[0].heightmapMesh:
        exMesh = heightmapMesh.to_mesh(bpy.context.scene, True, "PREVIEW")
        exMesh.name = "hm_" + heightmap["entity/name"]
        oldMesh = heightmapMesh.data
        heightmapMesh.data = exMesh
        heightmapMesh.modifiers["displace_heightmap"].show_render = False
        heightmapMesh.modifiers["displace_heightmap"].show_viewport = False
        if bpy.data.worlds[0].useObj:
            iUtils.exportObj(heightmap_outpath, heightmapMesh)
            filename = os.path.join("heightmaps", exMesh.name + ".obj")
        elif bpy.data.worlds[0].useStl:
            iUtils.exportStl(heightmap_outpath, heightmapMesh)
            filename = os.path.join("heightmaps", exMesh.name + ".stl")
        elif bpy.data.worlds[0].useDae:
            iUtils.exportDae(heightmap_outpath, heightmapMesh)
            filename = os.path.join("heightmaps", exMesh.name + ".dae")
        else:
            log("No mesh export type checked! Aborting heightmap export.",
                "ERROR", __name__ + ".handleScene_heightmap")
            return {}
        heightmapMesh.modifiers["displace_heightmap"].show_render = True
        heightmapMesh.modifiers["displace_heightmap"].show_viewport = True
        heightmapMesh.data = oldMesh
        bpy.data.meshes.remove(exMesh)
        entry = {
            "name": heightmap["entity/name"],
            "type": "mesh",
            "file": filename,
            "anchor": heightmap["anchor"] if "anchor" in heightmap else "none",
            "position": entitypose["translation"],
            "rotation": entitypose["rotation_quaternion"]
        }

    else:
        imagepath = os.path.abspath(
            os.path.join(
                os.path.split(bpy.data.filepath)[0], heightmap["image"]))
        shutil.copy2(imagepath, heightmap_outpath)
        entry = {
            "name": heightmap["entity/name"],
            "type": "heightmap",
            "file": os.path.join("heightmaps", os.path.basename(imagepath)),
            "anchor": heightmap["anchor"] if "anchor" in heightmap else "none",
            "width": heightmapMesh.dimensions[1],
            "length": heightmapMesh.dimensions[0],
            "height": heightmapMesh.modifiers["displace_heightmap"].strength,
            "position": entitypose["translation"],
            "rotation": entitypose["rotation_quaternion"]
        }
    return entry
Ejemplo n.º 6
0
def deriveEntity(entity, outpath):
    """This function handles a heightmap entity in a scene to export it

    Args:
      smurf(bpy.types.Object): The heightmap root object.
      outpath(str): The path to export to.
      savetosubfolder(bool): If True data will be exported into subfolders.
      entity: 

    Returns:
      : dict - An entry for the scenes entitiesList

    """

    heightmap = entity

    # determine outpath for the heightmap export
    heightmap_outpath = securepath(os.path.join(outpath, structure_subfolder))

    log("Exporting " + heightmap["entity/name"] + " as a heightmap entity", "INFO")
    entitypose = models.deriveObjectPose(heightmap)
    heightmapMesh = sUtils.getImmediateChildren(heightmap)[0]
    if bpy.data.window_managers[0].heightmapMesh:
        exMesh = heightmapMesh.to_mesh(bpy.context.scene, True, "PREVIEW")
        exMesh.name = "hm_" + heightmap["entity/name"]
        oldMesh = heightmapMesh.data
        heightmapMesh.data = exMesh
        heightmapMesh.modifiers["displace_heightmap"].show_render = False
        heightmapMesh.modifiers["displace_heightmap"].show_viewport = False
        # CHECK are the heightmaps exported to the right directory?
        if bpy.data.window_managers[0].useObj:
            ioUtils.exportObj(heightmap_outpath, heightmapMesh)
            filename = os.path.join("heightmaps", exMesh.name + ".obj")
        elif bpy.data.window_managers[0].useStl:
            ioUtils.exportStl(heightmap_outpath, heightmapMesh)
            filename = os.path.join("heightmaps", exMesh.name + ".stl")
        elif bpy.data.window_managers[0].useDae:
            ioUtils.exportDae(heightmap_outpath, heightmapMesh)
            filename = os.path.join("heightmaps", exMesh.name + ".dae")
        else:
            log("No mesh export type checked! Aborting heightmap export.", "ERROR")
            return {}
        heightmapMesh.modifiers["displace_heightmap"].show_render = True
        heightmapMesh.modifiers["displace_heightmap"].show_viewport = True
        heightmapMesh.data = oldMesh
        bpy.data.meshes.remove(exMesh)
        entry = {
            "name": heightmap["entity/name"],
            "type": "mesh",
            "file": filename,
            "anchor": heightmap["anchor"] if "anchor" in heightmap else "none",
            "position": entitypose["translation"],
            "rotation": entitypose["rotation_quaternion"],
        }

    else:
        imagepath = os.path.abspath(
            os.path.join(os.path.split(bpy.data.filepath)[0], heightmap["image"])
        )
        shutil.copy2(imagepath, heightmap_outpath)
        entry = {
            "name": heightmap["entity/name"],
            "type": "heightmap",
            "file": os.path.join("heightmaps", os.path.basename(imagepath)),
            "anchor": heightmap["anchor"] if "anchor" in heightmap else "none",
            "width": heightmapMesh.dimensions[1],
            "length": heightmapMesh.dimensions[0],
            "height": heightmapMesh.modifiers["displace_heightmap"].strength,
            "position": entitypose["translation"],
            "rotation": entitypose["rotation_quaternion"],
        }
    return entry
Ejemplo n.º 7
0
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