Skip to content

Commit

Permalink
Refactor attributes code (#641)
Browse files Browse the repository at this point in the history
* initial refactoring

* minor tweaks

* working refactor and tests

* more refactoring

* cleanup

* more refactoring

* BlenderObject is subclassed by Molecule

* cleanup some trajectory

* cleanup

* more refactoring

* tinkering

* cleanup

* refactoring

* fix

* fix

* cleanup collections code

* more refactoring of node code

* some refactoring for centering

* add missed file

* fix adding operator

* refactoring

* cleanup pdbx extra annotations
  • Loading branch information
BradyAJohnston authored Nov 3, 2024
1 parent be51a5a commit 4d792c5
Show file tree
Hide file tree
Showing 45 changed files with 1,591 additions and 1,391 deletions.
3 changes: 2 additions & 1 deletion molecularnodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from bpy.app.handlers import frame_change_post, load_post, save_post

from . import entities, operators, props, session, ui
from .utils import add_current_module_to_path
from .ui import pref
from .ui.node_menu import MN_add_node_menu
from .ui.panel import MN_PT_Scene, pt_object_context, change_style_node_menu
Expand Down Expand Up @@ -48,7 +49,7 @@ def register():
except Exception as e:
print(e)
pass

add_current_module_to_path()
bpy.types.NODE_MT_add.append(MN_add_node_menu)
bpy.types.VIEW3D_MT_object_context_menu.prepend(pt_object_context)
bpy.types.NODE_MT_context_menu.prepend(change_style_node_menu)
Expand Down
20 changes: 1 addition & 19 deletions molecularnodes/blender/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
from pathlib import Path
from typing import Union
import bpy


def path_resolve(path: Union[str, Path]) -> Path:
if isinstance(path, str):
return Path(bpy.path.abspath(path))
elif isinstance(path, Path):
return Path(bpy.path.abspath(str(path)))
else:
raise ValueError(f"Unable to resolve path: {path}")


def active_object(context: bpy.types.Context = None) -> bpy.types.Object:
if context is None:
return bpy.context.active_object

return context.active_object
from .utils import path_resolve
115 changes: 0 additions & 115 deletions molecularnodes/blender/bones.py

This file was deleted.

80 changes: 22 additions & 58 deletions molecularnodes/blender/coll.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,34 @@
import bpy
from bpy.types import Collection
from ..bpyd.collection import create_collection


def mn():
"""Return the MolecularNodes Collection
def mn() -> Collection:
"Return the 'MolecularNodes' collection, creating it first if required"
return create_collection("MolecularNodes")

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')
if not coll:
coll = bpy.data.collections.new('MolecularNodes')
bpy.context.scene.collection.children.link(coll)
return coll

def data() -> Collection:
"Return the MolecularNodes/data collection and disable it"
name = ".MN_data"

def armature(name='MN_armature'):
coll = bpy.data.collections.get(name)
if not coll:
coll = bpy.data.collections.new(name)
mn().children.link(coll)
return coll
try:
return bpy.data.collections[name]
except KeyError:
collection = create_collection(name=name, parent=mn())

bpy.context.view_layer.layer_collection.children["MolecularNodes"].children[
collection.name
].exclude = True
return collection

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

collection = bpy.data.collections.get(name)
if not collection:
collection = bpy.data.collections.new(name)
mn().children.link(collection)
def frames(name: str = "") -> Collection:
"Return a collection for storing the objects that are the frames of a trajectory"
return create_collection(f".data_{name}_frames", parent=data())

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


def frames(name="", parent=None, suffix="_frames"):
"""Create a Collection for Frames of a Trajectory
Args:
name (str, optional): Name of the collection for the frames. Defaults to "".
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)
if not parent:
mn().children.link(coll_frames)
else:
parent.children.link(coll_frames)

return coll_frames


def cellpack(name="", parent=None, fallback=False):
def cellpack(name: str = "") -> Collection:
"Return a collection for storing the instances for a CellPack Ensemble"
full_name = f"cellpack_{name}"
coll = bpy.data.collections.get(full_name)
if coll and fallback:
return coll

coll = bpy.data.collections.new(full_name)

if parent:
parent.children.link(coll)
else:
data().children.link(coll)

return coll
return create_collection(full_name, parent=data())
30 changes: 12 additions & 18 deletions molecularnodes/blender/material.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import bpy
import os
from bpy.types import Material
from ..bpyd.material import append_from_blend
from ..utils import MN_DATA_FILE

materials = [
MATERIAL_NAMES = [
"MN Default",
"MN Flat Outline",
"MN Squishy",
Expand All @@ -11,23 +11,17 @@
]


def append_material(name: str) -> bpy.types.Material:
mat = bpy.data.materials.get(name)

if not mat:
bpy.ops.wm.append(
directory=os.path.join(MN_DATA_FILE, "Material"),
filename=name,
link=False,
)

return bpy.data.materials[name]
def append(name: str) -> Material:
"Append a material from the MN_DATA_FILE."
return append_from_blend(name, MN_DATA_FILE)


def add_all_materials() -> None:
for mat in materials:
append_material(mat)
"Append all pre-defined materials from the MN_DATA_FILE."
for name in MATERIAL_NAMES:
append(name)


def default() -> bpy.types.Material:
return append_material("MN Default")
def default() -> Material:
"Return the default material."
return append("MN Default")
Loading

0 comments on commit 4d792c5

Please sign in to comment.