Class: Blender3d::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/blender-3d/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Model

Returns a new instance of Model.



9
10
11
12
# File 'lib/blender-3d/model.rb', line 9

def initialize(file = nil)
  @blocks = []
  deserialize(file) if file
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



3
4
5
# File 'lib/blender-3d/model.rb', line 3

def blocks
  @blocks
end

#headerObject (readonly)

Returns the value of attribute header.



3
4
5
# File 'lib/blender-3d/model.rb', line 3

def header
  @header
end

#pointersObject (readonly)

Returns the value of attribute pointers.



3
4
5
# File 'lib/blender-3d/model.rb', line 3

def pointers
  @pointers
end

Class Method Details

.from_file(path) ⇒ Object



5
6
7
# File 'lib/blender-3d/model.rb', line 5

def self.from_file(path)
  File.open(path, 'rb') { |file| new(file) }
end

Instance Method Details

#create_reader(file) ⇒ Object



42
43
44
# File 'lib/blender-3d/model.rb', line 42

def create_reader(file)
  ObjectReader.new(file, self)
end

#deserialize(file) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/blender-3d/model.rb', line 14

def deserialize(file)
  @header = FileHeader.new(file)
  reader = create_reader(file)
  @blocks.clear

  loop do
    @blocks << FileBlock.new(reader)
    break if reader.tell == reader.file.size
  end

  dna_block = self.dna_block
  return self unless dna_block

  @blocks.select { |block| block.type_index != 0 }.each do |block|
    block.type = dna_block.data.structures[block.type_index]
    block.parse_data(self)
  end

  @pointers = {}
  @blocks.each { |block| @pointers[block.pointer] = block.data }

  self
end

#dna_blockObject



38
39
40
# File 'lib/blender-3d/model.rb', line 38

def dna_block
  @blocks.find { |block| block.code == 'DNA1' }
end