Пример #1
0
def parse_v2(file_path):
    """
    Parse the UV file format, using JSON.

    :param file_path: File path to read.

    :return: List of MarkerData objects.
    """
    mkr_data_list = []
    f = open(file_path)
    data = json.load(f)
    f.close()

    points = data.get('points', [])
    for point_data in points:
        mkr_data = interface.MarkerData()

        # Static point information.
        name = point_data.get('name')
        set_name = point_data.get('set_name')
        id_ = point_data.get('id')
        assert isinstance(name, basestring)
        assert set_name is None or isinstance(set_name, basestring)
        assert id_ is None or isinstance(id_, int)
        mkr_data.set_name(name)
        mkr_data.set_group_name(set_name)
        mkr_data.set_id(id_)

        # Create marker
        frames = []
        per_frame = point_data.get('per_frame', [])
        for frame_data in per_frame:
            frame_num = frame_data.get('frame')
            assert frame_num is not None
            frames.append(frame_num)

            pos = frame_data.get('pos')
            assert pos is not None
            mkr_u, mkr_v = pos

            mkr_weight = frame_data.get('weight')

            # Set Marker Data
            mkr_data.x.set_value(frame_num, mkr_u)
            mkr_data.y.set_value(frame_num, mkr_v)
            mkr_data.weight.set_value(frame_num, mkr_weight)
            mkr_data.enable.set_value(frame_num, True)

        # Fill in occluded point frames
        all_frames = list(range(min(frames), max(frames) + 1))
        for frame in all_frames:
            mkr_enable = int(frame in frames)
            mkr_data.enable.set_value(frame, mkr_enable)
            if mkr_enable is False:
                mkr_data.weight.set_value(frame, 0.0)

        mkr_data_list.append(mkr_data)
    return mkr_data_list
Пример #2
0
def __convert_nodes_to_marker_data_list(cam_tfm, cam_shp,
                                        nodes,
                                        start_frame, end_frame):
    # Create nodes and objects for loop.
    node_pairs = []
    reproj_nodes = []
    mkr_data_list = []
    for node in nodes:
        reproj = __create_reprojection_node(cam_tfm, cam_shp)
        __connect_transform_to_reprojection(node, reproj)
        reproj_nodes.append(reproj)

        mkr_data = loadmkr_interface.MarkerData()
        mkr_data.set_name(node)
        mkr_data_list.append(mkr_data)

        node_pairs.append((node, reproj, mkr_data))

    # Query Screen-space coordinates across time for all nodes
    cur_time = maya.cmds.currentTime(query=True)
    for f in xrange(start_frame, end_frame + 1):
        maya.cmds.currentTime(f, edit=True, update=True)
        for node, reproj, mkr_data in node_pairs:
            node_attr = reproj + '.outNormCoord'
            mkr_u = maya.cmds.getAttr(node_attr + 'X')
            mkr_v = maya.cmds.getAttr(node_attr + 'Y')
            mkr_enable = True
            mkr_weight = 1.0

            mkr_data.weight.set_value(f, mkr_weight)
            mkr_data.enable.set_value(f, mkr_enable)
            mkr_data.x.set_value(f, mkr_u)
            mkr_data.y.set_value(f, mkr_v)

    if len(reproj_nodes) > 0:
        maya.cmds.delete(reproj_nodes)

    maya.cmds.currentTime(cur_time, edit=True, update=True)
    return mkr_data_list
Пример #3
0
def parse_v1(file_path):
    """
    Parse the UV file format or 3DEqualizer .txt format.

    :param file_path:
    :return:
    """
    f = open(file_path, 'r')
    lines = f.readlines()
    f.close()
    if len(lines) == 0:
        raise OSError('No contents in the file: %s' % file_path)
    mkr_data_list = []

    num_points = int(lines[0])
    if num_points < 1:
        raise interface.ParserError('No points exist.')

    idx = 1  # Skip the first line
    for i in xrange(num_points):
        mkr_name = lines[idx]
        mkr_name = mkr_name.strip()

        # Create marker
        mkr_data = interface.MarkerData()
        mkr_data.set_name(mkr_name)

        idx += 1
        num_frames = int(lines[idx])
        if num_frames <= 0:
            msg = 'point has no data: %r'
            LOG.warning(msg, mkr_name)
            continue

        # Frame data parsing
        frames = []
        j = num_frames
        while j > 0:
            idx += 1
            line = lines[idx]
            if len(line) == 0:
                # Have we reached the end of the file?
                break
            j = j - 1
            split = line.split()
            if len(split) != 4:
                # We should not get here
                msg = 'File invalid, there must be 4 numbers in line: %r'
                raise interface.ParserError(msg % line)
            frame = int(split[0])
            mkr_u = float(split[1])
            mkr_v = float(split[2])
            mkr_weight = 1.0
            mkr_weight = float(split[3])

            mkr_data.weight.set_value(frame, mkr_weight)
            mkr_data.x.set_value(frame, mkr_u)
            mkr_data.y.set_value(frame, mkr_v)
            frames.append(frame)

        # Fill in occluded point frames
        all_frames = list(range(min(frames), max(frames) + 1))
        for frame in all_frames:
            mkr_enable = int(frame in frames)
            mkr_data.enable.set_value(frame, mkr_enable)
            if mkr_enable is False:
                mkr_data.weight.set_value(frame, 0.0)

        mkr_data_list.append(mkr_data)
        idx += 1

    return mkr_data_list
Пример #4
0
    def parse(self, file_path, **kwargs):
        """
        Parse the file path as a 3DEqualizer .txt file.

        :param file_path: File path to parse.
        :type file_path: str

        :param kwargs: expected to contain 'image_width' and 'image_height'.

        :return: List of MarkerData.
        """
        # If the image width/height is not given we raise an error immediately.
        image_width = kwargs.get('image_width')
        image_height = kwargs.get('image_height')
        if isinstance(image_width, (int, float)):
            ValueError('image_width must be float or int.')
        if isinstance(image_height, (int, float)):
            ValueError('image_height must be float or int.')
        if image_width is None:
            image_width = 1.0
        if image_height is None:
            image_height = 1.0
        inv_image_width = 1.0 / image_width
        inv_image_height = 1.0 / image_height

        f = open(file_path, 'r')
        lines = f.readlines()
        f.close()
        if len(lines) == 0:
            raise OSError('No contents in the file: %s' % file_path)
        mkr_data_list = []

        line = lines[0]
        line = line.strip()
        num_points = int(line)
        if num_points < 1:
            raise interface.ParserError('No points exist.')

        idx = 1  # Skip the first line
        for i in xrange(num_points):
            line = lines[idx]
            mkr_name = line.strip()

            # Create marker
            mkr_data = interface.MarkerData()
            mkr_data.set_name(mkr_name)

            # Get point color
            idx += 1
            line = lines[idx]
            line = line.strip()
            mkr_color = int(line)
            mkr_data.set_color(mkr_color)

            idx += 1
            line = lines[idx]
            line = line.strip()
            num_frames = int(line)
            if num_frames <= 0:
                idx += 1
                msg = 'point has no data: %r'
                LOG.warning(msg, mkr_name)
                continue

            # Frame data parsing
            frames = []
            j = num_frames
            while j > 0:
                idx += 1
                line = lines[idx]
                line = line.strip()
                if len(line) == 0:
                    # Have we reached the end of the file?
                    break
                j = j - 1
                split = line.split()
                if len(split) != 3:
                    # We should not get here
                    msg = 'File invalid, there must be 3 numbers in line: %r'
                    raise interface.ParserError(msg % line)
                frame = int(split[0])
                mkr_u = float(split[1]) * inv_image_width
                mkr_v = float(split[2]) * inv_image_height
                mkr_weight = 1.0

                mkr_data.weight.set_value(frame, mkr_weight)
                mkr_data.x.set_value(frame, mkr_u)
                mkr_data.y.set_value(frame, mkr_v)
                frames.append(frame)

            # Fill in occluded point frames
            all_frames = list(range(min(frames), max(frames) + 1))
            for frame in all_frames:
                mkr_enable = int(frame in frames)
                mkr_data.enable.set_value(frame, mkr_enable)
                if mkr_enable is False:
                    mkr_data.weight.set_value(frame, 0.0)

            mkr_data_list.append(mkr_data)
            idx += 1

        return mkr_data_list
Пример #5
0
    def parse(self, file_path, **kwargs):
        if not isinstance(file_path, basestring):
            raise TypeError('file_path is not a string: %r' % file_path)
        if not os.path.isfile(file_path):
            raise OSError('File path does not exist: %s' % file_path)

        mkr_data_list = []
        f = open(file_path, 'r')
        text = f.read()
        f.close()

        idx = text.find('imageSequence')
        if idx == -1:
            msg = "Could not get 'imageSequence' index from: %r"
            raise interface.ParserError(msg % text)

        start_idx = text.find('{', idx + 1)
        if start_idx == -1:
            msg = 'Could not get the starting index from: %r'
            raise interface.ParserError(msg % text)

        end_idx = text.find('}', start_idx + 1)
        if end_idx == -1:
            msg = 'Could not get the ending index from: %r'
            raise interface.ParserError(msg % text)

        imgseq = text[start_idx + 1:end_idx]
        imgseq = imgseq.strip()
        splt = imgseq.split()
        x_res = int(splt[0])
        y_res = int(splt[1])

        # Get path
        imgseq_path = re.search(r'.*f\(\s\"(.*)\"\s\).*', imgseq)
        if imgseq_path is None:
            msg = 'Could not get the image sequence path from: %r'
            raise interface.ParserError(msg % imgseq)
        imgseq_path = imgseq_path.groups()

        # Get frame range
        range_regex = re.search(r'.*b\(\s(\d*)\s(\d*)\s(\d*)\s\)', imgseq)
        if range_regex is None:
            msg = 'Could not get the frame range from: %r'
            raise interface.ParserError(msg % imgseq)
        range_grps = range_regex.groups()
        start_frame = int(range_grps[0])
        end_frame = int(range_grps[1])
        by_frame = int(range_grps[2])
        frames = xrange(start_frame, end_frame, by_frame)

        idx = end_idx
        while True:
            idx = text.find('pointTrack', idx + 1)
            if idx == -1:
                break
            start_idx = text.find('{', idx + 1)
            if start_idx == -1:
                break
            end_idx = text.find('}', start_idx + 1)
            if end_idx == -1:
                break

            # Get point track name
            point_track_header = text[idx:start_idx]
            track_regex = re.search(r'pointTrack\s*\"(.*)\".*',
                                    point_track_header)
            if track_regex is None:
                continue
            track_grps = track_regex.groups()
            if len(track_grps) == 0:
                continue
            mkr_name = track_grps[0]

            # create marker
            mkr_data = interface.MarkerData()
            mkr_data.set_name(mkr_name)
            mkr_data.weight.set_value(start_frame, 1.0)
            for frame in frames:
                mkr_data.enable.set_value(frame, 0)

            point_track = text[start_idx + 1:end_idx]
            for line in point_track.splitlines():
                splt = line.split()
                if len(splt) == 0:
                    continue
                frame = int(splt[0])
                # NOTE: In MatchMover, top-left is (0,0), but we want
                # bottom-left to be (0,0).
                x = float(splt[1]) / x_res
                y = ((float(splt[2]) / y_res) * -1) + 1.0
                enable_value = int(frame in frames)

                mkr_data.enable.set_value(frame, enable_value)
                mkr_data.x.set_value(frame, x)
                mkr_data.y.set_value(frame, y)

            mkr_data_list.append(mkr_data)

        return mkr_data_list