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

Extracted the processing of each joint position (jpo) on the jointStructure method into separate methods #377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
144 changes: 60 additions & 84 deletions release/scripts/mgear/shifter/component/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ def step_04(self):
"""
Step 04. Joint structure creation.
"""
self.retrieveParentJoint()
self.jointStructure()
self.setJointRadius()
return

def step_05(self):
Expand Down Expand Up @@ -1950,13 +1952,8 @@ def postConnect(self):
# =====================================================
# JOINTS STRUCTURE
# =====================================================
def jointStructure(self):
"""Build the Joint structure

Handle the building of the joint structure, when we select jnt_org
option.

"""
def retrieveParentJoint(self):
"""Retrieve the parent joint based on settings."""
# get parent component joint
if self.settings["useIndex"]:
try:
Expand Down Expand Up @@ -1991,12 +1988,8 @@ def jointStructure(self):
except Exception:
if oParent_comp.parent_comp:
pgpc = oParent_comp.guide.parentComponent
parent_name = pgpc.getName(
oParent_comp.guide.parentLocalName
)
relative_name = oParent_comp.rig.getRelativeName(
parent_name
)
parent_name = pgpc.getName(oParent_comp.guide.parentLocalName)
relative_name = oParent_comp.rig.getRelativeName(parent_name)
else:
pm.displayInfo(
"The parent components for: %s don't have joint "
Expand All @@ -2006,99 +1999,82 @@ def jointStructure(self):

oParent_comp = oParent_comp.parent_comp

# Joint creation
for jpo in self.jnt_pos:
def jointStructure(self):
"""Build the Joint structure

Handle the building of the joint structure, when we select jnt_org
option.

# NOTE: using a list was a mitake. Adding support for a kwargs dict
"""
# Create joints based on joint positions.
for jpo in self.jnt_pos:
if isinstance(jpo, list):
if len(jpo) >= 3 and self.options["joint_rig"]:
if jpo[2] == "component_jnt_org":
newActiveJnt = self.component_jnt_org
elif jpo[2] == "parent_relative_jnt":
# this option force the active jnt always to the parent
# relative jnt.
# If None the active jnt will be updated to the latest in
# each jnt creation
newActiveJnt = self.parent_relative_jnt
else:
try:
# here jpo[2] is also the string name of the jnt inside
# the component. IE: "root"
newActiveJnt = self.jointList[
self.jointRelatives[jpo[2]]
]

except Exception:
if jpo[2]:
pm.displayWarning(
"Joint Structure creation: "
"The object %s can't be found. Joint parent is"
" NONE for %s, from %s"
% (jpo[2], jpo[0], self.fullName)
)
newActiveJnt = None
else:
newActiveJnt = None
# Process joint position when provided as a list
newActiveJnt = self.determineActiveJoint(jpo, index=2)
# Handle the uniform scale
if len(jpo) >= 4 and self.options["joint_rig"]:
uniScale = jpo[3]
else:
uniScale = False

# TODO: handle rotation offset and vanilla nodes connection
# handle the matrix node connection

# Defaults to use Maya multiply Matrix node
if len(jpo) >= 5 and self.options["joint_rig"]:
gearMulMatrix = jpo[4]
else:
gearMulMatrix = False

# Handle rotation offset
rotOffset = jpo[5] if len(jpo) >= 6 else (0, 0, 0)

self.jointList.append(
self.addJoint(
jpo[0],
jpo[1],
newActiveJnt,
uniScale,
obj=jpo[0],
name=jpo[1],
newActiveJnt=newActiveJnt,
UniScale=uniScale,
gearMulMatrix=gearMulMatrix,
)
rot_off=rotOffset)
)
elif isinstance(jpo, dict):
if "newActiveJnt" in jpo.keys():
if jpo["newActiveJnt"] == "component_jnt_org":
jpo["newActiveJnt"] = self.component_jnt_org
elif jpo["newActiveJnt"] == "parent_relative_jnt":
# this option force the active jnt always to the parent
# relative jnt.
# If None the active jnt will be updated to the latest in
# each jnt creation
jpo["newActiveJnt"] = self.parent_relative_jnt
else:
try:
# here jpo["newActiveJnt"] is also the string name
# of the jnt inside the component. IE: "root"
jpo["newActiveJnt"] = self.jointList[
self.jointRelatives[jpo["newActiveJnt"]]
]

except Exception:
if jpo["newActiveJnt"]:
pm.displayWarning(
"Joint Structure creation: "
"The object %s can't be found. Joint parent is"
" NONE for %s, from %s"
% (
jpo["newActiveJnt"],
jpo["obj"],
self.fullName,
)
)
jpo["newActiveJnt"] = None

elif isinstance(jpo, dict):
# Process joint position when provided as a dictionary
if "newActiveJnt" in jpo:
jpo["newActiveJnt"] = self.determineActiveJoint(jpo, key="newActiveJnt")
self.jointList.append(self.addJoint(**jpo))

def determineActiveJoint(self, jpo, index=None, key=None):
"""Determine the active joint based on the joint position info."""

# Check if jpo is a list and index is within range
if isinstance(jpo, list) and index is not None and index < len(jpo):
joint_identifier = jpo[index]
# Check if jpo is a dictionary and key exists
elif isinstance(jpo, dict) and key is not None and key in jpo:
joint_identifier = jpo.get(key)
else:
# Handle cases where jpo does not have the expected index or key
return None

if joint_identifier == "component_jnt_org":
return self.component_jnt_org
elif joint_identifier == "parent_relative_jnt":
return self.parent_relative_jnt
else:
try:
return self.jointList[self.jointRelatives[joint_identifier]]
except Exception:
obj = jpo[0] if isinstance(jpo, list) else jpo["obj"]
if joint_identifier:
pm.displayWarning(
"Joint Structure creation: The object %s can't be found. "
"Joint parent is NONE for %s, from %s" % (joint_identifier, obj, self.fullName)
)
return None

def setJointRadius(self):
"""Set the radius for all joints."""
radiusValue = self.rig.guide.model.joint_radius.get()
for jnt in self.jointList:
radiusValue = self.rig.guide.model.joint_radius.get()
jnt.radius.set(radiusValue)

# =====================================================
Expand Down
Loading