Created
December 9, 2022 05:49
-
-
Save brandon-lockaby/e3e5dd241c9ed03d1c6cfe5330c24bbf to your computer and use it in GitHub Desktop.
Trying to infer datablock sizes from pointers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from itertools import chain | |
import math | |
def convert_size(size_bytes): | |
if size_bytes == 0: | |
return "0B" | |
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") | |
i = int(math.floor(math.log(size_bytes, 1024))) | |
p = math.pow(1024, i) | |
s = round(size_bytes / p, 2) | |
return "%s %s" % (s, size_name[i]) | |
def popup(title = "Info", icon = 'INFO', lines=""): | |
myLines=lines | |
def draw(self, context): | |
for n in myLines: | |
self.layout.label(text=n) | |
bpy.context.window_manager.popup_menu(draw, title = title, icon = icon) | |
dbs = [] | |
class DB: | |
def __init__(self, db): | |
self.type = type(db).bl_rna.identifier | |
self.name = db.name | |
self.ptr = db.as_pointer() | |
dbs.append(self) | |
for db in chain(bpy.data.actions, bpy.data.armatures, bpy.data.brushes, bpy.data.cache_files, bpy.data.cameras, bpy.data.collections, bpy.data.curves, bpy.data.fonts, bpy.data.grease_pencils, bpy.data.hair_curves, bpy.data.images, bpy.data.lattices, bpy.data.libraries, bpy.data.lightprobes, bpy.data.lights, bpy.data.linestyles, bpy.data.masks, bpy.data.materials, bpy.data.meshes, bpy.data.metaballs, bpy.data.movieclips, bpy.data.node_groups, bpy.data.objects, bpy.data.paint_curves, bpy.data.palettes, bpy.data.particles, bpy.data.pointclouds, bpy.data.scenes, bpy.data.screens, bpy.data.shape_keys, bpy.data.sounds, bpy.data.speakers, bpy.data.texts, bpy.data.textures, bpy.data.volumes, bpy.data.window_managers, bpy.data.workspaces, bpy.data.worlds): | |
DB(db) | |
dbs.sort(key=lambda x: x.ptr) | |
for i, db in enumerate(dbs): | |
if i > 0: | |
prev = dbs[i-1] | |
prev.size = db.ptr - prev.ptr | |
dbs[-1].size = 0 | |
dbs.sort(key=lambda x: x.size, reverse=True) | |
dblist = [] | |
for db in dbs: | |
dblist.append("{} ({}) = {}".format(db.name, db.type, convert_size(db.size))) | |
info = '\r\n'.join(dblist) | |
popup(title="Datablocks by size in memory", icon='INFO', lines=dblist) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment