We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

Scripting with conda + blender (bpy) + verge3D

Home Forums Programming Scripting with conda + blender (bpy) + verge3D

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #20457
    jacksund
    Customer

    I hope I’m not asking too much, but I felt that posting here would be better than the general Blender forums! Anyways, I think Verge3D is an awesome opportunity to establish python-backend 3D-model scripting — that is script the creation of *.gltf and *.bin files for online viewing. You could reach out to many developers that stick with python (i.e. all Django users).

    I’m trying to set this up currently, but am struggling to get the Blender+Verge3D code working as an independent python module. The end goal to be able to run the following example script in an anaconda environment:

    # import the blender python module (with Verge3D add-on)
    import bpy
    
    << code that makes 3D-model goes here >>
    << code that edits verge3D settings >>
    
    # save the *.blend file created
    bpy.ops.wm.save_as_mainfile(filepath='C:/Users/jacks/Desktop/example.blend')
    
    # export the file using Verge3D gltf (with compressed files)
    bpy.context.scene.v3d_export.lzma_enabled = True
    bpy.ops.export_scene.v3d_gltf(filepath="C:/Users/jacks/Desktop/example")

    Making bpy as a standalone python module has been done before (see here: https://anaconda.org/kitsune.one/python-blender) (and here: https://pypi.org/project/bpy/), but I can’t figure out how to replicate this for Blender 2.80 with Verge3D functionality.

    Does your team have any insight on how to do this? Or is there alternative method to scripting *.blend/*.gltf/*.bin files that you’d suggest?

    Any help is greatly appreciated!!

    -Jack

    #20463

    Hi,
    awesome idea!
    In theory, you can run any python script in Blender environment without GUI (headless mode) using the following command:
    blender -b -P /path/to/my/script/my_script.py
    Let me how it goes.

    Soft8Soft Tech Chief
    X | FB | LinkedIn

    #20475
    jacksund
    Customer

    I do have scripts successfully running by that method, but that’s actually the reverse of what I’d like to do! With this approach, you always need to run scripts via the ‘blender’ command, which in turn checks for a built-in python. You also can remove the built-in python from blender, and the ‘blender -P’ command will fall back to the system’s default python. That’s a step in the right direction, but it’s not useful for when you want to add bpy functionality to your existing scripts without being forced to use the ‘blender -P’ command instead of ‘py’.

    In my case, I have a conda enviornment with a bunch of high-level modules (django, scipy, plotly, pymatgen, etc.), where I’d add bpy to the server’s functionality — rather than change to running every app/script with the command ‘blender’ instead of ‘py’. If this is possible, then the bpy module becomes much much more accessible to those that have complex environments already built – particularly those that run a django server and those in data science research (like me!).

    The two links that I sent in my original post make the stand-alone python module for blender — I’d like to replicate that with blender 2.80 + Verge3D. So far I’m struggling to do this, but I have since found instructions on building Blender (without Verge3D): https://wiki.blender.org/wiki/Building_Blender/Other/BlenderAsPyModule

    I’m close, but I need help building the module with Verge3D incorporated. Alternatively, we can make a bpy module via the method in the link above and then make a separate Verge3D module that uses bpy as a dependency. I’m not sure which route would be easiest.

    -Jack

    #20492

    Hi,

    thanks for the clarification! I’ve never heard about this BlenderAsPyModule project. So the only thing remains is to import Verge3D addon properly and get access to its features. Have you tried doing something like:

    >>> import sys
    >>> sys.path.append('~/verge3d/addons/verge3d')
    >>> import verge3d

    Replace ‘~/verge3d/addons/verge3d’ to the actual path to Verge3D addon.

    Soft8Soft Tech Chief
    X | FB | LinkedIn

    #20513
    jacksund
    Customer

    I’ll give this a go! If I get a working build, I’ll upload to anaconda for others to use as well. Thank you!

    -Jack

    #20531
    jacksund
    Customer

    I’m unfortunately unable to get this to work. bpy looks like it loads all add-ons when it initializes, so verge3D needs to be in that directory when I use ‘import bpy’. I’ve also tried this, but I’m still getting errors.

    Currently bpy (as installed via anaconda : https://anaconda.org/kitsune.one/python-blender) throws an error with all add-ons. Trying to make my own build of bpy is failing at the build stage as well — though this is just because of my inexperience with cmake.

    I’ve even tried adding all of blender’s python path to my external python, but without any luck there. Errors are thrown when the module tries importing binaries (e.g. import _bpy).

    I’ll likely link this discussion to a post in the general blender forums because I need help with the build bpy module.

    -Jack

    #20596

    Yep, you better ask Blender developers for that.

    Verge3D comes with no binaries and no external dependencies so I believe it should be pretty straightforward to import it, once you have more information on how to deal with Blender addons in your setup.

    Soft8Soft Tech Chief
    X | FB | LinkedIn

    #20753
    jacksund
    Customer

    So I finally got this working! Here are the steps that I followed:

    – Create an anaconda enviornment based on python 3.7
    – Install blender as a python module from https://anaconda.org/kitsune.one/python-blender
    – Download Verge3D (for Blender) from https://www.soft8soft.com/get-verge3d/
    – From the verge3d download, copy the verge3d/addons/verge3d folder into C:/Users/jacks/Anaconda3/envs/blender_env/2.79/scripts/addons
    – Now open up spyder with the blender_env active and run the following:
    import bpy
    import addon_utils
    addon_utils.enable(module_name=”verge3d”)
    NOTE: ‘import bpy’ will throw a bunch of errors/warnings, but this is fine! Error error is associated with blender trying to initiate Blender’s GUI features — which we aren’t using. This will also happen when you enable verge3d. Base functions will still work as intended though.

    #20758

    This is great! Glad you worked it out :good:

    Soft8Soft Tech Chief
    X | FB | LinkedIn

    #25597
    GLiFTeK
    Customer

    Jacksund,
    This is very interesting to me! Do you have any online examples of its functionality?
    I’m not versed in python, but i s seriously like to keep track of these types of developments to share with future collaborators to accomplish certain results.

    Thanks!

    #25598
    jacksund
    Customer

    Sorry to disappoint, but I don’t have any examples. As far I as I know, I’m the only one trying to script with this method. I have everything working (Verge3D + Blender + Django -> all in python), but I can’t publish it yet as much of the code includes my progress towards a research publication (I’m a chemist). I will say that this full python approach isn’t ideal for non-scientists. In 99% of cases, it’s best to go with javascript, which is probably why I’m the only one doing it.

    You can get Verge3d + Blender working in python using the steps I describe above (#20753) – note that if the bpy module doesn’t work, then you have to build it yourself. The tricky part is throwing Django in the mix, which I don’t describe here.

    Depending on what you’re trying to accomplish, maybe I could give some guidance…? Otherwise, I can’t point you to a github example until I publish my research. Sorry!

    -Jack

    #25606
    GLiFTeK
    Customer

    Well a while back I asked what member Ulock recently asked, which is about having objects dynamically load from a linked library scene or more depending…

    Someone replied to me with the how to and I saved that info, soon to revisit it and attempt it.

    They had it working, but noted that they, like me, “cobble” code for prose re: unfamiliar languages.

    So if I get tripped up somewhere, I’ll know who to come and pester about it heheh :mail:

    I’m also curious about integrating control over dynamic geometric generation based on multiple svg images. 2 at a time, rarely 3. Ie:lithographic views in 2D images to calculate a 3D result.

    I know three.js has svg loaders and basic extrusion tools for such, which I’m learning, but there’s affects of it I’m thinking Blender’s armament of abilities would better solve.
    Probably will have to chat with plug in writers… Like the guys who make boxcutter and the guy behind the Tissue plugin. (Fantastic for the iterative geometry style I’m in need of.)

    Anyway, thanks for responding, and I’ll keep in touch!

    #32331
    kubuz
    Customer

    Keeping myself in the loop for further development on this topic.

    #32345
    derekwang0605
    Customer

    Hi,
    Greatest post!
    I can not find this directory
    C:/Users/jacks(admin name)/Anaconda3/envs/blender_env/2.79/scripts/addons

    I add a directory in C:\ProgramData\Anaconda3\envs\blender_envs
    mod = __import__(module_name)
    ModuleNotFoundError: No module named ‘verge3d’

    #32347
    jacksund
    Customer

    How are you setting up your anaconda environment? My guess is that’s the issue. I named my environment ‘blender_env’ which is why that’s in the folder path above. If you named it something different, then the path won’t be the same. Here’s how you would setup your environment:

    conda create -n blender_env python=3.7
    conda install -n blender_env -c kitsune.one python-blender
    conda activate blender_env

    Also make sure you have the correct environment active when you’re running python — the ‘conda activate’ step above does this if you’re launching python from the terminal.

    Last thing of note is that kitsune’s bpy module isn’t great. A while back, I kept running into issues with it, so I just build my bpy module from source. This is a lot more work but depending on your application it might be worth it.

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.