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

#codesObject (readonly)

Returns the value of attribute codes.



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

def codes
  @codes
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



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

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

  @pointers = @blocks.reject { |block| block.code == 'ENDB' }.map { |block| [block.pointer, block] }.to_h

  dna_block = self.dna_block
  return self unless dna_block

  render_block = @blocks.find { |b| b.code == 'REND' }
  if render_block
    render_block.type_index = dna_block.data.structures.size
    dna_block.data.structures << render_info
  end

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

  @blocks.select(&:type).each_with_index do |block, i|
    block.parse_data(self)
  end

  @codes = @blocks.group_by(&:code).sort.to_h

  self
end

#dna_blockObject



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

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

#render_infoObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/blender-3d/model.rb', line 56

def render_info
  @render_info ||= begin
    sizeof_int = dna_block.data.types.find { |name, _| name == 'int' }.last
    struct_size = 2 * sizeof_int + 64
    StructureDefinition.new('RenderInfo', struct_size, [
      Field.new(SimpleType.new('int'), 'sfra'),
      Field.new(SimpleType.new('int'), 'efra'),
      Field.new(FixedLengthStringType.new(64), 'scene_name'),
    ])
  end
end

#to_xmlObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/blender-3d/model.rb', line 68

def to_xml
  content = blocks.reject { |b| b.code == 'DNA1' }.map(&:to_xml)
  REXML::Element.new(self.class.basename).tap do |e|
    e.add_attribute 'identifier', @header.identifier
    e.add_attribute 'pointer_size', @header.pointer_size.to_s
    e.add_attribute 'endianness', @header.endianness.to_s
    e.add_attribute 'version', @header.version
    content.each_with_index do |c, i|
      c.add_attribute 'index', i.to_s
      e << c
    end
  end
end