-
Notifications
You must be signed in to change notification settings - Fork 0
/
tposereader.pyp
130 lines (99 loc) · 4.13 KB
/
tposereader.pyp
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
import os
import c4d
import math
# Be sure to use a unique ID obtained from www.plugincafe.com
PLUGIN_ID = 123456789
# IDs
ORIGIN_OBJECT = 2000
TARGET_OBJECT = 2001
SETTINGS_AXIS = 2002
SETTINGS_AXIS_X = 2003
SETTINGS_AXIS_Y = 2004
SETTINGS_AXIS_Z = 2005
RESULT_ROTATION = 3000
def CalculateRotation(x, y):
return 2 * math.atan(y / (x + math.sqrt(math.pow(x, 2) + math.pow(y, 2))))
class PoseReader(c4d.plugins.TagData):
"""Look at Camera"""
def Init(self, node):
"""
Called when Cinema 4D Initialize the TagData (used to define, default values)
:param node: The instance of the TagData.
:type node: c4d.GeListNode
:return: True on success, otherwise False.
"""
data = node.GetDataInstance()
data[SETTINGS_AXIS] = SETTINGS_AXIS_X
self.rotation = 0.0
self.axis = SETTINGS_AXIS_X
c4d.EventAdd()
return True
@classmethod
def CalculateVector(cls, originObject, targetObject, offset):
targetPosition = offset * targetObject.GetMg() * ~originObject.GetMg()
return c4d.Vector(targetPosition - originObject.GetMg().off).GetNormalized()
def Execute(self, tag, doc, op, bt, priority, flags):
"""
Called by Cinema 4D at each Scene Execution, this is the place where calculation should take place.
:param tag: The instance of the TagData.
:type tag: c4d.BaseTag
:param doc: The host document of the tag's object.
:type doc: c4d.documents.BaseDocument
:param op: The host object of the tag.
:type op: c4d.BaseObject
:param bt: The Thread that execute the this TagData.
:type bt: c4d.threading.BaseThread
:param priority: Information about the execution priority of this TagData.
:type priority: EXECUTIONPRIORITY
:param flags: Information about when this TagData is executed.
:type flags: EXECUTIONFLAGS
:return:
"""
data = tag.GetDataInstance()
originObject = data[ORIGIN_OBJECT]
targetObject = data[TARGET_OBJECT]
axis = data[SETTINGS_AXIS]
if originObject and targetObject:
try:
if axis == SETTINGS_AXIS_X:
# calculate rotation along x
targetVectorX = PoseReader.CalculateVector(originObject, targetObject, c4d.Vector(0, 0, 100))
self.rotation = CalculateRotation(targetVectorX.z, targetVectorX.y)
elif axis == SETTINGS_AXIS_Y:
# calculate rotation along y
targetVectorY = PoseReader.CalculateVector(originObject, targetObject, c4d.Vector(100, 0, 0))
self.rotation = CalculateRotation(targetVectorY.x, targetVectorY.z)
elif axis == SETTINGS_AXIS_Z:
# calculate rotation along z
targetVectorZ = PoseReader.CalculateVector(originObject, targetObject, c4d.Vector(0, 100, 0))
self.rotation = math.radians(90.0) - CalculateRotation(targetVectorZ.x, targetVectorZ.y)
except ZeroDivisionError:
pass
data[RESULT_ROTATION] = self.rotation
return c4d.EXECUTIONRESULT_OK
# def SetDParameter(self, node, id, t_data, flags):
# if not node:
# return
# data = node.GetDataInstance()
# print(id[0].id, SETTINGS_AXIS)
# if id[0].id == SETTINGS_AXIS:
# print(data[SETTINGS_AXIS])
# return False
if __name__ == "__main__":
# Retrieves the icon path
directory, _ = os.path.split(__file__)
fn = os.path.join(directory, "res", "tposereader.png")
# Creates a BaseBitmap
bmp = c4d.bitmaps.BaseBitmap()
if bmp is None:
raise MemoryError("Failed to create a BaseBitmap.")
# Init the BaseBitmap with the icon
if bmp.InitWith(fn)[0] != c4d.IMAGERESULT_OK:
raise MemoryError("Failed to initialize the BaseBitmap.")
c4d.plugins.RegisterTagPlugin(id=PLUGIN_ID,
str="PoseReader",
info=c4d.TAG_EXPRESSION | c4d.TAG_VISIBLE,
g=PoseReader,
description="Tposereader",
icon=bmp
)