object.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. @property
  13. def data(self):
  14. """
  15. :return: returns the data block of the node
  16. """
  17. return api.data(self.node)
  18. def _init_camera(self):
  19. """Initialize camera attributes"""
  20. logger.debug("Object()._init_camera()")
  21. self[constants.FAR] = api.camera.far(self.data)
  22. self[constants.NEAR] = api.camera.near(self.data)
  23. if self[constants.TYPE] == constants.PERSPECTIVE_CAMERA:
  24. self[constants.ASPECT] = api.camera.aspect(self.data)
  25. self[constants.FOV] = api.camera.fov(self.data)
  26. elif self[constants.TYPE] == constants.ORTHOGRAPHIC_CAMERA:
  27. self[constants.LEFT] = api.camera.left(self.data)
  28. self[constants.RIGHT] = api.camera.right(self.data)
  29. self[constants.TOP] = api.camera.top(self.data)
  30. self[constants.BOTTOM] = api.camera.bottom(self.data)
  31. #@TODO: need more light attributes. Some may have to come from
  32. # custom blender attributes.
  33. def _init_light(self):
  34. """Initialize light attributes"""
  35. logger.debug("Object()._init_light()")
  36. self[constants.COLOR] = api.light.color(self.data)
  37. self[constants.INTENSITY] = api.light.intensity(self.data)
  38. # Commented out because Blender's distance is not a cutoff value.
  39. #if self[constants.TYPE] != constants.DIRECTIONAL_LIGHT:
  40. # self[constants.DISTANCE] = api.light.distance(self.data)
  41. self[constants.DISTANCE] = 0;
  42. lightType = self[constants.TYPE]
  43. if lightType == constants.SPOT_LIGHT:
  44. self[constants.ANGLE] = api.light.angle(self.data)
  45. self[constants.DECAY] = api.light.falloff(self.data)
  46. elif lightType == constants.POINT_LIGHT:
  47. self[constants.DECAY] = api.light.falloff(self.data)
  48. def _init_mesh(self):
  49. """Initialize mesh attributes"""
  50. logger.debug("Object()._init_mesh()")
  51. mesh = api.object.mesh(self.node, self.options)
  52. node = self.scene.geometry(mesh)
  53. if node:
  54. self[constants.GEOMETRY] = node[constants.UUID]
  55. else:
  56. msg = "Could not find Geometry() node for %s"
  57. logger.error(msg, self.node)
  58. def _node_setup(self):
  59. """Parse common node attributes of all objects"""
  60. logger.debug("Object()._node_setup()")
  61. self[constants.NAME] = api.object.name(self.node)
  62. transform = api.object.matrix(self.node, self.options)
  63. matrix = []
  64. for col in range(0, 4):
  65. for row in range(0, 4):
  66. matrix.append(transform[row][col])
  67. self[constants.MATRIX] = matrix
  68. self[constants.VISIBLE] = api.object.visible(self.node)
  69. self[constants.TYPE] = api.object.node_type(self.node)
  70. if self.options.get(constants.MATERIALS):
  71. logger.info("Parsing materials for %s", self.node)
  72. material_name = api.object.material(self.node)
  73. if material_name:
  74. logger.info("Material found %s", material_name)
  75. material_inst = self.scene.material(material_name)
  76. self[constants.MATERIAL] = material_inst[constants.UUID]
  77. else:
  78. logger.info("%s has no materials", self.node)
  79. casts_shadow = (constants.MESH,
  80. constants.DIRECTIONAL_LIGHT,
  81. constants.SPOT_LIGHT)
  82. if self[constants.TYPE] in casts_shadow:
  83. logger.info("Querying shadow casting for %s", self.node)
  84. self[constants.CAST_SHADOW] = \
  85. api.object.cast_shadow(self.node)
  86. if self[constants.TYPE] == constants.MESH:
  87. logger.info("Querying shadow receive for %s", self.node)
  88. self[constants.RECEIVE_SHADOW] = \
  89. api.object.receive_shadow(self.node)
  90. camera = (constants.PERSPECTIVE_CAMERA,
  91. constants.ORTHOGRAPHIC_CAMERA)
  92. lights = (constants.AMBIENT_LIGHT,
  93. constants.DIRECTIONAL_LIGHT,
  94. constants.POINT_LIGHT,
  95. constants.SPOT_LIGHT, constants.HEMISPHERE_LIGHT)
  96. if self[constants.TYPE] == constants.MESH:
  97. self._init_mesh()
  98. elif self[constants.TYPE] in camera:
  99. self._init_camera()
  100. elif self[constants.TYPE] in lights:
  101. self._init_light()
  102. no_anim = (None, False, constants.OFF)
  103. if self.options.get(constants.KEYFRAMES) not in no_anim:
  104. logger.info("Export Transform Animation for %s", self.node)
  105. if self._scene:
  106. # only when exporting scene
  107. tracks = api.object.animated_xform(self.node, self.options)
  108. merge = self._scene[constants.ANIMATION][0][constants.KEYFRAMES]
  109. for track in tracks:
  110. merge.append(track)
  111. if self.options.get(constants.HIERARCHY, False):
  112. for child in api.object.children(self.node, self.scene.valid_types):
  113. if not self.get(constants.CHILDREN):
  114. self[constants.CHILDREN] = [Object(child, parent=self)]
  115. else:
  116. self[constants.CHILDREN].append(Object(child, parent=self))
  117. if self.options.get(constants.CUSTOM_PROPERTIES, False):
  118. self[constants.USER_DATA] = api.object.custom_properties(self.node)
  119. def _root_setup(self):
  120. """Applies to a root/scene object"""
  121. logger.debug("Object()._root_setup()")
  122. self[constants.MATRIX] = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
  123. 1, 0, 0, 0, 0, 1]