Skip to content

Commit

Permalink
general cleanup and reorganising
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyAJohnston committed Jun 12, 2024
1 parent 11e2625 commit e71e850
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 112 deletions.
Binary file modified molecularnodes/assets/MN_data_file_4.2.blend
Binary file not shown.
50 changes: 0 additions & 50 deletions molecularnodes/blender/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,6 @@
"density_wire": "Style Density Wire",
}

STYLE_ITEMS = (
("presets", "Presets", "A pre-made combination of different styles"),
("spheres", "Spheres", "Space-filling atoms style."),
("surface", "Surface", "Solvent-accsible surface."),
("cartoon", "Cartoon", "Secondary structure cartoons"),
("ribbon", "Ribbon", "Continuous backbone ribbon."),
("sticks", "Sticks", "Sticks for each bond."),
("ball_and_stick", "Ball and Stick", "Spheres for atoms, sticks for bonds"),
)

bpy.types.Scene.MN_import_style = bpy.props.EnumProperty(
name="Style",
description="Default style for importing molecules.",
items=STYLE_ITEMS,
default="spheres",
)


MN_DATA_FILE = os.path.join(ADDON_DIR, "assets", "MN_data_file_4.2.blend")

Expand Down Expand Up @@ -181,7 +164,6 @@ def style_node(group):
prev = previous_node(get_output(group))
is_style_node = "Style" in prev.name
while not is_style_node:
# print(prev.name)
prev = previous_node(prev)
is_style_node = "Style" in prev.name
return prev
Expand Down Expand Up @@ -259,7 +241,6 @@ def material_default():
mat = bpy.data.materials.get(mat_name)

if not mat:
print("appending material")
bpy.ops.wm.append(
directory=os.path.join(MN_DATA_FILE, "Material"),
filename="MN Default",
Expand Down Expand Up @@ -314,34 +295,6 @@ def assign_material(node, material="default"):
material_socket.default_value = material


def add_node(node_name, label: str = "", show_options=False, material="default"):
# intended to be called upon button press in the node tree

prev_context = bpy.context.area.type
bpy.context.area.type = "NODE_EDITOR"
# actually invoke the operator to add a node to the current node tree
# use_transform=True ensures it appears where the user's mouse is and is currently
# being moved so the user can place it where they wish
bpy.ops.node.add_node(
"INVOKE_DEFAULT", type="GeometryNodeGroup", use_transform=True
)
bpy.context.area.type = prev_context
node = bpy.context.active_node
node.node_tree = bpy.data.node_groups[node_name]
node.width = 200.0
node.show_options = show_options

# if label == "":
# node.label = format_node_name(node_name)
# else:
# node.label = label
node.label = node_name
node.name = node_name

# if added node has a 'Material' input, set it to the default MN material
assign_material(node, material=material)


def add_custom(
group,
name,
Expand Down Expand Up @@ -952,7 +905,6 @@ def resid_multiple_selection(node_name, input_resid_string):
selections in specific proteins.
"""

# print(f'recieved input: {input_resid_string}')
# do a cleanning of input string to allow fuzzy input from users
for c in ";/+ .":
if c in input_resid_string:
Expand All @@ -962,8 +914,6 @@ def resid_multiple_selection(node_name, input_resid_string):
if c in input_resid_string:
input_resid_string = input_resid_string.replace(c, "-")

# print(f'fixed input:{input_resid_string}')

# parse input_resid_string into sub selecting string list
sub_list = [item for item in input_resid_string.split(",") if item]

Expand Down
4 changes: 3 additions & 1 deletion molecularnodes/ui/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .ops import CLASSES
from . import ops, panel, node_menu

CLASSES = ops.CLASSES + panel.CLASSES + node_menu.CLASSES
2 changes: 0 additions & 2 deletions molecularnodes/ui/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

def build_menu(layout, items):
for item in items:
# print(item)
if item == "break":
layout.separator()
elif item["label"] == "custom":
for button in item["values"]:
print(f"{button=}")
item["function"](
layout,
label=button["label"],
Expand Down
102 changes: 79 additions & 23 deletions molecularnodes/ui/ops.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
import bpy
from bpy.types import Operator
from bpy.props import BoolProperty, EnumProperty, IntProperty, StringProperty

from ..blender import nodes
from .panel import STYLE_ITEMS


def _add_node(
node_name, context, label: str = "", show_options=False, material="default"
):
# intended to be called upon button press in the node tree, and not for use
# in general scripting

prev_context = context.area.type
context.area.type = "NODE_EDITOR"
# actually invoke the operator to add a node to the current node tree
# use_transform=True ensures it appears where the user's mouse is and is currently
# being moved so the user can place it where they wish
bpy.ops.node.add_node(
"INVOKE_DEFAULT", type="GeometryNodeGroup", use_transform=True
)
context.area.type = prev_context
node = context.active_node
node.node_tree = bpy.data.node_groups[node_name]
node.width = 200.0
node.show_options = show_options

# if label == "":
# node.label = format_node_name(node_name)
# else:
# node.label = label
node.label = node_name
node.name = node_name

# if added node has a 'Material' input, set it to the default MN material
nodes.assign_material(node, material=material)


class MN_OT_Add_Custom_Node_Group(bpy.types.Operator):
class MN_OT_Add_Custom_Node_Group(Operator):
bl_idname = "mn.add_custom_node_group"
bl_label = "Add Custom Node Group"
# bl_description = "Add Molecular Nodes custom node group."
bl_options = {"REGISTER", "UNDO"}
node_name: bpy.props.StringProperty(
node_name: StringProperty( # type: ignore
name="node_name", description="", default="", subtype="NONE", maxlen=0
)
node_label: bpy.props.StringProperty(name="node_label", default="")
node_description: bpy.props.StringProperty(
node_label: StringProperty(name="node_label", default="") # type: ignore
node_description: StringProperty( # type: ignore
name="node_description",
description="",
default="Add MolecularNodes custom node group.",
subtype="NONE",
)
node_link: bpy.props.BoolProperty(name="node_link", default=True)
node_link: BoolProperty(name="node_link", default=True) # type: ignore

@classmethod
def description(cls, context, properties):
Expand All @@ -26,7 +61,7 @@ def description(cls, context, properties):
def execute(self, context):
try:
nodes.append(self.node_name, link=self.node_link)
nodes.add_node(self.node_name) # , label=self.node_label)
_add_node(self.node_name, context) # , label=self.node_label)
except RuntimeError:
self.report(
{"ERROR"},
Expand All @@ -35,13 +70,13 @@ def execute(self, context):
return {"FINISHED"}


class MN_OT_Assembly_Bio(bpy.types.Operator):
class MN_OT_Assembly_Bio(Operator):
bl_idname = "mn.assembly_bio"
bl_label = "Build"
bl_description = "Adds node to build biological assembly based on symmetry operations that are extraced from the structure file"
bl_options = {"REGISTER", "UNDO"}

inset_node: bpy.props.BoolProperty(default=False)
inset_node: BoolProperty(default=False) # type: ignore

@classmethod
def poll(self, context):
Expand All @@ -62,7 +97,7 @@ def execute(self, context):
nodes.assembly_insert(bob)
else:
tree_assembly = nodes.assembly_initialise(bob)
nodes.add_node(tree_assembly.name)
_add_node(tree_assembly.name, context)
except (KeyError, ValueError) as e:
self.report({"ERROR"}, "Unable to build biological assembly node.")
self.report({"ERROR"}, str(e))
Expand All @@ -71,25 +106,25 @@ def execute(self, context):
return {"FINISHED"}


class MN_OT_iswitch_custom(bpy.types.Operator):
class MN_OT_iswitch_custom(Operator):
bl_idname = "mn.iswitch_custom"
# bl_idname = "mn.selection_custom"
bl_label = "Chain Selection"
bl_options = {"REGISTER", "UNDO"}

description: bpy.props.StringProperty(name="Description")
dtype: bpy.props.EnumProperty( # type: ignore
description: StringProperty(name="Description") # type: ignore
dtype: EnumProperty( # type: ignore
name="Data type",
items=(
("RGBA", "RGBA", "Color iswitch."),
("BOOLEAN", "BOOLEAN", "Boolean iswitch"),
),
)
field: bpy.props.StringProperty(name="field", default="chain_id")
prefix: bpy.props.StringProperty(name="prefix", default="Chain ")
node_property: bpy.props.StringProperty(name="node_property", default="chain_ids")
node_name: bpy.props.StringProperty(name="node_name", default="chain")
starting_value: bpy.props.IntProperty(name="starting_value", default=0)
field: StringProperty(name="field", default="chain_id") # type: ignore
prefix: StringProperty(name="prefix", default="Chain ") # type: ignore
node_property: StringProperty(name="node_property", default="chain_ids") # type: ignore
node_name: StringProperty(name="node_name", default="chain") # type: ignore
starting_value: IntProperty(name="starting_value", default=0) # type: ignore

@classmethod
def description(cls, context, properties):
Expand Down Expand Up @@ -122,20 +157,20 @@ def execute(self, context):
prefix=self.prefix,
)

nodes.add_node(node_chains.name)
_add_node(node_chains.name, context)

return {"FINISHED"}


class MN_OT_Residues_Selection_Custom(bpy.types.Operator):
class MN_OT_Residues_Selection_Custom(Operator):
bl_idname = "mn.residues_selection_custom"
bl_label = "Multiple Residue Selection"
bl_description = "Create a selection based on the provided residue strings.\nThis \
node is built on a per-molecule basis, taking into account the residues that \
were input."
bl_options = {"REGISTER", "UNDO"}

input_resid_string: bpy.props.StringProperty(
input_resid_string: StringProperty( # type: ignore
name="Select residue IDs: ",
description="Enter a string value.",
default="19,94,1-16",
Expand All @@ -147,29 +182,50 @@ def execute(self, context):
input_resid_string=self.input_resid_string,
)

nodes.add_node(node_residues.name)
_add_node(node_residues.name, context)
return {"FINISHED"}

def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)


class MN_OT_Change_Style(bpy.types.Operator):
class MN_OT_Change_Style(Operator):
bl_idname = "mn.style_change"
bl_label = "Style"

style: bpy.props.EnumProperty(name="Style", items=nodes.STYLE_ITEMS)
style: EnumProperty(name="Style", items=STYLE_ITEMS) # type: ignore

def execute(self, context):
object = context.active_object
nodes.change_style_node(object, self.style)
return {"FINISHED"}


class MN_OT_Swap_Style_Node(bpy.types.Operator):
bl_idname = "mn.style_change_node"
bl_label = "Style"

style: bpy.props.EnumProperty(name="Style", items=STYLE_ITEMS) # type: ignore

@classmethod
def poll(self, context):
node = context.space_data.edit_tree.nodes.active
return node.name.startswith("Style")

def execute(self, context):
nodes.swap_style_node(
tree=context.space_data.node_tree,
node_style=context.space_data.edit_tree.nodes.active,
style=self.style,
)
return {"FINISHED"}


CLASSES = [
MN_OT_Add_Custom_Node_Group,
MN_OT_Residues_Selection_Custom,
MN_OT_Change_Style,
MN_OT_Assembly_Bio,
MN_OT_iswitch_custom,
MN_OT_Swap_Style_Node,
]
Loading

0 comments on commit e71e850

Please sign in to comment.