Skip to content

Commit

Permalink
Fix some mypy errors after upgrading to mypy 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lathreas committed Feb 18, 2023
1 parent 61a9038 commit d85633a
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 96 deletions.
119 changes: 65 additions & 54 deletions scripts/anatomic_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/python3

from typing import TYPE_CHECKING
import math
import bpy
from ..modules.geoscript import nodetrees
Expand Down Expand Up @@ -99,60 +100,70 @@ class Tubercule(BlenderObjectWrapper):
class ARF_HingeJointProperties(bpy.types.PropertyGroup):
"""CollectionProperty"""

angle1: bpy.props.FloatProperty(
name="Angle 1",
description="The starting angle of a hingejoint.",
min=-math.pi,
max=math.pi,
step=0.1 * math.pi,
)
angle2: bpy.props.FloatProperty(
name="Angle 2",
description="The ending angle of a hingejoint.",
min=-math.pi,
max=math.pi,
step=0.1 * math.pi,
)
smoothness_angle1: bpy.props.FloatProperty(
name="Angle 1 Smoothness",
description="The amount of blending between the hinge joint's articular surface and the surrounding bone at the wedge's start angle.",
min=0.0,
max=10.0,
step=0.1,
)
smoothness_angle2: bpy.props.FloatProperty(
name="Angle 2 Smoothness",
description="The amount of blending between the hinge joint's articular surface and the surrounding bone at the wedge's end angle.",
min=0.0,
max=10.0,
step=0.1,
)
smoothness_cap1: bpy.props.FloatProperty(
name="Cap 1 Smoothness",
description="The amount of blending between the hinge joint's articular surface and the surrounding bone at the cylinder's start cap.",
min=0.0,
max=10.0,
step=0.1,
)
smoothness_cap2: bpy.props.FloatProperty(
name="Cap 2 Smoothness",
description="The amount of blending between the hinge joint's articular surface and the surrounding bone at the cylinder's end cap.",
min=0.0,
max=10.0,
step=0.1,
)
resolution_arc: bpy.props.IntProperty(
name="Arc Resolution",
description="The amount of subdivisions over the arc of the cylinder. Higher is more precise, lower is faster.",
min=1,
soft_max=32,
)
resolution_length: bpy.props.IntProperty(
name="Length Resolution",
description="The amount of subdivisions over the length of the cylinder. Higher is more precise, lower is faster.",
min=1,
soft_max=32,
)
if TYPE_CHECKING:
angle1: bpy.types.FloatProperty
angle2: bpy.types.FloatProperty
smoothness_angle1: bpy.types.FloatProperty
smoothness_angle2: bpy.types.FloatProperty
smoothness_cap1: bpy.types.FloatProperty
smoothness_cap2: bpy.types.FloatProperty
resolution_arc: bpy.types.IntProperty
resolution_length: bpy.types.IntProperty
else:
angle1: bpy.props.FloatProperty(
name="Angle 1",
description="The starting angle of a hingejoint.",
min=-math.pi,
max=math.pi,
step=0.1 * math.pi,
)
angle2: bpy.props.FloatProperty(
name="Angle 2",
description="The ending angle of a hingejoint.",
min=-math.pi,
max=math.pi,
step=0.1 * math.pi,
)
smoothness_angle1: bpy.props.FloatProperty(
name="Angle 1 Smoothness",
description="The amount of blending between the hinge joint's articular surface and the surrounding bone at the wedge's start angle.",
min=0.0,
max=10.0,
step=0.1,
)
smoothness_angle2: bpy.props.FloatProperty(
name="Angle 2 Smoothness",
description="The amount of blending between the hinge joint's articular surface and the surrounding bone at the wedge's end angle.",
min=0.0,
max=10.0,
step=0.1,
)
smoothness_cap1: bpy.props.FloatProperty(
name="Cap 1 Smoothness",
description="The amount of blending between the hinge joint's articular surface and the surrounding bone at the cylinder's start cap.",
min=0.0,
max=10.0,
step=0.1,
)
smoothness_cap2: bpy.props.FloatProperty(
name="Cap 2 Smoothness",
description="The amount of blending between the hinge joint's articular surface and the surrounding bone at the cylinder's end cap.",
min=0.0,
max=10.0,
step=0.1,
)
resolution_arc: bpy.props.IntProperty(
name="Arc Resolution",
description="The amount of subdivisions over the arc of the cylinder. Higher is more precise, lower is faster.",
min=1,
soft_max=32,
)
resolution_length: bpy.props.IntProperty(
name="Length Resolution",
description="The amount of subdivisions over the length of the cylinder. Higher is more precise, lower is faster.",
min=1,
soft_max=32,
)


class ARF_JointProperties(bpy.types.PropertyGroup):
Expand Down
107 changes: 65 additions & 42 deletions scripts/ui/bone_properties.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
#!/usr/bin/python3

from typing import Any
from typing import Any, Optional, TYPE_CHECKING
import bpy

from ..anatomic_types import ARF_GeneralObjectProperties


class UI_UL_BoneModifiers(bpy.types.UIList):
def draw_item(
self,
context: bpy.types.Context,
context: Optional[bpy.types.Context],
layout: bpy.types.UILayout,
data: bpy.types.AnyType,
item: bpy.types.AnyType,
icon: int,
data: Optional[bpy.types.AnyType],
item: Optional[bpy.types.AnyType],
icon: Optional[int],
active_data: bpy.types.AnyType,
active_propname: str,
index: int = 0,
flt_flag: int = 0,
index: Optional[int] = 0,
flt_flag: Optional[int] = 0,
) -> Any:
"""Draws an item in a collection.
Expand All @@ -40,7 +42,8 @@ def draw_item(
them here if you don't need them.
"""
icon_name = "X"
if item and item.constraint_object:
# if item and item.constraint_object:
if item and hasattr(item, "constraint_object"):
if item.constraint_object.ARF.object_type == "JOINT":
icon_name = "RESTRICT_COLOR_ON"
elif item.constraint_object.ARF.object_type == "TUBERCLE":
Expand All @@ -64,19 +67,27 @@ class CUSTOM_OT_list_action(bpy.types.Operator):
bl_description = "Move items up and down, add and remove"
bl_options = {"REGISTER"}

action: bpy.props.EnumProperty(
items=(
("UP", "Up", ""),
("DOWN", "Down", ""),
("REMOVE", "Remove", ""),
("ADD", "Add", ""),
)
enumitems = (
("UP", "Up", ""),
("DOWN", "Down", ""),
("REMOVE", "Remove", ""),
("ADD", "Add", ""),
)

# Hacky workaround to silence mypy false flag when using props.EnumProperty():
if TYPE_CHECKING:
action: bpy.types.EnumProperty
else:
action: bpy.props.EnumProperty(items=enumitems)

def invoke(self, context: bpy.types.Context, event: bpy.types.Event):
obj = context.object
obj_ARF_data = obj.ARF # type: ignore
bone_properties = obj_ARF_data.bone_properties

if not hasattr(obj, "ARF"):
return
assert isinstance(obj.ARF, ARF_GeneralObjectProperties)

bone_properties = obj.ARF.bone_properties
idx = bone_properties.geometry_constraint_index

try:
Expand Down Expand Up @@ -157,16 +168,26 @@ class VIEW_3D_PT_ARF_Properties(bpy.types.Panel):
bl_region_type = "UI"

def draw(self, context: bpy.types.Context):
layout = self.layout
obj = context.object

obj_ARF_data = obj.ARF # type: ignore
if not hasattr(obj, "ARF"):
return
assert isinstance(obj.ARF, ARF_GeneralObjectProperties)

self.draw_properties_header(obj)
self.draw_feature_drawing_buttons()
self.draw_modifier_list(obj)
self.draw_constraint_properties(obj.ARF)

def draw_properties_header(self, obj: bpy.types.Object):
assert hasattr(obj, "ARF")
layout = self.layout
layout.row().label(text="Bone geometry constraints", icon="CONSTRAINT_BONE")
layout.row().label(text=obj.name)
layout.row().prop(obj_ARF_data, "object_type", text="Object Type")
layout.row().prop(obj.ARF, "object_type", text="Object Type")

row = layout.row()
def draw_feature_drawing_buttons(self):
row = self.layout.row()
col = row.column(align=True)
row = col.row(align=True)
row.operator("arf_bone_mode.draw_tubercle", icon="STROKE", text="Draw tubercle")
Expand All @@ -177,37 +198,21 @@ def draw(self, context: bpy.types.Context):
text="Draw muscle insertion",
)

self.draw_modifier_list(obj)

index = obj_ARF_data.bone_properties.geometry_constraint_index
geometry_constraints = obj_ARF_data.bone_properties.geometry_constraints

# Don't display info if no entry is selected, i.e. index is out of bounds:
if len(geometry_constraints) <= index:
return

item = geometry_constraints[index]
layout.row().prop(item, "constraint_type", text="Type")
layout.row().prop(item, "name", text="Name")
layout.row().prop(item, "constraint_object", text="Constraint Object")

if item and item.constraint_object:
if item.constraint_object.ARF.object_type == "JOINT":
row = layout.row()
row.prop(item, "inner_outer_surface", text="Attach To...")

def draw_modifier_list(self, obj: bpy.types.Object) -> None:
row = self.layout.row()
self.draw_modifier_list_selector(row, obj)
self.draw_modifier_list_buttons(row)

def draw_modifier_list_selector(self, row, obj: bpy.types.Object) -> None:
if not hasattr(obj, "ARF"):
return
assert isinstance(obj.ARF, ARF_GeneralObjectProperties)
row.template_list(
"UI_UL_BoneModifiers",
"Object Modifiers",
obj.ARF.bone_properties, # type: ignore
obj.ARF.bone_properties,
"geometry_constraints",
obj.ARF.bone_properties, # type: ignore
obj.ARF.bone_properties,
"geometry_constraint_index",
rows=1,
)
Expand All @@ -220,6 +225,24 @@ def draw_modifier_list_buttons(self, row) -> None:
col.operator("custom.list_action", icon="TRIA_UP", text="").action = "UP"
col.operator("custom.list_action", icon="TRIA_DOWN", text="").action = "DOWN"

def draw_constraint_properties(self, obj_ARF: ARF_GeneralObjectProperties) -> None:
index = obj_ARF.bone_properties.geometry_constraint_index
geometry_constraints = obj_ARF.bone_properties.geometry_constraints

# Don't display info if no entry is selected, i.e. index is out of bounds:
if len(geometry_constraints) <= index:
return

item = geometry_constraints[index]
self.layout.row().prop(item, "constraint_type", text="Type")
self.layout.row().prop(item, "name", text="Name")
self.layout.row().prop(item, "constraint_object", text="Constraint Object")

if item and item.constraint_object:
if item.constraint_object.ARF.object_type == "JOINT":
row = self.layout.row()
row.prop(item, "inner_outer_surface", text="Attach To...")


class CUSTOM_colorCollection(bpy.types.PropertyGroup):
# name: StringProperty() -> Instantiated by default
Expand Down

0 comments on commit d85633a

Please sign in to comment.