-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAddTargetObjects.py
More file actions
executable file
·265 lines (200 loc) · 12 KB
/
Copy pathAddTargetObjects.py
File metadata and controls
executable file
·265 lines (200 loc) · 12 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#=================== EditTargetObjects.py =======================
# These functions are used to add or remove target objects to or
# from the scene.
#
import bpy
import math
import os
import numpy as np
import mathutils as mu
import socket
from InitBlendScene import InitBlendScene
from GetOSpath import GetOSpath
InitBlendScene(2,1) # Initialize scene geomerty
Prefix = GetOSpath() # Get OS-specific path
RefObjFile = Prefix[0] + 'murphya/MacaqueFace3D/GameRenders/Golf_Ball.obj' # Full path of mesh to use as reference objects
TargetType = 2
SphereRad = 0.01 # Set sphere radius (meters)
Scale = (SphereRad,SphereRad,SphereRad)
def AddTargetObjects():
#================= Set target appearance
AddTex = 0
ViewingDist = 0.97
SphereDepth = -0.3
MetresPerDeg = math.tan(math.pi/180)*(ViewingDist+SphereDepth)
SphereEcc = 10*MetresPerDeg # Set target eccentricity (degrees visual angle -> meters)
SphereNumber = 24
SpherePolAng = np.linspace(0,2*math.pi, SphereNumber+1)
SphereLocs = np.zeros((SphereNumber,3))
for sph in range(0,SphereNumber):
SphereLocs[sph][0] = SphereEcc*math.sin(SpherePolAng[sph])
SphereLocs[sph][2] = SphereEcc*math.cos(SpherePolAng[sph])
SphereLocs[sph][1] = SphereDepth
SphereColor = (0,0,1) # Set target colors (RGB)
#================= Create material
AllMats = [mat for mat in bpy.data.materials if mat.name.startswith("Target")]
if all(AllMats):
mat = bpy.data.materials.new("TargetMat") # Create new material
mat.diffuse_color = SphereColor # Set material color
mat.specular_intensity = 0 # Set intensity of specular reflection (0-1)
elif not(all(AllMats)):
mat = AllMats[0]
#============ Add texture to target surfaces
if AddTex == 1:
heightTex = bpy.data.textures.new('TargetTex', type = 'CLOUDS')
heightTex.noise_scale = 0.002
#================= Create targets
bpy.ops.group.create(name="Targets")
#bpy.ops.object.group_link(group="Targets")
for sph in range(0, len(SphereLocs)):
if sph == 0:
if TargetType == 1:
bpy.ops.mesh.primitive_ico_sphere_add(
subdivisions = 100,
size = SphereRad,
calc_uvs = False,
view_align = False,
enter_editmode = False,
location = SphereLocs[sph],
rotation = (0,0,0))
bpy.data.objects['Icosphere'].active_material = mat # Set target color
bpy.data.objects['Icosphere'].name = "Target %02d" % sph # Rename object as target ID
if AddTex == 1:
dispMod = bpy.context.scene.objects["Target 0"].modifiers.new("Displace", type='DISPLACE')
dispMod.texture = heightTex
dispMod.strength = 0.001
elif TargetType == 2:
Import = bpy.ops.import_scene.obj(filepath=RefObjFile) # Import target geometry from '.obj' file
RefObj = bpy.context.selected_objects[0] # Get RefObj object handle
RefObj.location = SphereLocs[sph]
RefObj.name = "Target %02d" % sph
RefObj.scale = Scale # Set RefObj size
elif sph > 0:
d = bpy.data.objects['Target 00'].copy()
bpy.context.scene.objects.link(d)
d.location = SphereLocs[sph]
d.name = "Target %02d" % sph
#RefObj.rotation_euler = Orientation[n] # Set RefObj orientation
#RefObj.active_material = RefObjMat
#RefObj.material_slots[0].link = 'OBJECT'
#RefObj.material_slots[0].material = RefObjMat
bpy.context.scene.objects["Target %d" % sph].select = True
bpy.ops.object.group_link(group="Targets")
bpy.context.scene.objects["Target %d" % sph].select = False
#bpy.context.scene.objects.active = d
#bpy.ops.group.objects_add_active(group = "Targets")
return SphereLocs
def RemoveTargetObjects():
scene = bpy.context.scene
AllTargets = [obj for obj in scene.objects if obj.name.startswith("Target")] # Find all objects in scene with 'Target' in name
for t in range(0,len(AllTargets)):
AllTargets[t].select = True # Select next target object
bpy.ops.object.delete() # Delete target object
def EditTargetObjects(Add):
if Add == 1:
Locs = AddTargetObjects()
elif Add == 0:
RemoveTargetObjects()
def MonkeyLookAt(HeadLoc, GazeLoc):
#bpy.data.objects["HeadTracker"].location + context.active_pose_bone.tail
#HeadLoc = mu.Vector(HeadLoc) - bpy.data.objects["Root"].location # Convert world coordinates to head-centered coordinates
#HeadLoc = mu.Vector(HeadLoc[0],HeadLoc[2],HeadLoc[1])
RootOffset = bpy.data.objects['Root'].location
GazeLoc[2] = GazeLoc[2]+RootOffset[2]
bpy.data.objects["HeaDRig"].pose.bones['EyesTracker'].location = mu.Vector((GazeLoc))
bpy.data.objects["HeaDRig"].pose.bones['HeadTracker'].location = mu.Vector((HeadLoc))
def RenderAllViewsStill(Locs):
Congruent = 0
RestGazeLoc = (0, 0.18, 0.80)
if socket.gethostname().find("Aidans-Mac")==0:
RenderDir = '/Volumes/Seagate Backup 1/NIH_PhD_nonthesis/7. 3DMacaqueFaces/Renders/CuedLocations'
elif socket.gethostname().find("DESKTOP-5PBDLG6")==0:
RenderDir = 'P:\murphya\Blender\Renders\CuedAttention'
elif socket.gethostname().find("MH01918639MACDT")==0:
RenderDir = 'P:\murphya\Blender\Renders\CuedAttention'
for l in range(0, len(Locs)):
if Congruent == 1:
LookGazeLoc = (0, 0.10, 0.30)
MonkeyLookAt(Locs[l], LookGazeLoc)
elif Congruent == 0:
bpy.data.objects["HeaDRig"].pose.bones['blink'].location = mu.Vector((0,0,0)) # Open eye lids
MonkeyLookAt((0,-0.2,0), (Locs[l][0], Locs[l][2], 0.1) )
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
Filename = "CuedLocation_V2_headLoc%s.png" % (l)
print("Now rendering: " + Filename + " . . .\n")
bpy.context.scene.render.filepath = RenderDir + "/" + Filename
bpy.ops.render.render(write_still=True, use_viewport=True)
def HideTargets():
for t in range(0, 24):
bpy.data.objects['Target %02d' % t].hide = True
bpy.data.objects['Target %02d' % t].hide_render = True
def RenderAllViewsAnimated(Locs):
#================ Set animation parameters
Congruent = 0
FPS = 30
ClipDuration = 0.1 # Duration of each animation (seconds)
bpy.data.scenes["Scene"].render.fps = FPS # Set frame rate (Hz)
bpy.data.scenes["Scene"].frame_start = 1
bpy.data.scenes["Scene"].frame_end = round(ClipDuration*bpy.data.scenes["Scene"].render.fps)
bpy.data.scenes["Scene"].frame_step = 1
bpy.data.scenes["Scene"].render.image_settings.file_format = 'PNG'
#================ Set save directory
if socket.gethostname().find("Aidans-Mac")==0:
RenderDir = '/Volumes/Seagate Backup 1/NIH_PhD_nonthesis/7. 3DMacaqueFaces/Renders/CuedLocations'
elif socket.gethostname().find("DESKTOP-5PBDLG6")==0:
RenderDir = 'P:\murphya\Stimuli\AvatarRenders_2018\GazeFollowing\Circular_24_30cm'
elif socket.gethostname().find("MH01918639MACDT")==0:
RenderDir = 'P:\murphya\Stimuli\AvatarRenders_2018\GazeFollowing\Circular_24_30cm'
Conditions = ['EyesOnly','Head'];
DefaultGazeLoc = (0, 0.18, 0.88)
DefaultHeadLoc = (0, 0, -0.30)
LookGazeLoc = (0, 0.10, 0.30)
RootOffset = bpy.data.objects['Root'].location
#RootOffset = [RootOffset[0], RootOffset[2], RootOffset[1]]
#================ Loop through targets
for l in range(0, 1):#len(Locs)):
#============ Set default gaze/head position for frame 1
bpy.context.scene.frame_set(1)
MonkeyLookAt(DefaultHeadLoc, DefaultGazeLoc)
bpy.data.objects["HeaDRig"].pose.bones['EyesTracker'].keyframe_insert(data_path = "location")
bpy.data.objects["HeaDRig"].pose.bones['HeadTracker'].keyframe_insert(data_path = "location")
#============ Set target gaze/head position for last frame
bpy.context.scene.frame_set(bpy.data.scenes["Scene"].frame_end)
if Congruent == 1:
CurrentLoc = mu.Vector((Locs[l][0], -Locs[l][2], Locs[l][1]))
MonkeyLookAt(CurrentLoc , LookGazeLoc)
bpy.data.objects["HeaDRig"].pose.bones['EyesTracker'].keyframe_insert(data_path = "location")
bpy.data.objects["HeaDRig"].pose.bones['HeadTracker'].keyframe_insert(data_path = "location")
elif Congruent == 0:
#bpy.data.objects["HeaDRig"].pose.bones['blink'].location = mu.Vector((0,0,0)) # Open eye lids
CurrentLoc = mu.Vector((Locs[l][0], Locs[l][2], -Locs[l][1]))
MonkeyLookAt(DefaultHeadLoc, CurrentLoc)
bpy.data.objects["HeaDRig"].pose.bones['EyesTracker'].keyframe_insert(data_path = "location")
bpy.data.objects["HeaDRig"].pose.bones['HeadTracker'].keyframe_insert(data_path = "location")
#============ Return to default gaze/head position for final frame
#bpy.context.scene.frame_set(bpy.data.scenes["Scene"].frame_end)
#MonkeyLookAt(DefaultHeadLoc, DefaultGazeLoc)
#bpy.data.objects["HeaDRig"].pose.bones['EyesTracker'].keyframe_insert(data_path = "location")
#bpy.data.objects["HeaDRig"].pose.bones['HeadTracker'].keyframe_insert(data_path = "location")
#============ Render keyframe sequence
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
#print("Target %d location = %d %d %d" % (l, Locs[l]))
for f in range(bpy.data.scenes["Scene"].frame_start, bpy.data.scenes["Scene"].frame_end):
bpy.context.scene.frame_set( f ) # Set current frame
Filename = "LookAtLocation_%s_Target%s_Frame%03d.png" % (Conditions[Congruent], l, f)
if os.path.isfile(RenderDir + "/" + Filename) == 0:
print("Now rendering: " + Filename + " . . .\n")
#bpy.context.scene.render.filepath = RenderDir + "/" + Filename
#bpy.ops.render.render(write_still=True, use_viewport=True)
elif os.path.isfile(RenderDir + "/" + Filename) == 1:
print("File " + Filename + " already exists. Skipping . . .\n")
RemoveTargetObjects()
InitBlendScene()
Locs = AddTargetObjects()
HideTargets()
#bpy.data.objects["HeaDRig"].pose.bones['blink'].location = mu.Vector((0,0,0.007)) # Close eye lids (blink)
#MonkeyLookAt(Locs[7], Locs[1])
#RenderAllViewsStill(Locs)
RenderAllViewsAnimated(Locs)
#Loc = [0.15,0.2,0]
#MonkeyLookAt(Locs[3], Locs[3])