-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_droprelax.py
265 lines (204 loc) · 8.81 KB
/
color_droprelax.py
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
#import csv
#import os
#import bpy
#filename = 'pythontrial.txt'
#directory = r'C:\Users\gaura\OneDrive\Documents\Blends' # <-- if you have linux or osx
## directory = r'c:\some\directory' # <-- if windows, the r is important
## directory = 'c:/some/directory' # <-- if windows (alternative)
#fullpath = os.path.join(directory, filename)
#with open(fullpath, 'r', newline='') as csvfile:
# ofile = csv.reader(csvfile, delimiter=',')
# # next(ofile) # <-- skip the x,y,z header
# # this makes a generator of the remaining non-empty lines
# rows = (r for r in ofile if r)
# # this converts the string representation of each line
# # to an x,y,z list, and stores it in the verts list.
# verts = [[float(i) for i in r] for r in rows]
#if verts:
# # curve coordinates require a 4th 'W'(weight) component,
# # the +[0.0] adds that for us
# out2 = []
# [out2.extend(list(i)+[0.0]) for i in verts]
# # has one coordinate by default, we add one fewer than we need
# num_points_to_add = len(verts) - 1
# curve = bpy.data.curves.new("path_name", type='CURVE')
# polyline = curve.splines.new(type='POLY')
# polyline.points.add(num_points_to_add)
# polyline.points.foreach_set('co', out2)
# obj = bpy.data.objects.new("obj_name", curve)
# scene = bpy.context.scene
# scene.objects.link(obj)
#with open(fullpath, 'r', newline='') as csvfile:
# ofile = csv.reader(csvfile, delimiter=',')
# next(ofile) # <-- skip the x,y,z header
# # this makes a generator of the remaining non-empty lines
# rows = (r for r in ofile if r)
# # this converts the string representation of each line
# # to an x,y,z list, and stores it in the verts list.
# verts = [[float(i) for i in r] for r in rows]
#if verts:
# # join vertices into one uninterrupted chain of edges.
# edges = [[i, i+1] for i in range(len(verts)-1)]
# mesh = bpy.data.meshes.new("mesh_name")
# mesh.from_pydata(verts, edges, faces=[])
# mesh.update()
# obj = bpy.data.objects.new("obj_name", mesh)
# scene = bpy.context.scene
# scene.objects.link(obj)
import bpy
import csv, os, bmesh, math
import mathutils
from scipy.io import loadmat
import numpy as np
#filepaths
# filepath = bpy.data.filepath
#directory = os.path.dirname(filepath)
#filename = 'pythontrial.txt'
filnamload = r'C:\Users\gaura\OneDrive\Documents\Blends\Stretch_2_Ca0.3827_lmb0.056_N32_dt0.001_exten6.93'
path, dirs, files = next(os.walk(filnamload))
# create the light
# light_data = bpy.data.lamps.new('light', type='POINT')
# light=bpy.data.objects.new('light', light_data)
# scene.objects.link(light)
# light.location=mathutils.Vector((3, -4.2, 5))
# create ligth datablock, set attributes
#light_data = bpy.data.lights.new(name="light_2.80", type='SPOT')
#light_data.energy=5000
## create new object with our light datablock
#light_object = bpy.data.objects.new(name="light_2.80", object_data=light_data)
## set color
#light_object.data.color = (0.05, 1.0, 0.0)
#light_object.data.use_shadow = False
## link light object
#bpy.context.collection.objects.link(light_object)
## make it active
#bpy.context.view_layer.objects.active = light_object
## change location
## light_object.location = (5, 5, 5)
#light_object.location = mathutils.Vector((0, -10, 15))
#light_object.rotation_euler = mathutils.Euler((0.5, 0.0, 0.0))
## update scene if needed
## dg = bpy.context.evaluated_depsgraph_get()
## dg.update()
## create camera
#cam_data = bpy.data.cameras.new('camera')
#cam=bpy.data.objects.new('camera', cam_data)
#bpy.context.collection.objects.link(cam)
#bpy.context.scene.camera = cam
#cam.data.lens=6
#cam.location = mathutils.Vector((-0.25, 5, 3.5))
#cam.rotation_euler = mathutils.Euler((-0.95, 0.0, 0.0))
for j in range(4678,4678 ,5):# len(files)-2):
verts = []
edges = []
faces = []
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
# create the light
# light_data = bpy.data.lamps.new('light', type='POINT')
# light=bpy.data.objects.new('light', light_data)
# scene.objects.link(light)
# light.location=mathutils.Vector((3, -4.2, 5))
# create ligth datablock, set attributes
light_data = bpy.data.lights.new(name="light_2.80", type='SUN')
light_data.energy=5
# create new object with our light datablock
light_object = bpy.data.objects.new(name="light_2.80", object_data=light_data)
# set color
light_object.data.color = (1.0, 1.0, 1.0)
light_object.data.use_shadow = False
# link light object
bpy.context.collection.objects.link(light_object)
# make it active
bpy.context.view_layer.objects.active = light_object
# change location
# light_object.location = (5, 5, 5)
light_object.location = mathutils.Vector((0, 20, 10))
light_object.rotation_euler = mathutils.Euler((0, 0.0, 0.0))
# update scene if needed
# dg = bpy.context.evaluated_depsgraph_get()
# dg.update()
# create camera
cam_data = bpy.data.cameras.new('camera')
cam=bpy.data.objects.new('camera', cam_data)
bpy.context.collection.objects.link(cam)
bpy.context.scene.camera = cam
cam.data.lens=6
cam.location = mathutils.Vector((-0.25, 7, 5))
cam.rotation_euler = mathutils.Euler((-0.95, 0.0, 0.0))
# bpy.ops.mesh.primitive_plane_add(size=200, enter_editmode=False, align='WORLD', location=(0, 0, -2))
datafile = filnamload+'\data ('+str(j)+').mat'
data = loadmat(datafile, squeeze_me=True)
N = len(data['x'])-1
#pointsReader = csv.reader(open(csvpoints, newline=''), delimiter=',')
#with open(csvpoints, 'rt', encoding="utf8") as csvfile:
# pointsReader = csv.reader(csvfile, delimiter=',', quotechar='|')
# for idx, row in enumerate(pointsReader):
# if (idx > 0):
# vert = (float(row[0]), float(row[1]), float(row[2]))
# verts.append(vert)
x = data['x']
y = data['r']
z = np.zeros((N+1))
vert = np.transpose([x, y, z])
for i in range(0, N+1):
verts.append((float(vert[i, 0]), float(vert[i,1]), float(vert[i, 2])))
obj = bpy.context.object
#create mesh and object
mesh = bpy.data.meshes.new("wave")
object = bpy.data.objects.new("wave",mesh)
if verts:
# join vertices into one uninterrupted chain of edges.
edges = [[i, i+1] for i in range(len(verts)-1)]
#create mesh from python data
#mesh.from_pydata(verts,edges,[])
#mesh.update(calc_edges=True)
if verts:
# curve coordinates require a 4th 'W'(weight) component,
# the +[0.0] adds that for us
out2 = []
[out2.extend(list(i)+[0.0]) for i in verts]
# has one coordinate by default, we add one fewer than we need
num_points_to_add = len(verts) - 1
curve = bpy.data.curves.new("path_name", type='CURVE')
polyline = curve.splines.new(type='POLY')
polyline.points.add(num_points_to_add)
polyline.points.foreach_set('co', out2)
mesh_obj = bpy.ops.object.convert(target='MESH')
obj = bpy.data.objects.new("obj_name", curve)
scene = bpy.context.collection
scene.objects.link(obj)
screw = obj.modifiers.new("Screw", 'SCREW')
screw.axis ='X'
# bpy.context.space_data.context = 'MATERIAL'
# mat=bpy.ops.material.new(name="MaterialName")
# bpy.context.object.active_material
# mat.use_nodes=False
# # bpy.context.object.active_material.type='DD'
# # mat.node_tree.nodes["Glossy BSDF"].inputs[0].default_value = (0, 0, 0, 1)
# mat.diffuse_color = (0, 0, 1)
# mat.matallic=1
# obj.active_material = mat
# ob = bpy.context.scene.objects["obj_name"]
ob = bpy.data.objects['obj_name']
# activeObject = bpy.context.active_object
mat=bpy.data.materials.new(name="MaterialName")
# bpy.context.object.active_material.diffuse_color=(1, 0, 0, 1)
ob.data.materials.append(mat)
mat.diffuse_color = (0.0, 0.5, 1.0, 1)
mat.metallic=1
bpy.context.scene.render.image_settings.file_format='PNG'
bpy.context.scene.render.filepath=filnamload+'\data ('+str(j)+').png'
bpy.context.scene.render.resolution_x = 2480
bpy.context.scene.render.resolution_y= 1920
bpy.ops.render.render(use_viewport=True, write_still=True)
# Delete the object
# bpy.data.objects.remove( obj )
##set mesh location
#object.location = bpy.context.scene.cursor.location
#bpy.context.collection.objects.link(object)
# r,g, b = (0.5, 1, 1)
#
# # iterating over the selected objets
# for o in bpy.context.selected_objects:
# o.active_material.diffuse_color=(r, g, b)