Example #1
0
def parse(input_filename="default.effect",
          output_filename="default.json",
          asset_url="",
          asset_root=".",
          infiles=None,
          options=None):
    """
    Untility function to convert a Effect Yaml (.effect) into a JSON file.
    Known built-in textures are: default, quadratic, white, nofalloff, black, flat
    """
    try:
        with open(input_filename, 'r') as source:
            try:
                effects = yaml.load(source)
            # pylint: disable=E1101
            except yaml.scanner.ScannerError as e:
                # pylint: enable=E1101
                LOG.error('Failed processing: %s' % input_filename)
                LOG.error('  >> %s' % e)
            else:
                json_asset = JsonAsset()
                for effect_name, effect_parameters in effects.iteritems():
                    effect_type = effect_parameters.pop('type', None)
                    shader = effect_parameters.pop('shader', None)
                    meta = effect_parameters.pop('meta', None)
                    json_asset.attach_effect(effect_name, effect_type,
                                             effect_parameters, shader, meta)

                standard_json_out(json_asset, output_filename, options)
                return json_asset
    except IOError as e:
        LOG.error('Failed processing: %s' % output_filename)
        LOG.error('  >> %s' % e)
        return None
def parse(input_filename="default.effect", output_filename="default.json", asset_url="", asset_root=".", infiles=None,
          options=None):
    """
    Untility function to convert a Effect Yaml (.effect) into a JSON file.
    Known built-in textures are: default, quadratic, white, nofalloff, black, flat
    """
    try:
        with open(input_filename, 'r') as source:
            try:
                effects = yaml.load(source)
            # pylint: disable=E1101
            except yaml.scanner.ScannerError as e:
            # pylint: enable=E1101
                LOG.error('Failed processing: %s' % input_filename)
                LOG.error('  >> %s' % e)
            else:
                json_asset = JsonAsset()
                for effect_name, effect_parameters in effects.iteritems():
                    effect_type = effect_parameters.pop('type', None)
                    shader = effect_parameters.pop('shader', None)
                    meta = effect_parameters.pop('meta', None)
                    json_asset.attach_effect(effect_name, effect_type, effect_parameters, shader, meta)

                standard_json_out(json_asset, output_filename, options)
                return json_asset
    except IOError as e:
        LOG.error('Failed processing: %s' % output_filename)
        LOG.error('  >> %s' % e)
        return None
Example #3
0
def parse(input_filename="default.material",
          output_filename="default.json",
          asset_url="",
          asset_root=".",
          infiles=None,
          options=None):
    """
    Utility function to convert a Material Yaml (.material) into a JSON file.
    Known built-in textures are: default, quadratic, white, nofalloff, black, flat

    Example:

        # Example material
        material:
            effect: lambert
            diffuse: textures/wallstone.jpg
            color: [1.0, 0.5, 0.1]
            meta: &id1
                collision: True
                collisionFilter: ["ALL"]

        material2:
            diffuse: textures/wall.jpg
            meta:
                <<: *id1
                collision: False

        material3:
            diffuse: textures/stone.jpg
            meta: *id1
    """
    try:
        with open(input_filename, 'r') as source:
            try:
                materials = yaml.load(source)
            # pylint: disable=E1101
            except yaml.scanner.ScannerError as e:
                # pylint: enable=E1101
                LOG.error('Failed processing:%s' % input_filename)
                LOG.error('  >> %s' % e)
            else:
                json_asset = JsonAsset()
                for mat_name, material in materials.iteritems():
                    effect = material.pop('effect', None)
                    technique = material.pop('technique', None)
                    meta = material.pop('meta', None)
                    json_asset.attach_material(mat_name, effect, technique,
                                               material, meta)

                standard_json_out(json_asset, output_filename, options)
                return json_asset
    except IOError as e:
        LOG.error('Failed processing: %s' % output_filename)
        LOG.error('  >> %s' % e)
        return None
def parse(input_filename="default.material", output_filename="default.json", asset_url="", asset_root=".",
          infiles=None, options=None):
    """
    Utility function to convert a Material Yaml (.material) into a JSON file.
    Known built-in textures are: default, quadratic, white, nofalloff, black, flat

    Example:

        # Example material
        material:
            effect: lambert
            diffuse: textures/wallstone.jpg
            color: [1.0, 0.5, 0.1]
            meta: &id1
                collision: True
                collisionFilter: ["ALL"]

        material2:
            diffuse: textures/wall.jpg
            meta:
                <<: *id1
                collision: False

        material3:
            diffuse: textures/stone.jpg
            meta: *id1
    """
    try:
        with open(input_filename, 'r') as source:
            try:
                materials = yaml.load(source)
            # pylint: disable=E1101
            except yaml.scanner.ScannerError as e:
            # pylint: enable=E1101
                LOG.error('Failed processing:%s' % input_filename)
                LOG.error('  >> %s' % e)
            else:
                json_asset = JsonAsset()
                for mat_name, material in materials.iteritems():
                    effect = material.pop('effect', None)
                    technique = material.pop('technique', None)
                    meta = material.pop('meta', None)
                    json_asset.attach_material(mat_name, effect, technique, material, meta)

                standard_json_out(json_asset, output_filename, options)
                return json_asset
    except IOError as e:
        LOG.error('Failed processing: %s' % output_filename)
        LOG.error('  >> %s' % e)
        return None
Example #5
0
def standard_include(infiles):
    """Load and merge all the ``infiles``."""
    if infiles:
        definitions = {}
        for infile in infiles:
            if path_exists(infile):
                with open(infile, 'r') as infile_file:
                    infile_json = json_load(infile_file)
                    definitions = merge_dictionaries(infile_json, definitions)
            else:
                LOG.error('Missing file: %s', infile)
        return JsonAsset(definitions=definitions)
    else:
        return JsonAsset()
    return None
Example #6
0
def parse(input_filename="default.fontdat",
          output_filename="default.json",
          texture_prefix="",
          asset_root=".",
          options=None):
    """Untility function to convert an .fnt file into a JSON file."""
    with open(input_filename, 'rb') as source:
        asset = Bmfont2json(texture_prefix)
        try:
            asset.parse(source)
            asset_name = input_filename
            if input_filename.startswith(asset_root):
                asset_name = asset_name[(len(asset_root) + 1):-8]
            json_asset = JsonAsset(
                definitions=asset.get_definitions(asset_name))
            standard_json_out(json_asset, output_filename, options)
            return json_asset
        # pylint: disable=W0703
        except Exception as e:
            LOG.error(str(e))
Example #7
0
def parse(input_filename="default.obj", output_filename="default.json", asset_url="", asset_root=".",
          infiles=None, options=None):
    """Utility function to convert an OBJ file into a JSON file."""
    definitions_asset = standard_include(infiles)
    with open(input_filename, 'rt') as source:
        asset = Obj2json(basename(input_filename))
        asset.parse(source)
        # Remove any and all unused (e.g. default) shapes and surfaces
        purge_empty(asset.shapes, recurseOnce = True)
        # Generate primitives
        asset.unpack_vertices()
        # Remove any degenerate primitives unless they're requested to be kept
        keep_degenerates = True
        for shape in asset.shapes:
            for _, surface in asset.shapes[shape].surfaces.iteritems():
                material = definitions_asset.retrieve_material(surface.material_name)
                if material.meta('keep_degenerates'):
                    keep_degenerates = True
        if not keep_degenerates:
            asset.remove_degenerate_primitives()
        # Remove any unused vertices and calculate a bounding box
        asset.remove_redundant_vertexes()
        asset.generate_bbox()
        # Generate normals/tangents if required
        (need_normals, generate_normals,
         need_tangents, generate_tangents) = asset.extract_nbt_options(definitions_asset)
        if generate_tangents:
            if generate_normals:
                asset.generate_normals()
            asset.generate_smooth_nbts()
            asset.invert_v_texture_map()
        elif generate_normals:
            asset.generate_normals()
            asset.smooth_normals()
        json_asset = JsonAsset()
        for shape_name in asset.shapes.iterkeys():
            json_asset.attach_shape(shape_name)
            node_name = NodeName("node-%s" % shape_name)
            json_asset.attach_node(node_name)
            #TODO: Should the following be divided into separate shapes?
            json_asset.attach_positions(asset.positions, shape_name)
            # Attach texture map, normals and tangents/binormals if required
            if len(asset.uvs[0]) != 0:
                json_asset.attach_uvs(asset.uvs[0], shape_name)
            if shape_name in need_tangents:
                if len(asset.tangents):
                    # Needing tangents implies needing normals and binormals
                    json_asset.attach_nbts(asset.normals, asset.tangents, asset.binormals, shape_name)
                else:
                    LOG.error('tangents requested for shape %s, but no tangents or uvs available!' % shape_name)
            elif shape_name in need_normals:
                json_asset.attach_normals(asset.normals, shape_name)
            for surface_name, surface in asset.shapes[shape_name].surfaces.iteritems():
                material = definitions_asset.retrieve_material(surface.material_name)
                effect = material.get('effect', DEFAULT_EFFECT_NAME)
                effect_name = "effect-%s" % shape_name
                material_name = "material-%s" % surface.material_name
                instance_name = "instance-%s-%s" % (shape_name, surface_name)
                json_asset.attach_effect(effect_name, effect)
                mat_params = material.get('parameters', None)
                json_asset.attach_material(material_name, effect=effect, parameters=mat_params)
                def textures(mat_params):
                    for k, v in mat_params.iteritems():
                        # If a paramater of a material has a string value, it is assumed to be a texture definition
                        if isinstance(v, basestring):
                            # Return the type of the texture (e.g. 'diffuse')
                            yield k
                for t_type in textures(mat_params):
                    json_asset.attach_texture(material_name, t_type, mat_params[t_type])
                first = surface.first
                last = first + surface.count
                json_asset.attach_surface(asset.primitives[first:last], JsonAsset.SurfaceTriangles,
                                          shape_name, name=surface_name)
                json_asset.attach_node_shape_instance(node_name, instance_name, shape_name,
                                                      material_name, surface=surface_name)
        json_asset.attach_bbox(asset.bbox)
        standard_json_out(json_asset, output_filename, options)
        return json_asset
Example #8
0
def parse(input_filename="default.obj", output_filename="default.json", asset_url="", asset_root=".",
          infiles=None, options=None):
    """Utility function to convert an OBJ file into a JSON file."""
    definitions_asset = standard_include(infiles)
    with open(input_filename, 'r') as source:
        asset = Obj2json(basename(input_filename))
        asset.parse(source)
        # Remove any and all unused (e.g. default) shapes and surfaces
        purge_empty(asset.shapes, recurseOnce = True)
        # Generate primitives
        asset.unpack_vertices()
        # Remove any degenerate primitives unless they're requested to be kept
        keep_degenerates = True
        for shape in asset.shapes:
            for _, surface in asset.shapes[shape].surfaces.iteritems():
                material = definitions_asset.retrieve_material(surface.material_name)
                if material.meta('keep_degenerates'):
                    keep_degenerates = True
        if not keep_degenerates:
            asset.remove_degenerate_primitives()
        # Remove any unused vertices and calculate a bounding box
        asset.remove_redundant_vertexes()
        asset.generate_bbox()
        # Generate normals/tangents if required
        (need_normals, generate_normals,
         need_tangents, generate_tangents) = asset.extract_nbt_options(definitions_asset)
        if generate_tangents:
            if generate_normals:
                asset.generate_normals()
            asset.generate_smooth_nbts()
            asset.invert_v_texture_map()
        elif generate_normals:
            asset.generate_normals()
            asset.smooth_normals()
        json_asset = JsonAsset()
        for shape_name in asset.shapes.iterkeys():
            json_asset.attach_shape(shape_name)
            node_name = NodeName("node-%s" % shape_name)
            json_asset.attach_node(node_name)
            #TODO: Should the following be divided into separate shapes?
            json_asset.attach_positions(asset.positions, shape_name)
            # Attach texture map, normals and tangents/binormals if required
            if len(asset.uvs[0]) != 0:
                json_asset.attach_uvs(asset.uvs[0], shape_name)
            if shape_name in need_tangents:
                if len(asset.tangents):
                    # Needing tangents implies needing normals and binormals
                    json_asset.attach_nbts(asset.normals, asset.tangents, asset.binormals, shape_name)
                else:
                    LOG.error('tangents requested for shape %s, but no tangents or uvs available!', shape_name)
            elif shape_name in need_normals:
                json_asset.attach_normals(asset.normals, shape_name)
            for surface_name, surface in asset.shapes[shape_name].surfaces.iteritems():
                material = definitions_asset.retrieve_material(surface.material_name)
                effect = material.get('effect', DEFAULT_EFFECT_NAME)
                effect_name = "effect-%s" % shape_name
                material_name = "material-%s" % surface.material_name
                instance_name = "instance-%s-%s" % (shape_name, surface_name)
                json_asset.attach_effect(effect_name, effect)
                mat_params = material.get('parameters', None)
                json_asset.attach_material(material_name, effect=effect, parameters=mat_params)
                def textures(mat_params):
                    for k, v in mat_params.iteritems():
                        # If a paramater of a material has a string value, it is assumed to be a texture definition
                        if isinstance(v, basestring):
                            # Return the type of the texture (e.g. 'diffuse')
                            yield k
                for t_type in textures(mat_params):
                    json_asset.attach_texture(material_name, t_type, mat_params[t_type])
                first = surface.first
                last = first + surface.count
                json_asset.attach_surface(asset.primitives[first:last], JsonAsset.SurfaceTriangles,
                                          shape_name, name=surface_name)
                json_asset.attach_node_shape_instance(node_name, instance_name, shape_name,
                                                      material_name, surface=surface_name)
        json_asset.attach_bbox(asset.bbox)
        standard_json_out(json_asset, output_filename, options)
        return json_asset