def test_methods(self): """Test basic methods of NcAttrs""" # A blendshape is used, because it has "sub-attributes": .attr_a.attr_b # This comes in handy to test the "concatenation" of _held_attrs_list, # when these NcAttrs methods are invoked. test_blendshape = cmds.createNode("blendShape", name=TEST_BLENDSHAPE) # Create NcNode instance that will be used as an inConnection in_connection_plug = "{}.translateX".format(TEST_TRANSFORM) in_connection_node = noca.NcNode(in_connection_plug) # Define general variables used later on base_attribute = "inputTarget[0]" full_attribute = "{}.sculptInbetweenWeight".format(base_attribute) full_plug = "{}.{}".format(TEST_BLENDSHAPE, full_attribute) # Create NcAttrs test instance (manually with NcNode instance!) node_instance = noca.NcNode(TEST_BLENDSHAPE) attrs_instance = noca.NcAttrs(node_instance, base_attribute) # Test getattr method result = attrs_instance.sculptInbetweenWeight self.assertIsInstance(result, noca.NcAttrs) self.assertEqual(result.plugs, [full_plug]) # Test setattr method by comparing values before and after setting self.assertEqual(cmds.getAttr(full_plug), -1) attrs_instance.sculptInbetweenWeight = TEST_VALUE self.assertEqual(cmds.getAttr(full_plug), TEST_VALUE) attrs_instance.sculptInbetweenWeight = in_connection_node connections = cmds.listConnections(full_plug, connections=True, plugs=True) self.assertEqual(connections, [full_plug, in_connection_plug]) # Delete and recreated blendshape to start fresh for getitem/setitem methods cmds.delete(test_blendshape) cmds.createNode("blendShape", name=TEST_BLENDSHAPE) # Test getitem method node_instance = noca.NcNode(TEST_BLENDSHAPE) attrs_instance = noca.NcAttrs(node_instance, full_attribute) result = attrs_instance[0] self.assertIsInstance(result, noca.NcAttrs) self.assertEqual(result.plugs, [full_plug]) # Test setitem method self.assertEqual(cmds.getAttr(full_plug), -1) attrs_instance[0] = TEST_VALUE self.assertEqual(cmds.getAttr(full_plug), TEST_VALUE) attrs_instance[0] = in_connection_node connections = cmds.listConnections(full_plug, connections=True, plugs=True) self.assertEqual(connections, [full_plug, in_connection_plug])
def test_initialization(self): """Test the instantiation of the NcAttrs class""" # Test various ways a NcAttrs class can be instantiated nc_attrs_as_argument = noca.NcAttrs(self.node_instance, "tx") args_options = [ None, "tx", ["tx"], ["tx", "ty"], nc_attrs_as_argument, ] for attrs_arg in args_options: expected_attrs_arg = attrs_arg if isinstance(attrs_arg, noca.NcAttrs): expected_attrs_arg = attrs_arg.attrs_list if attrs_arg is None: expected_attrs_arg = [] if not isinstance(expected_attrs_arg, list): expected_attrs_arg = [expected_attrs_arg] attrs_instance = noca.NcAttrs(self.node_instance, attrs_arg) self.assertEqual(attrs_instance.node, TEST_TRANSFORM) self.assertEqual(attrs_instance.attrs_list, expected_attrs_arg) # Test auto_unravel & auto_consolidate of NcAttrs initialization for state in [True, False]: node_instance = noca.NcNode(TEST_TRANSFORM, auto_unravel=state) attrs_instance = node_instance.tx self.assertEqual(attrs_instance._auto_unravel, state) node_instance = noca.NcNode(TEST_TRANSFORM, auto_consolidate=state) attrs_instance = node_instance.tx self.assertEqual(attrs_instance._auto_consolidate, state)
def test_initialization(self): """Test the instantiation of the NcNode class""" # Test various ways a NcNode class can be instantiated nc_node_as_argument = noca.NcNode(TEST_TRANSFORM, "tx") nc_attrs_as_argument = noca.NcAttrs(nc_node_as_argument, "tx") args_options = [ ["A", None], ["A.t", None], ["A.tx", None], ["A", ["tx"]], ["A", ["tx", "ty"]], [nc_node_as_argument, None], [nc_attrs_as_argument, None], ] for node_arg, attrs_arg in args_options: expected_node_arg = node_arg expected_attrs_arg = attrs_arg if isinstance(node_arg, noca.NcBaseNode): expected_node_arg = node_arg.node expected_attrs_arg = node_arg.attrs_list elif "." in node_arg: expected_node_arg, expected_attrs_arg = node_arg.split(".", 1) elif attrs_arg is None: expected_attrs_arg = [] if not isinstance(expected_attrs_arg, list): expected_attrs_arg = [expected_attrs_arg] node_instance = noca.NcNode(node_arg, attrs_arg) self.assertEqual(node_instance.node, expected_node_arg) self.assertEqual(node_instance.attrs_list, expected_attrs_arg) # Test auto_unravel & auto_consolidate setting at NcNode initialization for state in [True, False]: node_instance = noca.NcNode(TEST_TRANSFORM, auto_unravel=state) self.assertEqual(node_instance._auto_unravel, state) node_instance = noca.NcNode(TEST_TRANSFORM, auto_consolidate=state) self.assertEqual(node_instance._auto_consolidate, state) # Test auto_unravel & auto_consolidate setting when initialized with NcNode for state in [True, False]: node_instance = noca.NcNode(TEST_TRANSFORM, auto_unravel=state) # First; initialize NcNode without specifying auto_unravel-state: # auto_unravel behaviour should mimic the state of the given NcNode node_offspring = noca.NcNode(node_instance) self.assertEqual(node_offspring._auto_unravel, state) # Second; A given auto_unravel state precedes the inherited state node_offspring = noca.NcNode(node_instance, auto_unravel=not state) self.assertEqual(node_offspring._auto_unravel, not state) # auto_consolidate testing similar to auto_unravel tests above node_instance = noca.NcNode(TEST_TRANSFORM, auto_consolidate=state) node_offspring = noca.NcNode(node_instance) self.assertEqual(node_offspring._auto_consolidate, state) node_offspring = noca.NcNode(node_instance, auto_consolidate=not state) self.assertEqual(node_offspring._auto_consolidate, not state)
def setUp(self): # Create regular polyCube transform & shape node self.test_mesh = cmds.polyCube(name=TEST_MESH, constructionHistory=False)[0] # Add a second shape under the same transform node shape_duplicate = cmds.duplicate(self.test_mesh)[0] cmds.parent( cmds.listRelatives(shape_duplicate, shapes=True)[0], self.test_mesh, relative=True, shape=True, ) cmds.delete(shape_duplicate) self.test_shapes = cmds.listRelatives(self.test_mesh, shapes=True) self.node_mesh = noca.NcNode(self.test_mesh) self.test_transform = cmds.createNode("transform", name=TEST_TRANSFORM) self.node_single_attr = noca.NcNode(self.test_transform, TEST_ATTRIBUTE_SINGLE) self.node_parent_attr = noca.NcNode(self.test_transform, TEST_ATTRIBUTE_PARENT) self.node_multi_attr = noca.NcNode(self.test_transform, TEST_ATTRIBUTE_MULTI) self.attrs_single_attr = noca.NcAttrs(self.node_single_attr, TEST_ATTRIBUTE_SINGLE) self.attrs_parent_attr = noca.NcAttrs(self.node_single_attr, TEST_ATTRIBUTE_PARENT) self.attrs_multi_attr = noca.NcAttrs(self.node_multi_attr, TEST_ATTRIBUTE_MULTI) self.test_items = [ self.node_single_attr, self.node_parent_attr, self.node_multi_attr, self.attrs_single_attr, self.attrs_parent_attr, self.attrs_multi_attr, ] # The desired attrs and values must match the order of the test_items! self.desired_attrs = [ TEST_ATTRIBUTE_SINGLE, TEST_ATTRIBUTE_PARENT, TEST_ATTRIBUTE_MULTI, TEST_ATTRIBUTE_SINGLE, TEST_ATTRIBUTE_PARENT, TEST_ATTRIBUTE_MULTI, ] self.desired_values_a = [ TEST_POSITION_A[0], TEST_POSITION_A, TEST_POSITION_A, TEST_POSITION_A[0], TEST_POSITION_A, TEST_POSITION_A, ] self.desired_values_b = [ TEST_POSITION_B[0], TEST_POSITION_B, TEST_POSITION_B, TEST_POSITION_B[0], TEST_POSITION_B, TEST_POSITION_B, ]