Skip to content

Commit

Permalink
Allow releasing of Textures
Browse files Browse the repository at this point in the history
  • Loading branch information
3b1b committed Oct 15, 2021
1 parent 329d2c6 commit e10a752
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions manimlib/camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,21 +440,28 @@ def refresh_perspective_uniforms(self):
}

def init_textures(self):
self.path_to_texture_id = {}
self.n_textures = 0
self.path_to_texture = {}

def get_texture_id(self, path):
if path not in self.path_to_texture_id:
# A way to increase tid's sequentially
tid = len(self.path_to_texture_id)
if path not in self.path_to_texture:
tid = self.n_textures
self.n_textures += 1
im = Image.open(path).convert("RGBA")
texture = self.ctx.texture(
size=im.size,
components=len(im.getbands()),
data=im.tobytes(),
)
texture.use(location=tid)
self.path_to_texture_id[path] = tid
return self.path_to_texture_id[path]
self.path_to_texture[path] = (tid, texture)
return self.path_to_texture[path][0]

def release_texture(self, path):
tid_and_texture = self.path_to_texture.pop(path, None)
if tid_and_texture:
tid_and_texture[1].release()
return self


# Mostly just defined so old scenes don't break
Expand Down

0 comments on commit e10a752

Please sign in to comment.