JoPmt commited on
Commit
6fae7e8
β€’
1 Parent(s): 31b03ee

Rename app (31).py to app.py

Browse files
Files changed (1) hide show
  1. app (31).py β†’ app.py +10 -157
app (31).py β†’ app.py RENAMED
@@ -6,15 +6,12 @@ import random
6
  from diffusers.utils import load_image
7
  from PIL import Image
8
  import PIL.Image
9
- ##from io_scene_gltf2.blender.exp import gltf2_blender_export
10
 
11
  os.putenv("XDG_RUNTIME_DIR", '.')
12
  fin_rend = './rendered_scene.png'
13
- fin_pth = './path_to_exp.blend'
14
  fin_blend = './exp_scene.blend'
15
- fin_obj = './exp_scene.obj'
16
  fin_glb = './exp_scene.glb'
17
- fin_gltf = './exp_scene.gltf'
18
  fin_anim = './animation.mp4'
19
  img = bpy.data.images.load('./STScI-01G8H1NK4W8CJYHF2DDFD1W0DQ.png')
20
  earth_texture = bpy.data.images.load('./ear0xuu2.jpg')
@@ -23,28 +20,17 @@ apol=[]
23
 
24
  def add_planet(name, radius, mass, distance_from_sun):
25
  material = bpy.data.materials.new(name="Mymarsmat"+str(random.randint(1,36)))
26
- ##material = bpy.data.materials.new(name="MyMaterial")
27
  material.use_nodes = True
28
-
29
- ##for node in material.node_tree.nodes:
30
- ## material.node_tree.nodes.remove(node)
31
-
32
-
33
  image_texture_node = material.node_tree.nodes.new('ShaderNodeTexImage')
34
  if name=="mars":
35
  image_texture_node.image = bpy.data.images.load("mar0kuu2.jpg")
36
  else:
37
  image_texture_node.image = bpy.data.images.load("ear0xuu2.jpg")
38
-
39
- # Link the image texture node to the principled shader
40
  bsdf_node = material.node_tree.nodes.get('Principled BSDF')
41
  material.node_tree.links.new(bsdf_node.inputs['Base Color'], image_texture_node.outputs['Color'])
42
-
43
-
44
  bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=(distance_from_sun * random.uniform(-8, 8), random.uniform(-8, 8), random.uniform(-8, 3)))
45
  planet = bpy.context.active_object
46
  planet.name = name
47
- ##planet_material = bpy.data.materials.new(name=name + "_texture")
48
  planet.data.materials.append(material)
49
  planet["mass"] = mass
50
  return planet
@@ -58,8 +44,6 @@ def visualize_planets():
58
  bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
59
  bpy.context.scene.cursor.location = (0, 0, 0)
60
 
61
- ##visualize_planets()
62
-
63
  def create_planet_flyby_animation(planet, radius, duration):
64
  bpy.context.view_layer.objects.active = planet
65
  planet.select_set(True)
@@ -71,12 +55,10 @@ def create_planet_flyby_animation(planet, radius, duration):
71
  angle = (frame / end_frame) * 2 * math.pi
72
  x = radius * math.cos(angle)
73
  y = radius * math.sin(angle)
74
- z = planet.location.z # Maintain the planet's z-coordinate
75
  planet.location = (x, y, z)
76
  planet.keyframe_insert(data_path="location", index=-1, frame=frame)
77
 
78
- ##create_planet_flyby_animation(planet, radius, 2)
79
-
80
  def add_animated_space_rocks():
81
  bpy.context.scene.render.fps = 10
82
  duration = 2
@@ -99,18 +81,13 @@ def create_particle_effects_animation():
99
  particle_system.particle_system.settings.count = 30
100
  particle_system.particle_system.settings.frame_start = 1
101
  particle_system.particle_system.settings.frame_end = duration * bpy.context.scene.render.fps
102
-
103
- # Set the particle system to be rendered as an object
104
  particle_system.particle_system.settings.render_type = 'OBJECT'
105
  particle_system.particle_system.settings.instance_object = bpy.data.objects["ParticleOne"]
106
-
107
- # Animate the emitter's location, rotation, and scale
108
  emitter.keyframe_insert(data_path="location", index=-1, frame=1)
109
- emitter.location = (random.uniform(-10, 10), random.uniform(-10, 10), random.uniform(-10, 10)) # Set the new location for the end of the animation
110
  emitter.keyframe_insert(data_path="location", index=-1, frame=duration * bpy.context.scene.render.fps)
111
 
112
  def create_meteor_with_particle_system():
113
- # Create a mesh object to serve as the meteor
114
  bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=4, radius=0.25, location=(random.uniform(-10, 10), random.uniform(-10, 10), random.uniform(-10, 10)))
115
  meteor = bpy.context.active_object
116
  meteor.name = "Meteor"
@@ -170,12 +147,6 @@ def create_scene():
170
  bg_node.image = img
171
  tree.links.new(bg_node.outputs[0], tree.nodes['Background'].inputs[0])
172
 
173
- ##world.light_settings.use_ambient_occlusion = True
174
- ##world.light_settings.ao_factor = 0.5
175
- ##world.light_settings.ao_factor = 0.5
176
- ##world.light_settings.use_environment_light = True
177
- ##world.light_settings.environment_energy = 0.5
178
-
179
  bpy.data.objects['Cube'].select_set(True)
180
  bpy.ops.object.delete()
181
  bpy.data.objects['Camera'].select_set(True)
@@ -197,9 +168,6 @@ def create_scene():
197
  ##planet2 = add_planet("mars", 1.75, 0.5, 1.5)
198
  add_planet("earth", 2.5, 1, 0)
199
  add_planet("mars", 1.75, 0.5, 1.5)
200
- ##bpy.context.active_object=None
201
-
202
-
203
 
204
  add_animated_space_rocks()
205
  create_meteor_with_particle_system()
@@ -208,14 +176,7 @@ def create_scene():
208
  create_scene()
209
 
210
  def export_blend():
211
- ##bpy.ops.wm.save_mainfile(filepath='./path_to_exp.blend')
212
  bpy.ops.wm.save_as_mainfile(filepath='./exp_scene.blend')
213
- ##bpy.ops.export_scene.obj(filepath='./exp_scene.obj', export_format='OBJ', export_materials='EXPORT')
214
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB', check_existing=False, export_image_format='AUTO', export_materials='EXPORT', export_cameras=True, export_lights=True)
215
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.gltf', export_selected=False)
216
- ##cntxt = bpy.context
217
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB')
218
- ##bpy.ops.export_scene.gltf(cntxt, RenderSettings)
219
  last_created_time = 0
220
  last_created_file = None
221
  apol=[]
@@ -228,21 +189,12 @@ def export_blend():
228
  last_created_time = created_time
229
  last_created_file = file_path
230
 
231
- ##imoge = load_image(last_created_file).convert('RGB').resize((512, 512))
232
  apol.append(last_created_file)
233
- ##return last_created_file
234
- ##return fin_rend
235
- ##return apol
236
  return last_created_file
237
 
238
- def render_scene(blk,sny):
239
  bpy.context.scene.render.filepath = './rendered_scene.png'
240
  bpy.ops.render.render(animation=False, write_still=True)
241
- ##bpy.ops.render.opengl(animation=False, render_keyed_only=False, sequencer=False, write_still=True, view_context=False)
242
- ##imoge = load_image(fin_rend)
243
- ##imoge.save('./rendered_scene.png', 'PNG')
244
- ##imoge = PIL.Image(fin_rend)
245
- ##imoge = load_image().convert('RGB').resize((512, 512))
246
  last_created_time = 0
247
  last_created_file = None
248
  apol=[]
@@ -257,93 +209,11 @@ def render_scene(blk,sny):
257
 
258
  imoge = load_image(last_created_file).convert('RGB').resize((512, 512))
259
  apol.append(imoge)
260
- ##blk=gr.HTML(value="""<html><head><title>Three.js Blender Scene Viewer</title><style>body { margin: 0; } canvas { display: block; } </style></head><body><script src='https://threejs.org/build/three.js'></script><script src='https://threejs.org/examples/jsm/loaders/GLTFLoader.js'></script><script> var scene, camera, renderer; function init() { scene = new THREE.Scene(); camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.z = 5; renderer = new THREE.WebGLRenderer({antialias: true}); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); var loader = new THREE.GLTFLoader(); loader.load('"""+sny+"""', function(gltf) { scene.add(gltf.scene); }, undefined, function(error) { console.error(error); }); }; function animate(){ requestAnimationFrame(animate); renderer.render(scene, camera); }; init(); animate();</script></body></html>""")
261
- pckg = f"/file={sny}"
262
  moog = gr.Model3D(value=sny)
263
- return apol, f"""<iframe src="file=frame.html" width="100%" height="200px" data-glb="exp_scene.glb"></iframe>""", sny, moog
264
-
265
- ##blk=gr.HTML(value=html)
266
- ##return last_created_file
267
- ##return fin_rend
268
- ##return apol,blk,sny
269
-
270
- def export_obj():
271
- ##bpy.ops.wm.save_mainfile(filepath='./path_to_exp.blend')
272
- ##bpy.ops.wm.save_as_mainfile(filepath='./exp_scene.blend')
273
- ##bpy.ops.export_scene.obj(filepath='./exp_scene.obj', export_format='OBJ', export_materials='EXPORT')
274
- bpy.ops.export_scene.gltf(filepath="./exp_scene.obj", export_format='OBJ', export_selected=False, export_apply=False, export_texcoords='TANGENT', export_normals='FACE', export_materials='EXPORT', export_colors='EXPORT', export_cameras='EXPORT', export_lights='EXPORT', export_displacement='NONE', export_animations=True, export_frame_range=True, export_frame_step=1, export_force_sampling=False, export_current_frame=False, export_extras=True)
275
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB', check_existing=False, export_image_format='AUTO', export_materials='EXPORT', export_cameras=True, export_lights=True)
276
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.gltf', export_selected=False)
277
- ##cntxt = bpy.context
278
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB')
279
- ##bpy.ops.export_scene.gltf(cntxt, RenderSettings)
280
- last_created_time = 0
281
- last_created_file = None
282
- apol=[]
283
-
284
- for root, dirs, files in os.walk('.'):
285
- for file in files:
286
- file_path = os.path.join(root, file)
287
- created_time = os.path.getctime(file_path)
288
- if created_time > last_created_time:
289
- last_created_time = created_time
290
- last_created_file = file_path
291
-
292
- ##imoge = load_image(last_created_file).convert('RGB').resize((512, 512))
293
- apol.append(last_created_file)
294
- ##return last_created_file
295
- ##return fin_rend
296
- return apol
297
 
298
  def export_glb():
299
- ##bpy.ops.wm.save_mainfile(filepath='./path_to_exp.blend')
300
- ##bpy.ops.wm.save_as_mainfile(filepath='./exp_scene.blend')
301
- ##bpy.ops.export_scene.obj(filepath='./exp_scene.obj', export_format='OBJ', export_materials='EXPORT')
302
- ##for ob in bpy.context.scene.objects:
303
- ## ob.select_set(False)
304
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB', export_image_format='NONE', export_materials='NONE', export_cameras=True, export_lights=True)
305
  bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB')##, export_image_format='AUTO', export_materials='EXPORT', export_cameras=True, export_lights=True)
306
- ##export_settings = {
307
- ##'filepath': './exp_scene.glb',
308
- ##'export_format': 'GLB',
309
- ##'gltf_current_frame': 1,
310
- ##}
311
-
312
- ##gltf2_blender_export.save(bpy.context, export_settings)
313
- ##bpy.ops.export_scene.gltf(filepath="./exp_scene.glb", export_format='GLB', export_selected=False, export_apply=False, export_texcoords=True, export_normals=True, export_materials=True, export_colors=True, export_cameras=True, export_lights=True, export_displacement=False, export_animations=True, export_frame_range=True, export_frame_step=1, export_force_sampling=False, export_current_frame=False, export_extras=True)
314
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.gltf', export_selected=False)
315
- ##cntxt = bpy.context
316
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB')
317
- ##bpy.ops.export_scene.gltf(cntxt, RenderSettings)
318
- last_created_time = 0
319
- last_created_file = None
320
- apol=[]
321
-
322
- for root, dirs, files in os.walk('.'):
323
- for file in files:
324
- file_path = os.path.join(root, file)
325
- created_time = os.path.getctime(file_path)
326
- if created_time > last_created_time:
327
- last_created_time = created_time
328
- last_created_file = file_path
329
-
330
- ##imoge = load_image(last_created_file).convert('RGB').resize((512, 512))
331
- apol.append(last_created_file)
332
- ##return last_created_file
333
- ##return fin_rend
334
- ##return apol
335
- return last_created_file
336
-
337
- def export_gltf():
338
- ##bpy.ops.wm.save_mainfile(filepath='./path_to_exp.blend')
339
- ##bpy.ops.wm.save_as_mainfile(filepath='./exp_scene.blend')
340
- ##bpy.ops.export_scene.obj(filepath='./exp_scene.obj', export_format='OBJ', export_materials='EXPORT')
341
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB', check_existing=False, export_image_format='AUTO', export_materials='EXPORT', export_cameras=True, export_lights=True)
342
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.gltf',export_format='GLTF_SEPARATE')
343
- bpy.ops.export_scene.gltf(filepath='./exp_scene.gltf',export_format='GLB')
344
- ##cntxt = bpy.context
345
- ##bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB')
346
- ##bpy.ops.export_scene.gltf(cntxt, RenderSettings)
347
  last_created_time = 0
348
  last_created_file = None
349
  apol=[]
@@ -356,14 +226,10 @@ def export_gltf():
356
  last_created_time = created_time
357
  last_created_file = file_path
358
 
359
- ##imoge = load_image(last_created_file).convert('RGB').resize((512, 512))
360
  apol.append(last_created_file)
361
- ##return last_created_file
362
- ##return fin_rend
363
- ##return apol
364
  return last_created_file
365
 
366
- def render_anim():
367
  bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
368
  bpy.context.scene.render.ffmpeg.format = 'MPEG4'
369
  bpy.context.scene.render.ffmpeg.codec = 'H264'
@@ -385,34 +251,21 @@ def render_anim():
385
  last_created_time = created_time
386
  last_created_file = file_path
387
 
388
- ##imoge = load_image(last_created_file).convert('RGB').resize((512, 512))
389
  apol.append(last_created_file)
390
- ##return last_created_file
391
- ##return fin_rend
392
- return apol[0]
393
-
394
- ##return fin_anim
395
-
396
- ##export_glb()
397
- ##export_gltf()
398
- ##export_blend()
399
 
400
  with gr.Blocks() as iface:
401
  with gr.Column():
402
- # HTML block to view the exported scene using Three.js
403
-
404
  moog = gr.Model3D()
405
- ##zery = gr.Button("create Scene")
406
  ony = gr.Button("render Image")
407
  twy = gr.Button("render Animation")
408
  rani = gr.Video()
409
  rdr = gr.Gallery(columns=1)
410
  sny = gr.File(value=export_glb(),interactive=False)
411
- ##sey = gr.File(value=export_gltf(),interactive=False)
412
  siy = gr.File(value=export_blend(),interactive=False)
413
- blk=gr.HTML(value="")
414
 
415
- ony.click(render_scene, [blk,sny], [rdr,blk,sny,moog])
416
- twy.click(render_anim, None, rani)
417
 
418
  iface.launch()
 
6
  from diffusers.utils import load_image
7
  from PIL import Image
8
  import PIL.Image
9
+ import tqdm
10
 
11
  os.putenv("XDG_RUNTIME_DIR", '.')
12
  fin_rend = './rendered_scene.png'
 
13
  fin_blend = './exp_scene.blend'
 
14
  fin_glb = './exp_scene.glb'
 
15
  fin_anim = './animation.mp4'
16
  img = bpy.data.images.load('./STScI-01G8H1NK4W8CJYHF2DDFD1W0DQ.png')
17
  earth_texture = bpy.data.images.load('./ear0xuu2.jpg')
 
20
 
21
  def add_planet(name, radius, mass, distance_from_sun):
22
  material = bpy.data.materials.new(name="Mymarsmat"+str(random.randint(1,36)))
 
23
  material.use_nodes = True
 
 
 
 
 
24
  image_texture_node = material.node_tree.nodes.new('ShaderNodeTexImage')
25
  if name=="mars":
26
  image_texture_node.image = bpy.data.images.load("mar0kuu2.jpg")
27
  else:
28
  image_texture_node.image = bpy.data.images.load("ear0xuu2.jpg")
 
 
29
  bsdf_node = material.node_tree.nodes.get('Principled BSDF')
30
  material.node_tree.links.new(bsdf_node.inputs['Base Color'], image_texture_node.outputs['Color'])
 
 
31
  bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=(distance_from_sun * random.uniform(-8, 8), random.uniform(-8, 8), random.uniform(-8, 3)))
32
  planet = bpy.context.active_object
33
  planet.name = name
 
34
  planet.data.materials.append(material)
35
  planet["mass"] = mass
36
  return planet
 
44
  bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
45
  bpy.context.scene.cursor.location = (0, 0, 0)
46
 
 
 
47
  def create_planet_flyby_animation(planet, radius, duration):
48
  bpy.context.view_layer.objects.active = planet
49
  planet.select_set(True)
 
55
  angle = (frame / end_frame) * 2 * math.pi
56
  x = radius * math.cos(angle)
57
  y = radius * math.sin(angle)
58
+ z = planet.location.z
59
  planet.location = (x, y, z)
60
  planet.keyframe_insert(data_path="location", index=-1, frame=frame)
61
 
 
 
62
  def add_animated_space_rocks():
63
  bpy.context.scene.render.fps = 10
64
  duration = 2
 
81
  particle_system.particle_system.settings.count = 30
82
  particle_system.particle_system.settings.frame_start = 1
83
  particle_system.particle_system.settings.frame_end = duration * bpy.context.scene.render.fps
 
 
84
  particle_system.particle_system.settings.render_type = 'OBJECT'
85
  particle_system.particle_system.settings.instance_object = bpy.data.objects["ParticleOne"]
 
 
86
  emitter.keyframe_insert(data_path="location", index=-1, frame=1)
87
+ emitter.location = (random.uniform(-10, 10), random.uniform(-10, 10), random.uniform(-10, 10))
88
  emitter.keyframe_insert(data_path="location", index=-1, frame=duration * bpy.context.scene.render.fps)
89
 
90
  def create_meteor_with_particle_system():
 
91
  bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=4, radius=0.25, location=(random.uniform(-10, 10), random.uniform(-10, 10), random.uniform(-10, 10)))
92
  meteor = bpy.context.active_object
93
  meteor.name = "Meteor"
 
147
  bg_node.image = img
148
  tree.links.new(bg_node.outputs[0], tree.nodes['Background'].inputs[0])
149
 
 
 
 
 
 
 
150
  bpy.data.objects['Cube'].select_set(True)
151
  bpy.ops.object.delete()
152
  bpy.data.objects['Camera'].select_set(True)
 
168
  ##planet2 = add_planet("mars", 1.75, 0.5, 1.5)
169
  add_planet("earth", 2.5, 1, 0)
170
  add_planet("mars", 1.75, 0.5, 1.5)
 
 
 
171
 
172
  add_animated_space_rocks()
173
  create_meteor_with_particle_system()
 
176
  create_scene()
177
 
178
  def export_blend():
 
179
  bpy.ops.wm.save_as_mainfile(filepath='./exp_scene.blend')
 
 
 
 
 
 
180
  last_created_time = 0
181
  last_created_file = None
182
  apol=[]
 
189
  last_created_time = created_time
190
  last_created_file = file_path
191
 
 
192
  apol.append(last_created_file)
 
 
 
193
  return last_created_file
194
 
195
+ def render_scene(sny,progress=gr.Progress(track_tqdm=True)):
196
  bpy.context.scene.render.filepath = './rendered_scene.png'
197
  bpy.ops.render.render(animation=False, write_still=True)
 
 
 
 
 
198
  last_created_time = 0
199
  last_created_file = None
200
  apol=[]
 
209
 
210
  imoge = load_image(last_created_file).convert('RGB').resize((512, 512))
211
  apol.append(imoge)
 
 
212
  moog = gr.Model3D(value=sny)
213
+ return apol, sny, moog
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
  def export_glb():
 
 
 
 
 
 
216
  bpy.ops.export_scene.gltf(filepath='./exp_scene.glb', export_format='GLB')##, export_image_format='AUTO', export_materials='EXPORT', export_cameras=True, export_lights=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  last_created_time = 0
218
  last_created_file = None
219
  apol=[]
 
226
  last_created_time = created_time
227
  last_created_file = file_path
228
 
 
229
  apol.append(last_created_file)
 
 
 
230
  return last_created_file
231
 
232
+ def render_anim(sny,progress=gr.Progress(track_tqdm=True)):
233
  bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
234
  bpy.context.scene.render.ffmpeg.format = 'MPEG4'
235
  bpy.context.scene.render.ffmpeg.codec = 'H264'
 
251
  last_created_time = created_time
252
  last_created_file = file_path
253
 
 
254
  apol.append(last_created_file)
255
+ moog = gr.Model3D(value=sny)
256
+ return apol[0], sny, moog
 
 
 
 
 
 
 
257
 
258
  with gr.Blocks() as iface:
259
  with gr.Column():
 
 
260
  moog = gr.Model3D()
 
261
  ony = gr.Button("render Image")
262
  twy = gr.Button("render Animation")
263
  rani = gr.Video()
264
  rdr = gr.Gallery(columns=1)
265
  sny = gr.File(value=export_glb(),interactive=False)
 
266
  siy = gr.File(value=export_blend(),interactive=False)
 
267
 
268
+ ony.click(render_scene, [sny], [rdr,sny,moog])
269
+ twy.click(render_anim, [sny], [rani,sny,moog])
270
 
271
  iface.launch()