File: preset.py

package info (click to toggle)
openstructure 2.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 205,228 kB
  • sloc: cpp: 188,129; python: 35,361; ansic: 34,298; fortran: 3,275; sh: 286; xml: 146; makefile: 29
file content (120 lines) | stat: -rw-r--r-- 3,943 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#------------------------------------------------------------------------------
# This file is part of the OpenStructure project <www.openstructure.org>
#
# Copyright (C) 2008-2020 by the OpenStructure authors
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3.0 of the License, or (at your option)
# any later version.
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#------------------------------------------------------------------------------
# -*- coding: utf-8 -*-

from ost import info
from ost import gfx

from ost.gfx import ColorOp

#Rendering Preset
class Preset:
  NAME_ATTRIBUTE_NAME = "Name"
  OP_GROUP_NAME = "Op"
  CLASS_NAME_ATTRIBUTE_NAME = "ClassName"
  INDEX_ATTRIBUTE_NAME = "Index"
  
  MODULE_NAMES = ["ost.gfx","ost.gui.scene.visibility_op","ost.gui.scene.render_op"]
  
  def __init__(self, name, parent=None):    
    self.name_ = name
    self.ops_ = list()
    
  def SetName(self, name):
    self.name_ = name
    
  def GetName(self):
    return self.name_
    
  def InsertOp(self, index, op):
    self.ops_.insert(index, op)
    
  def RemoveOp(self, op):
    self.ops_.remove(op)
  
  def RemoveOpAt(self, index):
    del(self.ops_[index])
    
  def GetOp(self, index):
    return self.ops_[index]
  
  def GetOpCount(self):
    return len(self.ops_)
  
  def SetOp(self, index, op):
    self.ops_[index] = op
  
  def AddOp(self, op):
    self.ops_.append(op)
  
  def GetOps(self):
    return self.ops_
  
  def ApplyOn(self, entity):
    if (entity is not None) and isinstance(entity, gfx.Entity):
      for op in self.ops_:
        if isinstance(op,ColorOp):
          entity.Apply(op)
        else:
          op.ApplyOn(entity)
  
  def ToInfo(self,group):
    group.SetAttribute(Preset.NAME_ATTRIBUTE_NAME, self.name_)
    for i in range(0,len(self.ops_)):
      op_group = group.CreateGroup(Preset.OP_GROUP_NAME)
      op_group.SetAttribute(Preset.INDEX_ATTRIBUTE_NAME, str(i))
      op_group.SetAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME, "%s"%(self.ops_[i].__class__.__name__))
      self.ops_[i].ToInfo(op_group)
    
  @staticmethod
  def FromInfo(group):
    preset = None
    if group.HasAttribute(Preset.NAME_ATTRIBUTE_NAME):
      name = group.GetAttribute(Preset.NAME_ATTRIBUTE_NAME)
      preset = Preset(name)
      group_list = group.GetGroups(Preset.OP_GROUP_NAME)
      
      class_order_dict = dict()
      for op_group in group_list:
        if(op_group.HasAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME) and op_group.HasAttribute(Preset.INDEX_ATTRIBUTE_NAME)):
          class_name = op_group.GetAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME)
          index = int(op_group.GetAttribute(Preset.INDEX_ATTRIBUTE_NAME))
          op_class = None
          for module in Preset.MODULE_NAMES:
            try:
              op_class = Preset.__get_op_class("%s.%s"%(module,class_name))
              break
            except AttributeError:
              pass
          if op_class is not None:
            op = op_class.FromInfo(op_group)
            class_order_dict[index]=op
    for i in range(0, len(class_order_dict)):
      if(i in class_order_dict):
        preset.AddOp(class_order_dict[i])
    return preset
    
  @staticmethod
  def __get_op_class( cls ):
    parts = cls.split('.')
    module = ".".join(parts[:-1])
    m = __import__( module )
    for comp in parts[1:]:
      m = getattr(m, comp)
    return m