Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load_material(self, material_string: str):
lines = material_string.splitlines()
material = Material()
material_name = DEFAULT
for line in lines:
parts = line.split(" ")
if parts[0] == "newmtl":
if material_name != DEFAULT:
self.add_material(material_name, material)
material = Material()
material_name = parts[1]
if parts[0] == "Kd":
# Currently we only support diffuse color
material.color = [
float(parts[1]),
float(parts[2]),
float(parts[3]),
]
def load(self, obj_string: str) -> None:
_vertices: VList = []
_indices: List[IList] = []
_indice_materials: List[str] = []
_normals: VList = []
_texcoords: VList = []
lines: List[str] = obj_string.splitlines()
material = DEFAULT
for line in lines:
line = line.replace(" ", " ")
parts = line.split(" ")
command = parts[0].lower()
if parts[0] == "mtllib":
self.load_material_file(f"{self.path}/{parts[1]}")
if command == "v":
x = float(parts[1])
y = float(parts[2])
z = float(parts[3])
_vertices.append([x, y, z])
if command == "vt":
u = float(parts[1])
w = 1.0 - float(parts[2]) if len(parts) > 2 else 0
_texcoords.append([u, w])
if command == "vn":
def load_material(self, material_string: str):
lines = material_string.splitlines()
material = Material()
material_name = DEFAULT
for line in lines:
parts = line.split(" ")
if parts[0] == "newmtl":
if material_name != DEFAULT:
self.add_material(material_name, material)
material = Material()
material_name = parts[1]
if parts[0] == "Kd":
# Currently we only support diffuse color
material.color = [
float(parts[1]),
float(parts[2]),
float(parts[3]),
]
if parts[0] == "map_Kd":
rest = " ".join(parts[1:])
if rest == "/":
material.texture = rest
[[4, 14, 5], [0, 21, 5], [1, 15, 5]],
]
i = 0
for index in _faces:
ind = []
for f in index:
l_vertex = _vertices[f[0]]
l_normal = _normals[f[2]]
l_tex = _texcoords[f[1]]
self._vertices.append(l_vertex)
self._normals.append(l_normal)
self._texcoords.append(l_tex)
ind.append(i)
i += 1
self.materials[DEFAULT]._indices.append(ind)
self._indices = self.materials[DEFAULT]._indices
return None
def append(self, vertices: VList, material: str = DEFAULT) -> None:
diff = len(vertices) # Number of vertices added
last_index = len(self._vertices)
self._vertices += vertices
self._texcoords += [[0, 0]] * diff
self._normals += [[0, 0, 1]] * diff
self._vertex_count = len(self._vertices)
self.material._vertex_count = self._vertex_count
indices = list(map(lambda x: x + last_index, range(diff)))
self.material._indices.extend([indices])
self._indices = self.material._indices
self._needs_update = True
self._vertices.append([x1, y1, z1])
self._vertices.append([x2, y2, z2])
self._vertices.append([x3, y3, z3])
self._vertices.append([x4, y4, z4])
self._texcoords.append([u1, v1])
self._texcoords.append([u2, v2])
self._texcoords.append([u3, v3])
self._texcoords.append([u4, v4])
self._normals.append([normal[0], normal[1], normal[2]])
self._normals.append([normal[0], normal[1], normal[2]])
self._normals.append([normal[0], normal[1], normal[2]])
self._normals.append([normal[0], normal[1], normal[2]])
self._indices.append([indices, indices + 1, indices + 2])
self._indices.append([indices, indices + 2, indices + 3])
self.materials[DEFAULT]._indices.append([indices, indices + 1, indices + 2])
self.materials[DEFAULT]._indices.append([indices, indices + 2, indices + 3])
indices += 4
return True
def add(self, vertices: VList, colors: Optional[VList] = None, material: str = DEFAULT,) -> None:
i = len(self._indices)
for vertex in vertices:
self._vertices.append(vertex)
self._indices.append([i])
self.materials[material]._indices.append([i])
i += 1
if colors is not None:
if len(colors) != len(vertices):
logging.error("len(colors) != len(vertices)")
return
for color in colors:
self._vertex_colors.append(color)
self._needs_update = True
def add_triangle(
self,
vertices: VList,
material: str = DEFAULT,
normals: Optional[VList] = None,
texcoords: Optional[VList] = None,
colors: Optional[VList] = None,
) -> None:
"""Add triangle to Mesh
Args:
vertices: Vertices of the triangle. This is required. Ex:
`[[0, 0, 0], [2, 0, 0], [1, 1, 0]]`
normals: Normals of the triangle. _(When left as None, Payton will
calculate the surface normal based on vertices and assign
it per given vertex.)_
material: Name of the material to use.
texcoords: Texture UV coordinates.
colors: Per vertex colors (optional)
"""
def material(self, mat: Material) -> None:
self.materials[DEFAULT] = mat