Welcome to mirror list, hosted at ThFree Co, Russian Federation.

envelope_symmetry.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a4f7a31b3b28af19c6ae409d214985b3d5be7d2 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!BPY

"""
Name: 'Envelope Symmetry'
Blender: 234
Group: 'Animation'
Tooltip: 'Make envelope symetrical'
"""

__author__ = "Jonas Petersen"
__url__ = ("blender", "elysiun", "Script's homepage, http://www.mindfloaters.de/blender/", "thread at blender.org, http://www.blender.org/modules.php?op=modload&name=phpBB2&file=viewtopic&t=4858 ")
__version__ = "0.9 2004-11-10"
__doc__ = """\
This script creates perfectly symmetrical envelope sets.  It is part of the
envelop assignment tools.

"Envelopes" are Mesh objects with names following this naming convention:

<bone name>:<float value>

Please check the script's homepage and the thread at blender.org (last link button above) for more info.

For this version users need to edit the script code to change default options.
"""

# --------------------------------------------------------------------------
# "Envelope Symmetry" by Jonas Petersen
# Version 0.9 - 10th November 2004 - first public release
# --------------------------------------------------------------------------
#
# A script for creating perfectly symmetrical envelope sets. It is
# part of the envelope assignment tool.
#
# It is available in Object Mode via the menu item:
#
#   Object -> Scripts -> Envelope Symmetry
#
# With default settings it will:
#
# - Look for bones
#
# Find the latest version at: http://www.mindfloaters.de/blender/
#
# --------------------------------------------------------------------------
# $Id$
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# Copyright (C) 2004: Jonas Petersen, jonas at mindfloaters dot de
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****

# --------------------------------------------------------------------------
# CONFIGURATION
# --------------------------------------------------------------------------

# Note: Theses values will later be editable via a gui interface
# within Blender.

# The suffix for the reference and opposite envelope.
# The configuration of of the opposite envelope will be overwritten by
# the configuration of the reference envelope (shape, position, bone, weight).
# The default is REF_SUFFIX = '.L' and OPP_SUFFIX = '.R'.
REF_SUFFIX = '.R'
OPP_SUFFIX = '.L'

# MIRROR_AXIS defines the axis in which bones are mirrored/aligned.
# Values:
#   0 for X (default)
#   1 for Y
#   2 for Z
MIRROR_AXIS = 0

# SEPARATOR is the character used to delimit the bone name and the weight
# in the envelope name.
SEPARATOR = ":"

# --------------------------------------------------------------------------
# END OF CONFIGURATION
# --------------------------------------------------------------------------

import Blender, math, sys
from Blender import Mathutils
from BPyNMesh import *

def flipFace(v):
	if len(v) == 3: v[0], v[1], v[2] = v[2], v[1], v[0]
	elif len(v) == 4: v[0], v[1], v[2], v[3] = v[3], v[2], v[1], v[0]

# return object with given object name (with variable parts) and mesh name
def getObjectByName(obj_name, mesh_name):
	objs = Blender.Object.Get()
	for obj in objs:
		if obj.getType() == "Mesh":
#			if obj.getName()[0:len(obj_name)] == obj_name and obj.getData().name == mesh_name:
			# use only mesh_name so bone name and weight (in the envelope name)
			# can be changed by the user and mirrored by the script.
			if obj.getData().name == mesh_name:
				return obj
	return False

SUFFIX_LEN = len(REF_SUFFIX);

Blender.Window.EditMode(0)

count = 0
objs = Blender.Object.Get()
for obj in objs:
	if obj.getType() != 'Mesh':
		continue

	count += 1
	name = obj.getName()
	pos = name.find(SEPARATOR)
	if (pos > -1):
		ApplySizeAndRotation(obj)

		base_name = name[0:pos-SUFFIX_LEN]
		suffix = name[pos-SUFFIX_LEN:pos]
		weight = name[pos:len(name)] # the SEPARATOR following a float value

		if suffix == REF_SUFFIX:
			mesh = obj.getData()
			mirror_name = base_name +  OPP_SUFFIX + weight
			mirror_mesh_name = mesh.name + ".mirror"

			mirror_obj = getObjectByName(base_name + OPP_SUFFIX, mirror_mesh_name)

			if mirror_obj:

				# update vertices

				mirror_mesh = mirror_obj.getData()
				for i in range(len(mesh.verts)):
					org = mesh.verts[i]
					mir = mirror_mesh.verts[i]
					mir.co[0], mir.co[1], mir.co[2] = org.co[0], org.co[1], org.co[2]
					mir.co[MIRROR_AXIS] *= -1

				mirror_mesh.update()
			else:

				# create mirror object

				mirror_mesh = Blender.NMesh.GetRaw(obj.getData().name)
				for face in mirror_mesh.faces:
					flipFace(face.v)
				for vert in mirror_mesh.verts:
					vert.co[MIRROR_AXIS] *= -1

				mirror_obj = Blender.NMesh.PutRaw(mirror_mesh, mirror_mesh_name)

			# update name, drawType and location
			
			mirror_obj.setName(mirror_name)
			mirror_obj.drawType = obj.drawType

			loc = [obj.LocX, obj.LocY, obj.LocZ]
			loc[MIRROR_AXIS] *= -1
			mirror_obj.setLocation(loc)

Blender.Window.EditMode(0)