Memory leak when exporting to gltf (using blender as a python module) #116319

Open
opened 2023-12-19 02:22:31 +01:00 by Julie-Fabre · 10 comments

System Information
Operating system: MacOS Ventura
Operating system: Linux-6.6.8-200.fc39.x86_64-x86_64-with-glibc2.38 64 Bits, X11 UI

Blender Version
bpy 4.0.0
bpy version: 4.1.0 Alpha, branch: main, commit date: 2024-01-21 18:49, hash: 3df7939eec96

Short description of error
Memory leak when exporting to gltf (using blender as a python module)

Exact steps for others to reproduce the error

import bpy

bpy.ops.wm.read_factory_settings()

bpy.ops.export_scene.gltf(filepath = sys.argv[1]) # Error: Not freed memory blocks: 27936, total unfreed memory 9.345375 MB
#bpy.ops.export_scene.fbx(filepath = sys.argv[1]) # no leaks reported
#bpy.ops.wm.alembic_export(filepath = sys.argv[1]) # no leaks reported

Original Report

I'm using bpy (4.0.0) to convert USDZ to GLB and my script is showing errors like this when exiting:

Error: Not freed memory blocks: 25605, total unfreed memory 8.550426 MB

I'm new to Python and Blender so i'm assuming i need to free memory somewhere, but i'm not sure what's causing it, I've tried resetting Blender to factory settings before the program exits as well as setting iFile to None and deleting it.

I've also tried to call gc.collect() explicitly but that didn't help, I checked gc.garbage but it is empty ([]).

I'm thinking maybe there is an issue with usd_import or maybe export_scene?

The script

import bpy
import os
import sys
import gc
import tempfile
import shutil
import zipfile

# method definitions

#--------------------------------------------------------------------------------------------
#find the USDC file inside the specified directory
def findUSDC(dirpath):

  files = os.listdir(dirpath)
  dirs = []

  for file in files:
    parts = file.split('.')
    filepath = os.path.join(dirpath, file)

    if os.path.isdir(filepath):
      dirs.append(filepath)
    elif len(parts) > 0 and parts[-1] == 'usdc':
      return filepath

  for dir in dirs:
    file = findUSDC(dir)
    if file != '':
      return file
  return ''

#--------------------------------------------------------------------------------------------
#unzip the specified USDZ file to a temp directory, look for the USDC file and return it
def extractUSDC(filepath):

  try:
    if os.path.exists(filepath) == False:
      raise Exception(filepath + " could not be found")

    filePath, fileName = os.path.split(filepath)
    fileName, fileType = fileName.split('.')
  except Exception as e:
    print(e, "unprocessable input")
    return None

  if fileType == 'usdz':
    with zipfile.ZipFile(filepath, 'r') as zf:

      try:
        print("---> temp path:"+tempPath)
        zf.extractall(tempPath)
      except Exception as e:
        print(e)

      zf.close()

      #get the usdc file
      usdcFile = findUSDC(tempPath)

      #recursively process the file
      return extractUSDC(usdcFile)

  elif fileType == 'usdc':
    print("--> USDC Found: "+filepath)
    return filepath
  else:
    print("unprocessable input")
    return None

#--------------------------------------------------------------------------------------------
# import the usdz, export the glb
def convert(usdzPath, glbPath):

  #clear possibly previously loaded data by starting with a blank scene
  bpy.ops.wm.read_factory_settings(use_empty = True)

  print("---> Loading File: ", usdzPath, os.path.getsize(usdzPath), "Bytes")

  #import the USD
  #see: https://docs.blender.org/api/current/bpy.ops.wm.html?highlight=usd#bpy.ops.wm.usd_import
  iFile = extractUSDC(usdzPath)

  if iFile == None:
      print("---> USDC extraction failed")
      return None

  bpy.ops.wm.usd_import(filepath = iFile, import_usd_preview = True)

  #export the GLB
  #see: https://docs.blender.org/api/current/bpy.ops.export_scene.html?highlight=glb#bpy.ops.export_scene.gltf
  bpy.ops.export_scene.gltf(filepath = glbPath)

  #clear all data in memory to avoid memory leaks
  bpy.ops.wm.read_factory_settings(use_empty = True)
  iFile = None
  del iFile

  print("---> Converted file: ", glbPath, os.path.getsize(glbPath), "Bytes")

#------------------------#
# main program execution #
#------------------------#

#make sure we have all the arguments
if len(sys.argv) != 3:
  print("Missing arguments.")
  print("Usage: usdz_to_glb.py file_to_convert.usdz file_converted.glb")
  exit(1)

gc.enable()

#Create a temp directory to extract to
#global so that after all the recusions we can clean it up
tempPath = tempfile.mkdtemp()

convert(sys.argv[1], sys.argv[2])

shutil.rmtree(tempPath)

gc.collect()
**System Information** Operating system: MacOS Ventura Operating system: Linux-6.6.8-200.fc39.x86_64-x86_64-with-glibc2.38 64 Bits, X11 UI **Blender Version** `bpy 4.0.0` bpy version: 4.1.0 Alpha, branch: main, commit date: 2024-01-21 18:49, hash: `3df7939eec96` **Short description of error** Memory leak when exporting to gltf (using blender as a python module) **Exact steps for others to reproduce the error** - install `bpy` as a python module (following https://developer.blender.org/docs/handbook/building_blender/python_module/ or through other sources such as pip) - save the following script and call it with an appropriate output filename - (it just exports the startup file as gltf [or FBX or alembic]) - when exporting to gltf, a memory leak is reported ```python import bpy bpy.ops.wm.read_factory_settings() bpy.ops.export_scene.gltf(filepath = sys.argv[1]) # Error: Not freed memory blocks: 27936, total unfreed memory 9.345375 MB #bpy.ops.export_scene.fbx(filepath = sys.argv[1]) # no leaks reported #bpy.ops.wm.alembic_export(filepath = sys.argv[1]) # no leaks reported ``` **Original Report** I'm using `bpy` (`4.0.0`) to convert USDZ to GLB and my script is showing errors like this when exiting: ``` Error: Not freed memory blocks: 25605, total unfreed memory 8.550426 MB ``` I'm new to Python and Blender so i'm assuming i need to free memory somewhere, but i'm not sure what's causing it, I've tried resetting Blender to factory settings before the program exits as well as setting `iFile` to `None` and deleting it. I've also tried to call `gc.collect()` explicitly but that didn't help, I checked `gc.garbage` but it is empty (`[]`). I'm thinking maybe there is an issue with `usd_import` or maybe `export_scene`? **The script** ```Py import bpy import os import sys import gc import tempfile import shutil import zipfile # method definitions #-------------------------------------------------------------------------------------------- #find the USDC file inside the specified directory def findUSDC(dirpath): files = os.listdir(dirpath) dirs = [] for file in files: parts = file.split('.') filepath = os.path.join(dirpath, file) if os.path.isdir(filepath): dirs.append(filepath) elif len(parts) > 0 and parts[-1] == 'usdc': return filepath for dir in dirs: file = findUSDC(dir) if file != '': return file return '' #-------------------------------------------------------------------------------------------- #unzip the specified USDZ file to a temp directory, look for the USDC file and return it def extractUSDC(filepath): try: if os.path.exists(filepath) == False: raise Exception(filepath + " could not be found") filePath, fileName = os.path.split(filepath) fileName, fileType = fileName.split('.') except Exception as e: print(e, "unprocessable input") return None if fileType == 'usdz': with zipfile.ZipFile(filepath, 'r') as zf: try: print("---> temp path:"+tempPath) zf.extractall(tempPath) except Exception as e: print(e) zf.close() #get the usdc file usdcFile = findUSDC(tempPath) #recursively process the file return extractUSDC(usdcFile) elif fileType == 'usdc': print("--> USDC Found: "+filepath) return filepath else: print("unprocessable input") return None #-------------------------------------------------------------------------------------------- # import the usdz, export the glb def convert(usdzPath, glbPath): #clear possibly previously loaded data by starting with a blank scene bpy.ops.wm.read_factory_settings(use_empty = True) print("---> Loading File: ", usdzPath, os.path.getsize(usdzPath), "Bytes") #import the USD #see: https://docs.blender.org/api/current/bpy.ops.wm.html?highlight=usd#bpy.ops.wm.usd_import iFile = extractUSDC(usdzPath) if iFile == None: print("---> USDC extraction failed") return None bpy.ops.wm.usd_import(filepath = iFile, import_usd_preview = True) #export the GLB #see: https://docs.blender.org/api/current/bpy.ops.export_scene.html?highlight=glb#bpy.ops.export_scene.gltf bpy.ops.export_scene.gltf(filepath = glbPath) #clear all data in memory to avoid memory leaks bpy.ops.wm.read_factory_settings(use_empty = True) iFile = None del iFile print("---> Converted file: ", glbPath, os.path.getsize(glbPath), "Bytes") #------------------------# # main program execution # #------------------------# #make sure we have all the arguments if len(sys.argv) != 3: print("Missing arguments.") print("Usage: usdz_to_glb.py file_to_convert.usdz file_converted.glb") exit(1) gc.enable() #Create a temp directory to extract to #global so that after all the recusions we can clean it up tempPath = tempfile.mkdtemp() convert(sys.argv[1], sys.argv[2]) shutil.rmtree(tempPath) gc.collect() ```
Julie-Fabre added the
Status
Needs Triage
Priority
Normal
Type
Report
labels 2023-12-19 02:22:32 +01:00
Member

Error: Not freed memory blocks: 25605, total unfreed memory 8.550426 MB

Do you get the same leaks reported when you do the steps manually?
If so, at which step does this occur?
the usd_import?
the export_scene.gltf?

Might also help if you attach an example USDZ (since I cannot reproduce with a simple test file here)

> Error: Not freed memory blocks: 25605, total unfreed memory 8.550426 MB Do you get the same leaks reported when you do the steps manually? If so, at which step does this occur? the `usd_import`? the `export_scene.gltf`? Might also help if you attach an example USDZ (since I cannot reproduce with a simple test file here)
Philipp Oeser added
Status
Needs Information from User
and removed
Status
Needs Triage
labels 2023-12-22 11:26:32 +01:00
Author

Error: Not freed memory blocks: 25605, total unfreed memory 8.550426 MB

Do you get the same leaks reported when you do the steps manually?
If so, at which step does this occur?
the usd_import?
the export_scene.gltf?

Might also help if you attach an example USDZ (since I cannot reproduce with a simple test file here)

If i only run usd_import there is no error, it only happens if i call the export_scene.gltf

I've attached both filed that i've tried this with, both give me the memory error.
Note that the converted gecko has the eyes texture floating way above where they are supposed to be and the alberto looks much darker, not sure how this could be fixed but i guess thats a different discussion.

> > Error: Not freed memory blocks: 25605, total unfreed memory 8.550426 MB > > Do you get the same leaks reported when you do the steps manually? > If so, at which step does this occur? > the `usd_import`? > the `export_scene.gltf`? > > Might also help if you attach an example USDZ (since I cannot reproduce with a simple test file here) If i only run `usd_import` there is no error, it only happens if i call the `export_scene.gltf` I've attached both filed that i've tried this with, both give me the memory error. Note that the converted `gecko` has the eyes texture floating way above where they are supposed to be and the `alberto` looks much darker, not sure how this could be fixed but i guess thats a different discussion.
Philipp Oeser added
Status
Needs Triage
and removed
Status
Needs Information from User
labels 2023-12-27 11:50:30 +01:00
Member

I cant spot leaks using the provided files when doing this through blenders UI.

@Julie-Fabre : do you also get leaks reported when you do this from inside blender?

I'm using bpy (4.0.0)

So you only get the memory leaks when using blender as a python module?

I cant spot leaks using the provided files when doing this through blenders UI. @Julie-Fabre : do you also get leaks reported when you do this from inside blender? > I'm using bpy (4.0.0) So you only get the memory leaks when using blender as a python module?
Philipp Oeser added
Status
Needs Information from User
and removed
Status
Needs Triage
labels 2023-12-28 14:06:14 +01:00
Author

I haven't tried through the UI I don't have that installed I don't know how to use it I just need to use it through scripting since it's for a web app, but I guess I can give it a try and figure it out just to rule things out, I'll report back.

I haven't tried through the UI I don't have that installed I don't know how to use it I just need to use it through scripting since it's for a web app, but I guess I can give it a try and figure it out just to rule things out, I'll report back.
Author

I cant spot leaks using the provided files when doing this through blenders UI.

@Julie-Fabre : do you also get leaks reported when you do this from inside blender?

I'm using bpy (4.0.0)

So you only get the memory leaks when using blender as a python module?

So it seems inside blender no errors pop up so i guess this just happens using blender as a python module.
Any ideas how to solve this?

> I cant spot leaks using the provided files when doing this through blenders UI. > > @Julie-Fabre : do you also get leaks reported when you do this from inside blender? > > > I'm using bpy (4.0.0) > > So you only get the memory leaks when using blender as a python module? So it seems inside blender no errors pop up so i guess this just happens using blender as a python module. Any ideas how to solve this?
Philipp Oeser changed title from Memory leak when working with USDZ files to Memory leak when working with USDZ files (when using blender as a python module) 2024-01-18 11:15:09 +01:00
Member

Will have to check again with blenderas a python module (sorry this has been lying around for a bit...)

Will have to check again with blenderas a python module (sorry this has been lying around for a bit...)
Member

Sorry again this took a while to answer.

Can confirm now (using a selfmade build of the bpy module following https://developer.blender.org/docs/handbook/building_blender/python_module/)

Please note this from the documentation though:

The option to build Blender as a Python module is not officially supported, in the sense Blender.org isn't distributing it along with regular releases. Currently, its a build option you can enable, for your own use.

However, I assume this should still be looked at.

For this, we should simplify the report description (will do) since this only affects the gltf/glb export (has nothing to do with USD, can also be reproduce with just the startup file, other exporters such as alembic or FBX dont have the same issue, see below).

So this already triggers it:

import bpy

bpy.ops.wm.read_factory_settings()

bpy.ops.export_scene.gltf(filepath = sys.argv[1]) # Error: Not freed memory blocks: 27936, total unfreed memory 9.345375 MB
#bpy.ops.export_scene.fbx(filepath = sys.argv[1]) # no leaks reported
#bpy.ops.wm.alembic_export(filepath = sys.argv[1]) # no leaks reported

This also might have to be moved to the Addon repository (not sure yet), but will leave in the blender repository since this only happens when using bpy as a python module

@JulienDuroure , @ideasman42 : does this ring a bell?

Sorry again this took a while to answer. Can confirm now (using a selfmade build of the bpy module following https://developer.blender.org/docs/handbook/building_blender/python_module/) Please note this from the documentation though: >The option to build Blender as a Python module is not officially supported, in the sense Blender.org isn't distributing it along with regular releases. Currently, its a build option you can enable, for your own use. However, I assume this should still be looked at. For this, we should simplify the report description (will do) since this only affects the gltf/glb export (has nothing to do with USD, can also be reproduce with just the startup file, other exporters such as alembic or FBX dont have the same issue, see below). So this already triggers it: ```python import bpy bpy.ops.wm.read_factory_settings() bpy.ops.export_scene.gltf(filepath = sys.argv[1]) # Error: Not freed memory blocks: 27936, total unfreed memory 9.345375 MB #bpy.ops.export_scene.fbx(filepath = sys.argv[1]) # no leaks reported #bpy.ops.wm.alembic_export(filepath = sys.argv[1]) # no leaks reported ``` This also might have to be moved to the Addon repository (not sure yet), but will leave in the blender repository since this only happens when using `bpy` as a python module @JulienDuroure , @ideasman42 : does this ring a bell?
Philipp Oeser changed title from Memory leak when working with USDZ files (when using blender as a python module) to Memory leak when exporting to gltf (using blender as a python module) 2024-01-25 11:57:32 +01:00
Member

Hello,
It's the first time someone reported this leak.
I can confirm that I can't see anything when import / export from Blender UI, but not sure if there are so many users using bpy as a py module, that can explain why it was not reported before.
Maybe Campbell will have more information

Hello, It's the first time someone reported this leak. I can confirm that I can't see anything when import / export from Blender UI, but not sure if there are so many users using bpy as a py module, that can explain why it was not reported before. Maybe Campbell will have more information
Author

Hi guys, any update on this? Anything i can do?

Hi guys, any update on this? Anything i can do?
Author
@lichtwerk @JulienDuroure any update?
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#116319
No description provided.