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

Add ruff as lintr and code formatter #507

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Ruff
on: [ push, pull_request ]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
14 changes: 9 additions & 5 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
# zips up the template file
def zip_template():
# Define the directory and zip file paths
dir_path = 'molecularnodes/assets/template/Molecular Nodes'
zip_file_path = 'molecularnodes/assets/template/Molecular Nodes.zip'
dir_path = "molecularnodes/assets/template/Molecular Nodes"
zip_file_path = "molecularnodes/assets/template/Molecular Nodes.zip"

# Create a ZipFile object in write mode
with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
with zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED) as zipf:
# Walk the directory tree and add files to the zip file
for root, dirs, files in os.walk(dir_path):
for file in files:
# Get the path of the file
file_path = os.path.join(root, file)
# Add the file to the zip file
zipf.write(file_path, arcname=os.path.relpath(
file_path, start='molecularnodes/assets/template/'))
zipf.write(
file_path,
arcname=os.path.relpath(
file_path, start="molecularnodes/assets/template/"
),
)


if __name__ == "__main__":
Expand Down
Binary file modified molecularnodes/assets/template/Molecular Nodes.zip
Binary file not shown.
35 changes: 17 additions & 18 deletions molecularnodes/blender/bones.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def clear_armature(object):
object.modifiers.remove(mod)


def add_bones(object, name='armature'):
def add_bones(object, name="armature"):
# creates bones and assigns correct weights

clear_armature(object)
Expand All @@ -20,27 +20,25 @@ def add_bones(object, name='armature'):

armature = create_bones(bone_positions, chain_ids)
for i in range(bone_weights.shape[1]):
group = object.vertex_groups.new(name=f'mn_armature_{i}')
group = object.vertex_groups.new(name=f"mn_armature_{i}")
vertex_indices = np.where(bone_weights[:, i] == 1)[0]
group.add(vertex_indices.tolist(), 1, 'ADD')
group.add(vertex_indices.tolist(), 1, "ADD")

object.select_set(True)
armature.select_set(True)
bpy.context.view_layer.objects.active = armature
bpy.ops.object.parent_set(type='ARMATURE')
bpy.ops.object.parent_set(type="ARMATURE")

bpy.context.view_layer.objects.active = object
bpy.ops.object.modifier_move_to_index(
'EXEC_DEFAULT', modifier="Armature", index=0)
bpy.ops.object.modifier_move_to_index("EXEC_DEFAULT", modifier="Armature", index=0)

return armature


def get_bone_positions(object):

positions, atom_name, chain_id, res_id, sec_struct = [
obj.get_attribute(object, att)
for att in ['position', 'atom_name', 'chain_id', 'res_id', 'sec_struct']
for att in ["position", "atom_name", "chain_id", "res_id", "sec_struct"]
]

is_alpha_carbon = atom_name == 2
Expand All @@ -59,17 +57,16 @@ def get_bone_positions(object):


def get_bone_weights(object):
print('hello world')

print("hello world")

def create_bones(positions, chain_ids, name='armature'):

bpy.ops.object.add(type='ARMATURE', enter_editmode=True)
def create_bones(positions, chain_ids, name="armature"):
bpy.ops.object.add(type="ARMATURE", enter_editmode=True)
object = bpy.context.active_object
object.name = name
coll.armature().objects.link(object)
armature = object.data
armature.name = f'{name}_frame'
armature.name = f"{name}_frame"
arm_name = armature.name
bones = []
# add bones
Expand All @@ -96,7 +93,7 @@ def create_bones(positions, chain_ids, name='armature'):
armature.edit_bones.active = armature.edit_bones[bone_a]
for bone in [bone_a, bone_b]:
armature.edit_bones[bone].select = True
bpy.ops.armature.parent_set(type='CONNECTED')
bpy.ops.armature.parent_set(type="CONNECTED")
for bone in [bone_a, bone_b]:
armature.edit_bones[bone].select = False
bpy.ops.object.editmode_toggle()
Expand All @@ -105,12 +102,14 @@ def create_bones(positions, chain_ids, name='armature'):


class MN_MT_Add_Armature(bpy.types.Operator):
bl_idname = 'mn.add_armature'
bl_label = 'Add Armature'
bl_description = 'Automatically add armature for each amino acid of the structure '
bl_idname = "mn.add_armature"
bl_label = "Add Armature"
bl_description = (
"Automatically add armature for each amino acid of the structure "
)

def execute(self, context):
object = context.active_object
add_bones(bpy.data.objects[object.name], name=object.name)

return {'FINISHED'}
return {"FINISHED"}
17 changes: 9 additions & 8 deletions molecularnodes/blender/coll.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
def mn():
"""Return the MolecularNodes Collection

The collection called 'MolecularNodes' inside the Blender scene is returned. If the
The collection called 'MolecularNodes' inside the Blender scene is returned. If the
collection does not exist first, it is created.
"""
coll = bpy.data.collections.get('MolecularNodes')
coll = bpy.data.collections.get("MolecularNodes")
if not coll:
coll = bpy.data.collections.new('MolecularNodes')
coll = bpy.data.collections.new("MolecularNodes")
bpy.context.scene.collection.children.link(coll)
return coll


def armature(name='MN_armature'):
def armature(name="MN_armature"):
coll = bpy.data.collections.get(name)
if not coll:
coll = bpy.data.collections.new(name)
Expand All @@ -23,8 +23,7 @@ def armature(name='MN_armature'):


def data(suffix=""):
"""A collection for storing MN related data objects.
"""
"""A collection for storing MN related data objects."""
name = f"MN_data{suffix}"

collection = bpy.data.collections.get(name)
Expand All @@ -33,7 +32,9 @@ def data(suffix=""):
mn().children.link(collection)

# disable the view of the data collection
bpy.context.view_layer.layer_collection.children['MolecularNodes'].children[name].exclude = True
bpy.context.view_layer.layer_collection.children["MolecularNodes"].children[
name
].exclude = True
return collection


Expand All @@ -42,7 +43,7 @@ def frames(name="", parent=None, suffix="_frames"):

Args:
name (str, optional): Name of the collection for the frames. Defaults to "".
parent (_type_, optional): A blender collection which will become the parent
parent (_type_, optional): A blender collection which will become the parent
collection. Defaults to the MolecularNodes collection if None.
"""
coll_frames = bpy.data.collections.new(name + suffix)
Expand Down
13 changes: 3 additions & 10 deletions molecularnodes/blender/node.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from abc import ABCMeta
from typing import Optional, Any
import warnings
import time
import numpy as np
import bpy


class Node(metaclass=ABCMeta):
def __init__(self, node: bpy.types.Node, chain=[]):

self.node = node
self.group = node.id_data
self.chain = chain
Expand All @@ -20,18 +16,15 @@ def location(self):
def new(self, name):
"Add a new node to the node group."
try:
return self.group.nodes.new(f'GeometryNode{name}')
return self.group.nodes.new(f"GeometryNode{name}")
except RuntimeError:
return self.group.nodes.new(f'ShaderNode{name}')
return self.group.nodes.new(f"ShaderNode{name}")

def link(self, name, linkto=0, linkfrom=0):
"Create a new node along in the chain and create a link to it. Return the new node."
new_node = self.new(name)
new_node.location = self.location + np.array((200, 0))

self.group.links.new(
self.node.outputs[linkfrom],
new_node.inputs[linkto]
)
self.group.links.new(self.node.outputs[linkfrom], new_node.inputs[linkto])

return Node(new_node, chain=self.chain + [self])
Loading
Loading