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



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



52
53
54
# File 'lib/blender-3d/model.rb', line 52

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
37
38
39
40
41
42
43
44
45
46
# 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 do |block|
    if block.data.is_a?(Array)
      pointer = block.pointer.location
      block.data.each do |data|
        @pointers[Pointer.new(pointer)] = data
        pointer += data.class.definition.size
      end
    else
      @pointers[block.pointer] = block.data
    end
  end

  self
end

#dna_blockObject



48
49
50
# File 'lib/blender-3d/model.rb', line 48

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