Example #1
0
def yaml_file_to_world_objects(file_path):
    tree = None
    with open(file_path, 'r') as f:
        tree = yaml.load(f, Loader=Loader)

    if tree is None:
        return []

    rv = {'camera': None, 'lights': [], 'world': [], 'config': None}

    defines = {}
    extends_map = {}

    for obj in tree:
        if "define" in obj:
            k = obj["define"]
            v = obj.get("value")
            opt = obj.get("extend")
            defines[k] = v
            if opt is not None:
                extends_map[k] = opt

    # replace 'extends' in defines map
    for obj_name in extends_map:
        parent_name = extends_map[
            obj_name]  # name of object which will be extended
        parent_value = defines[parent_name]
        child_value = defines[
            obj_name]  # name of object with 'extends' keyword
        new_parent_value = deepcopy(parent_value)
        if type(new_parent_value) == dict:
            # assume child value is same type
            for k in child_value:
                new_parent_value[k] = child_value[k]
            defines[obj_name] = new_parent_value

    expand_defines_in_tree(tree, defines)

    for obj in tree:
        if "add" in obj:
            if obj["add"] == "camera":
                rv['camera'] = Camera.from_yaml(obj)
            elif obj["add"] == "light":
                rv['lights'].append(Light.from_yaml(obj))
            elif obj['add'] == 'config':
                rv['config'] = GlobalConfig.from_yaml(obj)
            else:
                possible_item = recursive_add(obj, defines)
                if possible_item is not None:
                    rv['world'].append(possible_item)

    g = Group(material=Material(),
              transform=matrix4x4identity(),
              children=rv['world'])
    rv['world'] = [g]
    return rv
Example #2
0
    def _fan_triangulation(self, line):
        triangles = []
        if '/' in line[1]:
            v1 = line[1].split('/')[0]
            n1 = line[1].split('/')[2]
        else:
            v1 = line[1]

        for i in range(2, len(line) - 1):
            if '/' in line[i]:
                v2 = line[i].split('/')[0]
                n2 = line[i].split('/')[2]
            else:
                v2 = line[i]

            if '/' in line[i + 1]:
                v3 = line[i + 1].split('/')[0]
                n3 = line[i + 1].split('/')[2]
            else:
                v3 = line[i + 1]

            try:
                if len(self.normals) > 0:
                    tri = SmoothTriangle(
                        self.default_material, matrix4x4identity(),
                        self.vertices[int(v1)], self.vertices[int(v2)],
                        self.vertices[int(v3)], self.normals[int(n1)],
                        self.normals[int(n2)], self.normals[int(n3)])
                else:
                    tri = Triangle(self.default_material, matrix4x4identity(),
                                   self.vertices[int(v1)],
                                   self.vertices[int(v2)],
                                   self.vertices[int(v3)])
                triangles.append(tri)
            except IndexError as e:
                print(e, v1, v2, v3, n1, n2, n3)
                raise

        return triangles
Example #3
0
 def __init__(self, open_file, default_material=Material()):
     self.open_file = open_file
     self.default_material = default_material
     self.vertices = []
     self.normals = []
     self.vertices.append(None)
     self.normals.append(None)
     self.named_groups = {
         '##default_group':
         Group(self.default_material, matrix4x4identity(), set())
     }
     self.current_group_name = '##default_group'
     self.default_group = self.named_groups['##default_group']
     self._parse_file()
Example #4
0
    def from_yaml(cls, obj, defines) -> 'Group':
        path = obj['file']
        if 'material' in obj:
            mat = Material.from_yaml(obj['material'])
        else:
            mat = Material()

        if 'transform' in obj:
            xform = Transform.from_yaml(obj['transform'])
        else:
            xform = matrix4x4identity()
        g = obj_file_to_group(path, mat)
        g.transform = xform
        return g
Example #5
0
 def _parse_group(self, line):
     if line[1] not in self.named_groups:
         self.named_groups[line[1]] = Group(self.default_material,
                                            matrix4x4identity(), set())
     self.current_group_name = line[1]
Example #6
0
def obj_to_group(parser):
    obj_group = Group(Material(), matrix4x4identity(), set())
    obj_group.add_child(
        [v for k, v in parser.named_groups.items() if len(v.children) > 0])
    return obj_group