kolibril13 / ipyMolecularNodes

molecular plots in Jupyter, powererd by Blender Geometry Nodes
MIT License
54 stars 8 forks source link

load function not working in minimal example #3

Open thetechnocrat-dev opened 6 months ago

thetechnocrat-dev commented 6 months ago

I am getting the below issue at the moment when running the minimal example as a python script. I briefly looked at the molecular nodes package to see if I could find if the function name was updated.

    molecule = mn.load.molecule_rcsb("7TYG", starting_style="cartoon", center_molecule=True)
AttributeError: module 'molecularnodes' has no attribute 'load'
kolibril13 commented 6 months ago

Thanks for the report! I can reproduce this error:

image

from a clean python3.10 env with pip install ipykernel molecularnodes bpy

this can be fixed with using a previous version of molecular nodes with pip install molecularnodes==2.10.0 https://pypi.org/project/molecularnodes/#history

@BradyAJohnston : Can you give us a hint on how to use the load function with the latest api? :)

thetechnocrat-dev commented 6 months ago
pip install molecularnodes==2.10.0

Worked for me as well, good catch!

BradyAJohnston commented 6 months ago

The underlying API should not be considered stable at all. We are still trying to figure out what is the best approach for everything. It is discussed here and I welcome feedback or suggestions on what you would like to see.

To replicate importing from the PDB, you can now use as follows:

mol = mn.io.fetch('7YTG')

The result will be a Molecule object, which is intended to be a more helpful interface for working with Blender objects / underlying data.

mol.object # accesses the Blender object
mol.data   # accessses the underlying AtomArray and original data

# get and set named attributes on the Blender object
mol.get_attribute('position')
mol.set_attribute('b_factor', np.repeat(100, len(mol))

I welcome feedback and suggestions on how you think should be implemented / changed / improved

kolibril13 commented 6 months ago

oh, nice! Seems like these the latest mn.io.fetch is not yet released on pypi:

image

Otherwise, I like the naming of the new api!

BradyAJohnston commented 6 months ago

Ah yeah I have not been prioritising releasing to pypi because the API is in such flux, but I'll release the latest version for testing

BradyAJohnston commented 6 months ago

v4.0.12 is now on pypi

kolibril13 commented 6 months ago

Thanks for the update! With the latest version, I still get an error though

image
BradyAJohnston commented 6 months ago

Add in molecularnodes.register() after import and it should fix it.

kolibril13 commented 6 months ago

now I still get this error:

image
BradyAJohnston commented 6 months ago

the structure is not released yet and thus cannot be downloaded, this is not a MN issue CleanShot 2024-03-27 at 17 31 15@2x

kolibril13 commented 6 months ago

Oh, I realize there was a typo, it's 7TYG instead of 7YTG :)

mol = mn.io.fetch('7TYG') works fine now! Now I don't get any errors anymore, however the scene is rendering empty, probably I still have to add the object to the scene somehow.

image
BradyAJohnston commented 6 months ago

The default style is now spheres, if you use another style or change the spheres to work in eevee it will render

kolibril13 commented 6 months ago

Nice! Next question would be then: How does the API look now in order to change to style="ball_and_stick"?

I'm also right now thinking if it makes sense to maintain this repo at all, or if it would be better to archive it and keep the notebooks directly in the main molecular nodes repo. That way, it would probably be easier to keep the api in sync.

BradyAJohnston commented 6 months ago

Currently you can change the style by either stating a different style on import, or changing the style with a function (which isn't particularly nice but is just the current way to do it).

import molecularnodes as mn
mn.register()
# import with a specific style other than the default
mol = mn.io.fetch('7TYG', style='ball_and_stick')
# change the style node which is used for the blender object
mn.blender.nodes.change_style_node(mol.object, 'surface')

Again I have to emphasize that a lot of this will change (especially the changing of styles).

I'm also right now thinking if it makes sense to maintain this repo at all, or if it would be better to archive it and keep the notebooks directly in the main molecular nodes repo. That way, it would probably be easier to keep the api in sync.

If you would like to contribute to the improvement and refinement of the API and also some example documents / notebooks then I would certainly welcome it. I'm not focused on maintaining example code at the moment if the API is still not settled at all.

kolibril13 commented 6 months ago

awesome, that below script is working with mn version 4.0.12.

I think the best path for this repo is then to pin the molecularnodes version to 2.10.0 and install withpip install molecularnodes==2.10.0. And at one point in future when there is a more stable version, rendering molecules in notebooks could maybe be revisited.

image

here the script for reference:

import sys
import os

def clear_scene():
    bpy.ops.object.select_all(action="DESELECT")
    bpy.ops.object.select_by_type(type="MESH")
    bpy.ops.object.delete()

def render_image():
    # Render
    path = "test.png"
    bpy.context.scene.render.resolution_x = 1000
    bpy.context.scene.render.resolution_y = 500
    bpy.context.scene.render.image_settings.file_format = "PNG"
    bpy.context.scene.render.filepath = path
    bpy.ops.render.render(write_still=True)
    bpy.data.images["Render Result"].save_render(filepath=bpy.context.scene.render.filepath)

    display(Image(filename=path))

import bpy
from IPython.display import display, Image

clear_scene()

# Camera
camera = bpy.data.objects["Camera"]
camera.location = (5, -3, 4)
camera.data.dof.use_dof = True
camera.data.dof.focus_distance = 4
camera.data.dof.aperture_fstop = 4

import molecularnodes as mn
mn.register()
# import with a specific style other than the default
mol = mn.io.fetch('7TYG', style='ball_and_stick')
# change the style node which is used for the blender object
mn.blender.nodes.change_style_node(mol.object, 'surface')

render_image()