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

scripttemplate_pyconstraint.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68aa9194435e5dbb80846a890f881ec071185465 (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
#!BPY
"""
Name: 'Script Constraint'
Blender: 245
Group: 'ScriptTemplate'
Tooltip: 'Add a new script for custom constraints'
"""

from Blender import Window
import bpy

script_data = \
"""#BPYCONSTRAINT
'''
	PyConstraint template, access this in the "add constraint" scripts submenu.
	Add docstring here
'''

import Blender
from Blender import Draw
from Blender import Mathutils
import math

'''
 This variable specifies the number of targets 
 that this constraint can use
'''
NUM_TARGETS = 1


'''
 This function is called to evaluate the constraint
	obmatrix:		(Matrix) copy of owner's 'ownerspace' matrix
	targetmatrices:	(List) list of copies of the 'targetspace' matrices of the targets (where applicable)
	idprop:			(IDProperties) wrapped data referring to this 
					constraint instance's idproperties
'''
def doConstraint(obmatrix, targetmatrices, idprop):
	# Separate out the tranformation components for easy access.
	obloc = obmatrix.translationPart()	# Translation
	obrot = obmatrix.toEuler()			# Rotation
	obsca = obmatrix.scalePart()		# Scale

	# Define user-settable parameters.\
	# Must also be defined in getSettings().
	if not idprop.has_key('user_toggle'): idprop['user_toggle'] = 1
	if not idprop.has_key('user_slider'): idprop['user_slider'] = 1.0
	
	
	# Do stuff here, changing obloc, obrot, and obsca.

	
	# Convert back into a matrix for loc, scale, rotation,
	mtxloc = Mathutils.TranslationMatrix( obloc )
	mtxrot = obrot.toMatrix().resize4x4()
	mtxsca = Mathutils.Matrix([obsca[0],0,0,0], [0,obsca[1],0,0], [0,0,obsca[2],0], [0,0,0,1])
	
	# Recombine the separate elements into a transform matrix.
	outputmatrix = mtxsca * mtxrot * mtxloc

	# Return the new matrix.
	return outputmatrix



'''
 This function manipulates the matrix of a target prior to sending it to doConstraint()
	target_object:					wrapped data, representing the target object
	subtarget_bone:					wrapped data, representing the subtarget pose-bone/vertex-group (where applicable)
	target_matrix:					(Matrix) the transformation matrix of the target
	id_properties_of_constraint:	(IDProperties) wrapped idproperties
'''
def doTarget(target_object, subtarget_bone, target_matrix, id_properties_of_constraint):
	return target_matrix


'''
 This function draws a pupblock that lets the user set
 the values of custom settings the constraint defines.
 This function is called when the user presses the settings button.
	idprop:	(IDProperties) wrapped data referring to this 
			constraint instance's idproperties
'''
def getSettings(idprop):
	# Define user-settable parameters.
	# Must also be defined in getSettings().
	if not idprop.has_key('user_toggle'): idprop['user_toggle'] = 1
	if not idprop.has_key('user_slider'): idprop['user_slider'] = 1.0
	
	# create temporary vars for interface 
	utoggle = Draw.Create(idprop['user_toggle'])
	uslider = Draw.Create(idprop['user_slider'])
	

	# define and draw pupblock
	block = []
	block.append("Buttons: ")
	block.append(("Toggle", utoggle, "This is a toggle button."))
	block.append("More buttons: ")
	block.append(("Slider", uslider, 0.0000001, 1000.0, "This is a number field."))

	retval = Draw.PupBlock("Constraint Template", block)
	
	# update id-property values after user changes settings
	if (retval):
		idprop['user_toggle']= utoggle.val
		idprop['user_slider']= uslider.val

"""

new_text = bpy.data.texts.new('pyconstraint_template.py')
new_text.write(script_data)
bpy.data.texts.active = new_text
Window.RedrawAll()