test_nodepath.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. def test_nodepath_empty():
  2. """Tests NodePath behavior for empty NodePaths."""
  3. from panda3d.core import NodePath
  4. empty = NodePath('np')
  5. assert empty.get_pos() == (0, 0, 0)
  6. assert empty.get_hpr() == (0, 0, 0)
  7. assert empty.get_scale() == (1, 1, 1)
  8. def test_nodepath_parent():
  9. """Tests NodePath.reparentTo()."""
  10. from panda3d.core import NodePath
  11. np1 = NodePath('np')
  12. np2 = NodePath('np')
  13. assert np1.parent is None
  14. assert np2.parent is None
  15. np1.reparentTo(np2)
  16. assert np1.parent == np2
  17. assert np2.parent is None
  18. def test_nodepath_transform_changes():
  19. """Tests that NodePath applies transform changes to its managed node."""
  20. from panda3d.core import NodePath
  21. np = NodePath('np')
  22. assert np.get_pos() == (0, 0, 0)
  23. assert np.get_hpr() == (0, 0, 0)
  24. assert np.get_scale() == (1, 1, 1)
  25. np.set_pos(1, 2, 3)
  26. assert np.get_pos() == (1, 2, 3)
  27. assert np.node().get_transform().get_pos() == (1, 2, 3)
  28. def test_nodepath_transform_composition():
  29. """Tests that NodePath composes transform states according to the path it holds."""
  30. from panda3d.core import PandaNode, NodePath, LPoint3, LVector3
  31. # Create 3 PandaNodes, and give each some interesting transform state:
  32. node1 = PandaNode('node1')
  33. node2 = PandaNode('node2')
  34. node3 = PandaNode('node3')
  35. node1.set_transform(node1.get_transform().set_pos(LPoint3(0, 0, 1)).set_hpr(LVector3(90, 0, -90)))
  36. node2.set_transform(node2.get_transform().set_pos(LPoint3(0, 1, 0)).set_hpr(LVector3(180, 180, 0)))
  37. node3.set_transform(node3.get_transform().set_pos(LPoint3(1, 0, 0)).set_hpr(LVector3(270, 0, 270)))
  38. # node3 is going to be attached under both node1 and node2 and we will
  39. # hold a path both ways:
  40. node1.add_child(node3)
  41. node2.add_child(node3)
  42. assert len(node1.children) == 1
  43. assert len(node2.children) == 1
  44. assert len(node3.children) == 0
  45. assert len(node1.parents) == 0
  46. assert len(node2.parents) == 0
  47. assert len(node3.parents) == 2
  48. # np1 is the path to node3 via node1:
  49. np1 = NodePath(node1).children[0]
  50. # np2 is the path to node3 via node2:
  51. np2 = NodePath(node2).children[0]
  52. # Both should point to node3:
  53. assert np1.node() == node3
  54. assert np2.node() == node3
  55. # However if we ask for the net transform to node3, it should compose:
  56. assert np1.get_transform(NodePath()) == node1.get_transform().compose(node3.get_transform())
  57. assert np2.get_transform(NodePath()) == node2.get_transform().compose(node3.get_transform())
  58. # If we ask for np1 RELATIVE to np2, it should compose like so:
  59. leg1 = node2.get_transform().compose(node3.get_transform())
  60. leg2 = node1.get_transform().compose(node3.get_transform())
  61. relative_transform = leg1.get_inverse().compose(leg2)
  62. assert np1.get_transform(np2) == relative_transform