object.py 4.5 KB

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