test_nodepath.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import pytest, sys
  2. def test_nodepath_empty():
  3. """Tests NodePath behavior for empty NodePaths."""
  4. from panda3d.core import NodePath
  5. empty = NodePath('np')
  6. assert empty.get_pos() == (0, 0, 0)
  7. assert empty.get_hpr() == (0, 0, 0)
  8. assert empty.get_scale() == (1, 1, 1)
  9. def test_nodepath_parent():
  10. """Tests NodePath.reparentTo()."""
  11. from panda3d.core import NodePath
  12. np1 = NodePath('np')
  13. np2 = NodePath('np')
  14. assert np1.parent is None
  15. assert np2.parent is None
  16. np1.reparentTo(np2)
  17. assert np1.parent == np2
  18. assert np2.parent is None
  19. def test_nodepath_transform_changes():
  20. """Tests that NodePath applies transform changes to its managed node."""
  21. from panda3d.core import NodePath
  22. np = NodePath('np')
  23. assert np.get_pos() == (0, 0, 0)
  24. assert np.get_hpr() == (0, 0, 0)
  25. assert np.get_scale() == (1, 1, 1)
  26. np.set_pos(1, 2, 3)
  27. assert np.get_pos() == (1, 2, 3)
  28. assert np.node().get_transform().get_pos() == (1, 2, 3)
  29. def test_nodepath_transform_composition():
  30. """Tests that NodePath composes transform states according to the path it holds."""
  31. from panda3d.core import PandaNode, NodePath, LPoint3, LVector3
  32. # Create 3 PandaNodes, and give each some interesting transform state:
  33. node1 = PandaNode('node1')
  34. node2 = PandaNode('node2')
  35. node3 = PandaNode('node3')
  36. node1.set_transform(node1.get_transform().set_pos(LPoint3(0, 0, 1)).set_hpr(LVector3(90, 0, -90)))
  37. node2.set_transform(node2.get_transform().set_pos(LPoint3(0, 1, 0)).set_hpr(LVector3(180, 180, 0)))
  38. node3.set_transform(node3.get_transform().set_pos(LPoint3(1, 0, 0)).set_hpr(LVector3(270, 0, 270)))
  39. # node3 is going to be attached under both node1 and node2 and we will
  40. # hold a path both ways:
  41. node1.add_child(node3)
  42. node2.add_child(node3)
  43. assert len(node1.children) == 1
  44. assert len(node2.children) == 1
  45. assert len(node3.children) == 0
  46. assert len(node1.parents) == 0
  47. assert len(node2.parents) == 0
  48. assert len(node3.parents) == 2
  49. # np1 is the path to node3 via node1:
  50. np1 = NodePath(node1).children[0]
  51. # np2 is the path to node3 via node2:
  52. np2 = NodePath(node2).children[0]
  53. # Both should point to node3:
  54. assert np1.node() == node3
  55. assert np2.node() == node3
  56. # However if we ask for the net transform to node3, it should compose:
  57. assert np1.get_transform(NodePath()) == node1.get_transform().compose(node3.get_transform())
  58. assert np2.get_transform(NodePath()) == node2.get_transform().compose(node3.get_transform())
  59. # If we ask for np1 RELATIVE to np2, it should compose like so:
  60. leg1 = node2.get_transform().compose(node3.get_transform())
  61. leg2 = node1.get_transform().compose(node3.get_transform())
  62. relative_transform = leg1.get_inverse().compose(leg2)
  63. assert np1.get_transform(np2) == relative_transform
  64. def test_weak_nodepath_comparison():
  65. from panda3d.core import NodePath, WeakNodePath
  66. path = NodePath("node")
  67. weak = WeakNodePath(path)
  68. assert path == weak
  69. assert weak == path
  70. assert weak <= path
  71. assert path <= weak
  72. assert weak >= path
  73. assert path >= weak
  74. assert not (path != weak)
  75. assert not (weak != path)
  76. assert not (weak > path)
  77. assert not (path > weak)
  78. assert not (weak < path)
  79. assert not (path < weak)
  80. assert hash(path) == hash(weak)
  81. assert weak.get_node_path() == path
  82. assert weak.node() == path.node()
  83. def test_nodepath_python_tags():
  84. from panda3d.core import NodePath
  85. path = NodePath("node")
  86. with pytest.raises(KeyError):
  87. path.python_tags["foo"]
  88. path.python_tags["foo"] = "bar"
  89. assert path.python_tags["foo"] == "bar"
  90. # Make sure reference count stays the same
  91. rc1 = sys.getrefcount(path.python_tags)
  92. rc2 = sys.getrefcount(path.python_tags)
  93. assert rc1 == rc2