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]),
]
if parts[0] == "map_Kd":
rest = " ".join(parts[1:])
if rest == "/":
material.texture = rest
else:
material.texture = f"{self.path}/{rest}"
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]),
import os
from payton.scene import Scene
from payton.scene.geometry import Mesh
from payton.scene.material import DEFAULT, Material
scene = Scene()
mesh = Mesh()
yellow_material = Material(lights=False, color=[1.0, 1.0, 0.0, 1.0])
mesh.add_material("yellow", yellow_material)
# Regular add_triangle will add it with DEFAULT material
mesh.add_triangle([[0, 0, 0], [2, 0, 0], [2, 2, 0]], texcoords=[[0, 0], [1, 0], [1, 1]])
# Explicit material definition
mesh.add_triangle(
[[0, 0, 0], [2, 2, 0], [0, 2, 0]], texcoords=[[0, 0], [1, 1], [0, 1]], material="yellow",
)
texture_file = os.path.join(os.path.dirname(__file__), "cube.png")
mesh.materials[DEFAULT].texture = texture_file
# mesh.material.texture = texture_file # implicit declaration
scene.add_object("mesh", mesh)
scene.run()
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
]
self._vertices: List[float] = []
self._indices: List[int] = []
self._vertex_count: int = 0
self._model_matrix: Optional[np.ndarray] = None
self._material: Material = Material(display=1, lights=False)
self._material.color = self._color
self._lines: List[Line] = [
Line(vertices=[[0.0, 0.0, 0.01], [xres / 2.0, 0.0, 0.01]], color=[1.0, 0.0, 0.0],),
Line(vertices=[[0.0, 0.0, 0.01], [0.0, yres / 2.0, 0.01]], color=[0.0, 1.0, 0.0],),
Line(vertices=[[0.0, 0.0, 0.01], [0.0, 0.0, yres / 2.0]], color=[0.0, 0.0, 1.0],),
]
# Vertex Array Object pointer
self._vao: int = -1
self.visible: bool = True
self.resize(xres, yres)
def __init__(self, name="", visible=True, track_motion=False, **kwargs: Dict[str, Any],) -> None:
self.children: Dict[str, Object] = {}
# store diffeerent materials
self.materials: Dict[str, Material] = {DEFAULT: Material()}
self._vao: int = NO_VERTEX_ARRAY
self._vbos: List[int] = []
self._no_missing_vao = False
self.name = name
self._visible = visible
self.matrix: VList = [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
# Object vertices. Each vertex has 3 decimals (X, Y, Z). Vertices
# are continuous. [X, Y, Z, X, Y, Z, X, Y, Z, X, ... ]
# -- 1 -- -- 2 -- -- 3 -- -- 4 --
def from_dict(cls, d: Dict[str, Any]) -> "Mesh":
res = cls()
res._vertices = d["vertices"]
res._normals = d["normals"]
res._texcoords = d["texcoords"]
res.matrix = d["matrix"]
res.materials = {n: Material.from_dict(d["materials"][n]) for n in d["materials"]}
res.children = {n: cls.from_dict(d["children"][n]) for n in d["children"]}
return res