Skip to content

Commit

Permalink
Fixed node registration a bit more so it properly cast the dag_node t…
Browse files Browse the repository at this point in the history
…o anvil object.

Worked a bit more on buffer inserts...added logic for replacing the head.
Realized I swapped the collocation logic, need to fix the insert...currently it is not adding the children of the insert target to the new buffer.
Added back the anvil namespace to the module functions as it was causing a circular import.

Mapped anvil.factory to most of the node query functions on Transform.

on BipedFoot cast all inputs to anvil.factory and built out insert_pivot_buffer a bunch.
Added test case for bipedFoot + insole/outsole and added files to boot.
  • Loading branch information
AndresMWeber committed Feb 10, 2018
1 parent ddbae84 commit d09198b
Show file tree
Hide file tree
Showing 8 changed files with 532 additions and 32 deletions.
10 changes: 6 additions & 4 deletions anvil/grouping/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ def register_node(self, node_key, dag_node, overwrite=True, name_tokens=None, me
if dag_node is None:
self.warning('Attempted register node %s with key %s but it does not exist', dag_node, node_key)
return
try:
anvil.factory(dag_node)
except:
raise TypeError('Could not register unrecognized node type %s is not an anvil grouping or object class.')

if not anvil.is_anvil(dag_node):
try:
dag_node = anvil.factory(dag_node)
except:
raise TypeError('Could not register unrecognized node type %s is not an anvil grouping or object class.' % type(dag_node))

if self.hierarchy.get(node_key) is not None and not overwrite:
raise IndexError('Preexisting node already is stored under key %s in the hierarchy' % node_key)
Expand Down
38 changes: 23 additions & 15 deletions anvil/grouping/traversal.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from six import iteritems
import nomenclate.core.tools as ts
from anvil import factory, is_anvil
from anvil.log import LogMixin, obtainLogger
import anvil
import anvil.config as cfg
import anvil.runtime as rt
import anvil.objects as ob
import anvil.utils.generic as gc
import anvil.utils.scene as sc
from anvil.log import LogMixin, obtainLogger

class HierarchyChain(LogMixin):
LOG = obtainLogger(__name__)
Expand Down Expand Up @@ -34,7 +34,7 @@ def find_child(self, child):
if isinstance(child, int):
return self[child]

child = factory(child)
child = anvil.factory(child)
for node in self:
if node == child:
return node
Expand All @@ -48,8 +48,12 @@ def get_hierarchy(self, node_filter=None, as_list=False):
return hierarchy

def get_level(self, desired_level, traversal=None, level_tree=None, node_filter=None):
""" Returns a dictionary at depth "desired_level" from the hierarchy.
Returns {} if nothing is found at that depth.
"""
:param desired_level: int, level we are looking to get
:param traversal: iterable, arbitrary iterable to use instead of own hierarchy
:param node_filter: list(str), list of strings that represent nodes types to ignore.
:return: dict, dictionary at desired depth from the traversal or hierarchy, {} if depth does not exist
"""
node_filter = node_filter or self.node_filter
if level_tree is None:
Expand All @@ -70,7 +74,7 @@ def get_level(self, desired_level, traversal=None, level_tree=None, node_filter=
return level_tree

def insert_buffer(self, index_target, reference_node=None, buffer_node_class=None, collocate=False, **kwargs):
"""
""" Inserts node of type buffer_node_class at the index specified.
:param index_target: int or str or ob.UnicodeProxy, child index, child dag string or anvil object.
:param reference_node: str or ob.Transform, either a dag path or anvil node to match position to
Expand All @@ -80,11 +84,15 @@ def insert_buffer(self, index_target, reference_node=None, buffer_node_class=Non
"""
index_target = self.find_child(index_target)

buffer = (buffer_node_class if is_anvil(buffer_node_class) else self.DEFAULT_BUFFER_TYPE).build(**kwargs)
buffer.parent(index_target if collocate else index_target.get_parent())
buffer = (buffer_node_class if anvil.is_anvil(buffer_node_class) else self.DEFAULT_BUFFER_TYPE).build(**kwargs)
buffer.parent(index_target.get_parent() or None if collocate else index_target)
buffer.reset_transform()
buffer.match_transform(reference_node)
map(lambda node: node.parent(buffer), [index_target] if collocate else index_target.children())
map(lambda node: node.parent(buffer), [index_target] if collocate else index_target.get_children())

# If user specified the head we need to set it to the new head which is the buffer node.
if index_target == self.head:
self.head = buffer

return buffer

Expand Down Expand Up @@ -126,13 +134,13 @@ def _process_top_node(self, top_node, end_node, duplicate=False):
if duplicate:
top_node, end_node = self.duplicate_chain(top_node, end_node=end_node)

return factory(top_node), end_node
return anvil.factory(top_node), end_node

def _process_end_node(self, end_node_candidate, node_filter=None):
""" Returns the last item found of type
"""
try:
return factory(end_node_candidate)
return anvil.factory(end_node_candidate)
except (RuntimeError, IOError):
return self._get_linear_end(node_filter=node_filter)

Expand All @@ -143,11 +151,11 @@ def _traverse_up_linear_tree(self, downstream_node, upstream_node, node_filter=N
raise IndexError('Node %r is not a descendant of %s --> %s (filter=%s)' %
(downstream_node, upstream_node, all_descendants, self.node_filter))

current_node = factory(downstream_node)
current_node = anvil.factory(downstream_node)
chain_path = [current_node]

while factory(current_node).get_parent():
current_node = factory(current_node.get_parent())
while anvil.factory(current_node).get_parent():
current_node = anvil.factory(current_node.get_parent())
if any([current_node.type() in node_filter, node_filter is None, node_filter == []]):
chain_path.insert(0, current_node)
if current_node == upstream_node:
Expand All @@ -165,7 +173,7 @@ def _get_linear_end(self, node_filter=None):
node_filter = node_filter or self.node_filter
last_node = self.get_level(self.depth(), node_filter=node_filter)
if last_node:
return factory(list(last_node)[0])
return anvil.factory(list(last_node)[0])
else:
raise ValueError('Could not find last node at depth %d' % self.depth())

Expand Down
11 changes: 5 additions & 6 deletions anvil/objects/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@ def create_engine_instance(**flags):
return rt.dcc.create.create_transform(**flags)

def get_parent(self):
parents = rt.dcc.scene.list_relatives(self.name(), parent=True)
parents = map(anvil.factory, rt.dcc.scene.list_relatives(self.name(), parent=True))
try:
return parents[0]
except IndexError:
return parents

def get_children(self, **kwargs):
return rt.dcc.scene.list_relatives(self.name(), children=True, **kwargs)
return map(anvil.factory, rt.dcc.scene.list_relatives(self.name(), children=True, **kwargs))

def reset_transform(self):
self.translate.set(0, 0, 0)
self.rotate.set(0, 0, 0)
self.translate.set((0, 0, 0))
self.rotate.set((0, 0, 0))

def parent(self, new_parent):
self.debug('Parenting %s to %s', self, new_parent)
top_node, new_parent = self, new_parent
nodes_exist = [rt.dcc.scene.exists(node) for node in [top_node, new_parent] if node != None]
nodes_exist = [rt.dcc.scene.exists(node) for node in [top_node, new_parent] if node]
if all(nodes_exist or [False]):
rt.dcc.scene.parent(top_node, new_parent)
return True
Expand Down
25 changes: 18 additions & 7 deletions anvil/sub_rig_templates/biped_foot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from base import SubRigTemplate
import anvil
import anvil.node_types as nt
import anvil.config as cfg
import anvil.runtime as rt
from anvil.meta_data import MetaData


class BipedFoot(SubRigTemplate):
Expand All @@ -26,9 +28,9 @@ class BipedFoot(SubRigTemplate):
def __init__(self, heel=None, outsole=None, insole=None, has_ik=True, leg_ik=None, *args, **kwargs):
super(BipedFoot, self).__init__(*args, **kwargs)
self.ankle, self.ball, self.toe = self.layout_joints
self.heel = heel
self.outsole = outsole
self.insole = insole
self.heel = anvil.factory(heel) if heel else heel
self.outsole = anvil.factory(outsole) if outsole else outsole
self.insole = anvil.factory(insole) if insole else insole
self.has_ik = has_ik
self.leg_ik = leg_ik

Expand Down Expand Up @@ -74,6 +76,12 @@ def build(self, duplicate=True, leg_ik=None, **kwargs):
self.build_ik_toe()
else:
self.build_fk_toe()

control_hierarchy = nt.HierarchyChain(ankle_control.connection_group)

self.insert_pivot_buffer(self.OUTSOLE_TOKEN, control_hierarchy, 0)
self.insert_pivot_buffer(self.INSOLE_TOKEN, control_hierarchy, 0)

self.rename()

def build_ik_toe(self):
Expand All @@ -97,12 +105,15 @@ def build_ik_toe(self):
else:
rt.dcc.connections.parent(self.control_ball.connection_group, self.ankle)

def insert_pivot_buffer(self, pivot_label, **kwargs):
def insert_pivot_buffer(self, pivot, hierarchy, index, name_tokens=None, meta_data=None, **kwargs):
try:
pivot_reference_node = getattr(self, pivot_label)

buffer = hierarchy.insert_buffer(index, reference_node=getattr(self, pivot), **kwargs)
self.register_node(pivot + '_pivot', buffer,
name_tokens=MetaData(name=pivot, purpose='pivot', protected='purpose') + name_tokens,
meta_data=meta_data)
return buffer
except AttributeError:
self.warning('%r does not have pivot attribute %s...skipping' % (self, pivot_label))
self.warning('%r does not have pivot attribute %s...skipping' % (self, pivot))

def build_fk_toe(self):
md = self.register_node('ball_rotation_cancel_out',
Expand Down
6 changes: 6 additions & 0 deletions tests/acceptance/test_biped_foot.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def test_build_with_parent(self):
rig_instance = self.from_template_file(self.FOOT, parent=parent)
self.assertEqual(str(rig_instance.root.get_parent()), str(parent))

def test_build_with_leg_ik_and_soles(self):
with cleanup_nodes():
rig_instance = self.from_template_file(self.FOOT_WITH_LEG_AND_SOLES, insole='insole', outsole='outsole',
pre_build_hook=self.build_leg_ik)
# TODO: Test info about soles here

def test_build_with_leg_ik(self):
with cleanup_nodes():
parent = nt.Transform.build(name='test')
Expand Down
Loading

0 comments on commit d09198b

Please sign in to comment.