~ubuntu-branches/ubuntu/saucy/blender/saucy-proposed

« back to all changes in this revision

Viewing changes to release/scripts/addons/io_scene_ms3d/ms3d_utils.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ##### BEGIN GPL LICENSE BLOCK #####
 
2
#
 
3
#  This program is free software; you can redistribute it and/or
 
4
#  modify it under the terms of the GNU General Public License
 
5
#  as published by the Free Software Foundation; either version 2
 
6
#  of the License, or (at your option) any later version.
 
7
#
 
8
#  This program is distributed in the hope that it will be useful,
 
9
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
#  GNU General Public License for more details.
 
12
#
 
13
#  You should have received a copy of the GNU General Public License
 
14
#  along with this program; if not, write to the Free Software Foundation,
 
15
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
16
#
 
17
# ##### END GPL LICENSE BLOCK #####
 
18
 
 
19
# <pep8 compliant>
 
20
 
 
21
###############################################################################
 
22
#234567890123456789012345678901234567890123456789012345678901234567890123456789
 
23
#--------1---------2---------3---------4---------5---------6---------7---------
 
24
 
 
25
 
 
26
# ##### BEGIN COPYRIGHT BLOCK #####
 
27
#
 
28
# initial script copyright (c)2011-2013 Alexander Nussbaumer
 
29
#
 
30
# ##### END COPYRIGHT BLOCK #####
 
31
 
 
32
 
 
33
#import python stuff
 
34
from os import (
 
35
        path
 
36
        )
 
37
 
 
38
 
 
39
# import io_scene_ms3d stuff
 
40
from io_scene_ms3d.ms3d_strings import (
 
41
        ms3d_str,
 
42
        )
 
43
 
 
44
 
 
45
#import blender stuff
 
46
from bpy import (
 
47
        ops,
 
48
        )
 
49
 
 
50
 
 
51
###############################################################################
 
52
def enable_edit_mode(enable, blender_context):
 
53
    if blender_context.active_object is None \
 
54
            or not blender_context.active_object.type in {'MESH', 'ARMATURE', }:
 
55
        return
 
56
 
 
57
    if enable:
 
58
        modeString = 'EDIT'
 
59
    else:
 
60
        modeString = 'OBJECT'
 
61
 
 
62
    if ops.object.mode_set.poll():
 
63
        ops.object.mode_set(mode=modeString)
 
64
 
 
65
 
 
66
###############################################################################
 
67
def enable_pose_mode(enable, blender_context):
 
68
    if blender_context.active_object is None \
 
69
            or not blender_context.active_object.type in {'ARMATURE', }:
 
70
        return
 
71
 
 
72
    if enable:
 
73
        modeString = 'POSE'
 
74
    else:
 
75
        modeString = 'OBJECT'
 
76
 
 
77
    if ops.object.mode_set.poll():
 
78
        ops.object.mode_set(mode=modeString)
 
79
 
 
80
 
 
81
###############################################################################
 
82
def select_all(select):
 
83
    if select:
 
84
        actionString = 'SELECT'
 
85
    else:
 
86
        actionString = 'DESELECT'
 
87
 
 
88
    if ops.object.select_all.poll():
 
89
        ops.object.select_all(action=actionString)
 
90
 
 
91
    if ops.mesh.select_all.poll():
 
92
        ops.mesh.select_all(action=actionString)
 
93
 
 
94
    if ops.pose.select_all.poll():
 
95
        ops.pose.select_all(action=actionString)
 
96
 
 
97
 
 
98
###############################################################################
 
99
def pre_setup_environment(porter, blender_context):
 
100
    # inject undo to porter
 
101
    # and turn off undo
 
102
    porter.undo = blender_context.user_preferences.edit.use_global_undo
 
103
    blender_context.user_preferences.edit.use_global_undo = False
 
104
 
 
105
    # inject active_object to self
 
106
    porter.active_object = blender_context.scene.objects.active
 
107
 
 
108
    # change to a well defined mode
 
109
    enable_edit_mode(True, blender_context)
 
110
 
 
111
    # enable face-selection-mode
 
112
    blender_context.tool_settings.mesh_select_mode = (False, False, True)
 
113
 
 
114
    # change back to object mode
 
115
    enable_edit_mode(False, blender_context)
 
116
 
 
117
    blender_context.scene.update()
 
118
 
 
119
 
 
120
###############################################################################
 
121
def post_setup_environment(porter, blender_context):
 
122
    # restore active object
 
123
    blender_context.scene.objects.active = porter.active_object
 
124
 
 
125
    if not blender_context.scene.objects.active \
 
126
            and blender_context.selected_objects:
 
127
        blender_context.scene.objects.active \
 
128
                = blender_context.selected_objects[0]
 
129
 
 
130
    # restore pre operator undo state
 
131
    blender_context.user_preferences.edit.use_global_undo = porter.undo
 
132
 
 
133
 
 
134
###############################################################################
 
135
def get_edge_split_modifier_add_if(blender_mesh_object):
 
136
    blender_modifier = blender_mesh_object.modifiers.get(
 
137
            ms3d_str['OBJECT_MODIFIER_SMOOTHING_GROUP'])
 
138
 
 
139
    if blender_modifier is None:
 
140
        blender_modifier = blender_mesh_object.modifiers.new(
 
141
                ms3d_str['OBJECT_MODIFIER_SMOOTHING_GROUP'],
 
142
                type='EDGE_SPLIT')
 
143
        blender_modifier.show_expanded = False
 
144
        blender_modifier.use_edge_angle = False
 
145
        blender_modifier.use_edge_sharp = True
 
146
 
 
147
        blender_mesh_object.data.show_edge_seams = True
 
148
        blender_mesh_object.data.show_edge_sharp = True
 
149
 
 
150
    return blender_modifier
 
151
 
 
152
 
 
153
###########################################################################
 
154
def rotation_matrix(v_track, v_up):
 
155
    ## rotation matrix from two vectors
 
156
    ## http://gamedev.stackexchange.com/questions/20097/how-to-calculate-a-3x3-rotation-matrix-from-2-direction-vectors
 
157
    ## http://www.fastgraph.com/makegames/3drotation/
 
158
    matrix = Matrix().to_3x3()
 
159
 
 
160
    c1 = v_track
 
161
    c1.normalize()
 
162
 
 
163
    c0 = c1.cross(v_up)
 
164
    c0.normalize()
 
165
 
 
166
    c2 = c0.cross(c1)
 
167
    c2.normalize()
 
168
 
 
169
    matrix.col[0] = c0
 
170
    matrix.col[1] = c1
 
171
    matrix.col[2] = c2
 
172
 
 
173
    return matrix
 
174
 
 
175
 
 
176
###############################################################################
 
177
def matrix_difference(mat_src, mat_dst):
 
178
    mat_dst_inv = mat_dst.inverted()
 
179
    return mat_dst_inv * mat_src
 
180
 
 
181
 
 
182
###############################################################################
 
183
def set_sence_to_metric(blender_context):
 
184
    try:
 
185
        # set metrics
 
186
        blender_context.scene.unit_settings.system = 'METRIC'
 
187
        blender_context.scene.unit_settings.system_rotation = 'DEGREES'
 
188
        blender_context.scene.unit_settings.scale_length = 0.001 # 1.0mm
 
189
        blender_context.scene.unit_settings.use_separate = False
 
190
        blender_context.tool_settings.normal_size = 1.0 # 1.0mm
 
191
 
 
192
        # set all 3D views to texture shaded
 
193
        # and set up the clipping
 
194
        for screen in blender_context.blend_data.screens:
 
195
            for area in screen.areas:
 
196
                if (area.type != 'VIEW_3D'):
 
197
                    continue
 
198
 
 
199
                for space in area.spaces:
 
200
                    if (space.type != 'VIEW_3D'):
 
201
                        continue
 
202
 
 
203
                    #space.viewport_shade = 'SOLID'
 
204
                    space.show_textured_solid = True
 
205
                    space.clip_start = 0.1 # 0.1mm
 
206
                    space.clip_end = 1000000.0 # 1km
 
207
            #screen.scene.game_settings.material_mode = 'MULTITEXTURE'
 
208
 
 
209
    except Exception:
 
210
        raise
 
211
 
 
212
    else:
 
213
        pass
 
214
 
 
215
 
 
216
###############################################################################
 
217
 
 
218
###############################################################################
 
219
#234567890123456789012345678901234567890123456789012345678901234567890123456789
 
220
#--------1---------2---------3---------4---------5---------6---------7---------
 
221
# ##### END OF FILE #####