object.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from .. import constants, logger
  2. from . import base_classes, api
  3. class Object(base_classes.BaseNode):
  4. """Class that wraps an object node"""
  5. def __init__(self, node, parent=None, type=None):
  6. logger.debug("Object().__init__(%s)", node)
  7. base_classes.BaseNode.__init__(self, node, parent=parent, type=type)
  8. if self.node:
  9. self._node_setup()
  10. else:
  11. self._root_setup()
  12. def _init_camera(self):
  13. """Initialize camera attributes"""
  14. logger.debug("Object()._init_camera()")
  15. self[constants.FAR] = api.camera.far(self.node)
  16. self[constants.NEAR] = api.camera.near(self.node)
  17. if self[constants.TYPE] == constants.PERSPECTIVE_CAMERA:
  18. self[constants.ASPECT] = api.camera.aspect(self.node)
  19. self[constants.FOV] = api.camera.fov(self.node)
  20. elif self[constants.TYPE] == constants.ORTHOGRAPHIC_CAMERA:
  21. self[constants.LEFT] = api.camera.left(self.node)
  22. self[constants.RIGHT] = api.camera.right(self.node)
  23. self[constants.TOP] = api.camera.top(self.node)
  24. self[constants.BOTTOM] = api.camera.bottom(self.node)
  25. #@TODO: need more light attributes. Some may have to come from
  26. # custom blender attributes.
  27. def _init_light(self):
  28. """Initialize light attributes"""
  29. logger.debug("Object()._init_light()")
  30. self[constants.COLOR] = api.light.color(self.node)
  31. self[constants.INTENSITY] = api.light.intensity(self.node)
  32. if self[constants.TYPE] != constants.DIRECTIONAL_LIGHT:
  33. self[constants.DISTANCE] = api.light.distance(self.node)
  34. if self[constants.TYPE] == constants.SPOT_LIGHT:
  35. self[constants.ANGLE] = api.light.angle(self.node)
  36. def _init_mesh(self):
  37. """Initialize mesh attributes"""
  38. logger.debug("Object()._init_mesh()")
  39. mesh = api.object.mesh(self.node, self.options)
  40. node = self.scene.geometry(mesh)
  41. if node:
  42. self[constants.GEOMETRY] = node[constants.UUID]
  43. else:
  44. msg = "Could not find Geometry() node for %s"
  45. logger.error(msg, self.node)
  46. def _node_setup(self):
  47. """Parse common node attributes of all objects"""
  48. logger.debug("Object()._node_setup()")
  49. self[constants.NAME] = api.object.name(self.node)
  50. self[constants.POSITION] = api.object.position(
  51. self.node, self.options)
  52. self[constants.ROTATION] = api.object.rotation(
  53. self.node, self.options)
  54. self[constants.SCALE] = api.object.scale(
  55. self.node, self.options)
  56. self[constants.VISIBLE] = api.object.visible(self.node)
  57. self[constants.TYPE] = api.object.node_type(self.node)
  58. if self.options.get(constants.MATERIALS):
  59. logger.info("Parsing materials for %s", self.node)
  60. material_name = api.object.material(self.node)
  61. if material_name:
  62. logger.info("Material found %s", material_name)
  63. material_inst = self.scene.material(material_name)
  64. self[constants.MATERIAL] = material_inst[constants.UUID]
  65. else:
  66. logger.info("%s has no materials", self.node)
  67. casts_shadow = (constants.MESH,
  68. constants.DIRECTIONAL_LIGHT,
  69. constants.SPOT_LIGHT)
  70. if self[constants.TYPE] in casts_shadow:
  71. logger.info("Querying shadow casting for %s", self.node)
  72. self[constants.CAST_SHADOW] = \
  73. api.object.cast_shadow(self.node)
  74. if self[constants.TYPE] == constants.MESH:
  75. logger.info("Querying shadow receive for %s", self.node)
  76. self[constants.RECEIVE_SHADOW] = \
  77. api.object.receive_shadow(self.node)
  78. camera = (constants.PERSPECTIVE_CAMERA,
  79. constants.ORTHOGRAPHIC_CAMERA)
  80. lights = (constants.AMBIENT_LIGHT,
  81. constants.DIRECTIONAL_LIGHT,
  82. constants.AREA_LIGHT, constants.POINT_LIGHT,
  83. constants.SPOT_LIGHT, constants.HEMISPHERE_LIGHT)
  84. if self[constants.TYPE] == constants.MESH:
  85. self._init_mesh()
  86. elif self[constants.TYPE] in camera:
  87. self._init_camera()
  88. elif self[constants.TYPE] in lights:
  89. self._init_light()
  90. #for child in api.object.children(self.node, self.scene.valid_types):
  91. # if not self.get(constants.CHILDREN):
  92. # self[constants.CHILDREN] = [Object(child, parent=self)]
  93. # else:
  94. # self[constants.CHILDREN].append(Object(child, parent=self))
  95. def _root_setup(self):
  96. """Applies to a root/scene object"""
  97. logger.debug("Object()._root_setup()")
  98. self[constants.MATRIX] = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
  99. 1, 0, 0, 0, 0, 1]