Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update blenderproc to accommodate blender's introduction as a python module #840

Open
aviveh21 opened this issue Mar 5, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@aviveh21
Copy link

aviveh21 commented Mar 5, 2023

Hi,

I noticed that blender in version 3.4 has updated to be provided as a python module (bpy)

This means that we can use our own python environment instead of the blender python environment, and use blender as python module as all modules in python.

This is a huge improvement since we can now use our own python modules and python environment ( without using addons) and debug in our specific development environment. ( without using pycharm professional for example)

I used the blenderproc package and tweaked it a bit so I can use my own python environment with the bpy package to render images, instead of running blender as an executable as it is now, and it worked great.

Do you plan to upgrade blenderproc for this change? A lot of your pipeline is around that constraint of blender's python environment, so you can safely remove some setup stages.

@cornerfarmer
Copy link
Member

Hey @aviveh21,

I have also seen this nice new feature, however we cannot really use this at the moment due to multiple reasons:

  • It only supports python 3.10, so also all blenderproc users would be forced to use python 3.10
  • As far as I understand it, it only works in headless mode, so our GUI based debug mode would not work anymore.
@cornerfarmer cornerfarmer added the enhancement New feature or request label Mar 7, 2023
@ipcoder
Copy link

ipcoder commented Mar 7, 2023

Hey @cornerfarmer

I am also interested in the possibility to run blenderproc from a usual python environment, and would like to support @aviveh21 suggestion.

Allow me to address your points of objections:

  1. Python 3.10 is mostly backward compatible with older versions, and as far as I can say with blenderproc.
    So, from user perspective that would just mean to create a new 3.10 environment, which is a small price compared to the benefits of integrating blenderproc into the workflow.
  2. The main value of the blenderproc is a powerful programmable 3D modeling around the blender. Before blender was released as a stand-alone package implementation of the communication with blender required cumbersome methods through the OS calls, which was the only option considering the architectural constrains.

That is reflected in the blenderproc code, which consists of its core (lets call it 3D) part, and the blender-through-OS- communication part. Currently there are quite separated, but not completely. From the initial analysis it seems feasible to make a compete separation of the 3D part, so it can be used in two modes:

  1. as previously, using all the OS-based interface and retaining the current debugging and GUI functionality,
  2. as a direct python package extending bpy package.

Please let me know what you think about that.

@cornerfarmer
Copy link
Member

Okay, I will keep this thread here open. I see that this could be beneficial, although its gonna be quite a bit work and its not 100% clear that everything will work as expected.

@cornerfarmer cornerfarmer reopened this Mar 7, 2023
@yezhiyun
Copy link

Good idea! would you share how to tweak blenderproc so that it would work well in my own python environment?

@aviveh21
Copy link
Author

Good idea! would you share how to tweak blenderproc so that it would work well in my own python environment?

Of course.
You need to make sure you have python 3.10 as your interpreter to download bpy, and to download all python packages that BlenderProc needs. (You can look them up in the DefaultConfig.py file in the BlenderProc repo)

Then, do this steps:

  1. Include the option of using bpy:
    In the __ init __.py file in the blenderproc directory - I've introduced a new environment variable, called it "USING_BPY_PYTHON_MODULE", and when I include it in my python environment - I check for it first in the __ init __.py file - and import all of blenderproc API modules (copy lines 21-38)

  2. Change if statement to elif in line 12 (now 32). The script would now check first for my environment variable, and then for the rest of blenderproc options.

  3. In the Initializer.py script in the utility directory - include one more line of code in line 34 for setting the language.
    if bpy.context.preferences.view.language: and put lines 34-36 under that if statement.
    In bpy module, there are no languages that are loaded into the list above that blenderproc checks, so you need to check first if there are any languages in the list, and if there are - change it to English as blenderproc does. If not - ignore.

Lastly - you need to make sure you included your new env variable in the environment you are working on, and gave it a value.

Of course this is just a tweak. I do not guarantee that the code will be correct in all cases. It currently works well in the cases I tested.
Enjoy!

The code I included in the __ init __.py file:
if "USING_BPY_PYTHON_MODULE" in os.environ: from .api import loader from .api import utility from .api import sampler from .api import math from .python.utility.Initializer import init, clean_up from .api import postprocessing from .api import writer from .api import material from .api import lighting from .api import camera from .api import renderer from .api import world from .api import constructor from .api import types # pylint: disable=redefined-builtin from .api import object from .api import filter # pylint: enable=redefined-builtin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
4 participants