test_nodepath.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import pytest, sys
  2. def test_nodepath_empty():
  3. """Tests NodePath behavior for empty NodePaths."""
  4. from panda3d.core import NodePath, ParamNodePath
  5. import pickle
  6. empty = NodePath()
  7. assert empty.is_empty()
  8. assert not empty
  9. # Try pickling, which uses __reduce__
  10. dumped = pickle.dumps(empty)
  11. empty2 = pickle.loads(dumped)
  12. assert empty2.is_empty()
  13. assert not empty2
  14. assert empty == empty2
  15. # Test write_datagram/fillin, which are invoked when the NodePath is being
  16. # serialized indirectly, such as via ParamNodePath
  17. dumped = pickle.dumps(ParamNodePath(empty))
  18. empty2 = pickle.loads(dumped).get_value()
  19. assert empty2.is_empty()
  20. assert not empty2
  21. assert empty == empty2
  22. def test_nodepath_single():
  23. """Tests NodePath behavior for single-node NodePaths."""
  24. from panda3d.core import NodePath
  25. np = NodePath('np')
  26. assert not np.is_empty()
  27. assert np
  28. assert np.get_pos() == (0, 0, 0)
  29. assert np.get_hpr() == (0, 0, 0)
  30. assert np.get_scale() == (1, 1, 1)
  31. def test_nodepath_parent():
  32. """Tests NodePath.reparentTo()."""
  33. from panda3d.core import NodePath
  34. np1 = NodePath('np')
  35. np2 = NodePath('np')
  36. assert np1.parent is None
  37. assert np2.parent is None
  38. np1.reparentTo(np2)
  39. assert np1.parent == np2
  40. assert np2.parent is None
  41. def test_nodepath_transform_changes():
  42. """Tests that NodePath applies transform changes to its managed node."""
  43. from panda3d.core import NodePath
  44. np = NodePath('np')
  45. assert np.get_pos() == (0, 0, 0)
  46. assert np.get_hpr() == (0, 0, 0)
  47. assert np.get_scale() == (1, 1, 1)
  48. np.set_pos(1, 2, 3)
  49. assert np.get_pos() == (1, 2, 3)
  50. assert np.node().get_transform().get_pos() == (1, 2, 3)
  51. def test_nodepath_transform_composition():
  52. """Tests that NodePath composes transform states according to the path it holds."""
  53. from panda3d.core import PandaNode, NodePath, LPoint3, LVector3
  54. # Create 3 PandaNodes, and give each some interesting transform state:
  55. node1 = PandaNode('node1')
  56. node2 = PandaNode('node2')
  57. node3 = PandaNode('node3')
  58. node1.set_transform(node1.get_transform().set_pos(LPoint3(0, 0, 1)).set_hpr(LVector3(90, 0, -90)))
  59. node2.set_transform(node2.get_transform().set_pos(LPoint3(0, 1, 0)).set_hpr(LVector3(180, 180, 0)))
  60. node3.set_transform(node3.get_transform().set_pos(LPoint3(1, 0, 0)).set_hpr(LVector3(270, 0, 270)))
  61. # node3 is going to be attached under both node1 and node2 and we will
  62. # hold a path both ways:
  63. node1.add_child(node3)
  64. node2.add_child(node3)
  65. assert len(node1.children) == 1
  66. assert len(node2.children) == 1
  67. assert len(node3.children) == 0
  68. assert len(node1.parents) == 0
  69. assert len(node2.parents) == 0
  70. assert len(node3.parents) == 2
  71. # np1 is the path to node3 via node1:
  72. np1 = NodePath(node1).children[0]
  73. # np2 is the path to node3 via node2:
  74. np2 = NodePath(node2).children[0]
  75. # Both should point to node3:
  76. assert np1.node() == node3
  77. assert np2.node() == node3
  78. # However if we ask for the net transform to node3, it should compose:
  79. assert np1.get_transform(NodePath()) == node1.get_transform().compose(node3.get_transform())
  80. assert np2.get_transform(NodePath()) == node2.get_transform().compose(node3.get_transform())
  81. # If we ask for np1 RELATIVE to np2, it should compose like so:
  82. leg1 = node2.get_transform().compose(node3.get_transform())
  83. leg2 = node1.get_transform().compose(node3.get_transform())
  84. relative_transform = leg1.get_inverse().compose(leg2)
  85. assert np1.get_transform(np2) == relative_transform
  86. def test_weak_nodepath_comparison():
  87. from panda3d.core import NodePath, WeakNodePath
  88. path = NodePath("node")
  89. weak = WeakNodePath(path)
  90. assert path == weak
  91. assert weak == path
  92. assert weak <= path
  93. assert path <= weak
  94. assert weak >= path
  95. assert path >= weak
  96. assert not (path != weak)
  97. assert not (weak != path)
  98. assert not (weak > path)
  99. assert not (path > weak)
  100. assert not (weak < path)
  101. assert not (path < weak)
  102. assert hash(path) == hash(weak)
  103. assert weak.get_node_path() == path
  104. assert weak.node() == path.node()
  105. def test_nodepath_python_tags():
  106. from panda3d.core import NodePath
  107. path = NodePath("node")
  108. with pytest.raises(KeyError):
  109. path.python_tags["foo"]
  110. path.python_tags["foo"] = "bar"
  111. assert path.python_tags["foo"] == "bar"
  112. # Make sure reference count stays the same
  113. rc1 = sys.getrefcount(path.python_tags)
  114. rc2 = sys.getrefcount(path.python_tags)
  115. assert rc1 == rc2
  116. def test_nodepath_replace_texture():
  117. from panda3d.core import NodePath, Texture
  118. tex1 = Texture()
  119. tex2 = Texture()
  120. path1 = NodePath("node1")
  121. path1.set_texture(tex1)
  122. path1.replace_texture(tex1, tex2)
  123. assert path1.get_texture() == tex2
  124. path1 = NodePath("node1")
  125. path2 = path1.attach_new_node("node2")
  126. path2.set_texture(tex1)
  127. path1.replace_texture(tex1, tex2)
  128. assert not path1.has_texture()
  129. assert path2.get_texture() == tex2