@@ -453,71 +453,6 @@ class SpotLight(PointLight):
453
453
angle = CFloat (10 , sync = True )
454
454
exponent = CFloat (0.5 , sync = True )
455
455
456
- class SageGraphics (Mesh ):
457
- plot = Instance ('sage.plot.plot3d.base.Graphics3d' )
458
- # TODO material type option
459
-
460
- def _plot_changed (self , name , old , new ):
461
- self .type = new .scenetree_json ()['type' ]
462
- if (self .type == 'object' ):
463
- self .type = new .scenetree_json ()['geometry' ]['type' ]
464
- self .material = self .material_from_object (new )
465
- else :
466
- self .type = new .scenetree_json ()['children' ][0 ]['geometry' ]['type' ]
467
- self .material = self .material_from_other (new )
468
- if (self .type == 'index_face_set' ):
469
- self .geometry = self .geometry_from_plot (new )
470
- elif (self .type == 'sphere' ):
471
- self .geometry = self .geometry_from_sphere (new )
472
- elif (self .type == 'box' ):
473
- self .geometry = self .geometry_from_box (new )
474
-
475
-
476
- def material_from_object (self , p ):
477
- # TODO: do this without scenetree_json()
478
- t = p .texture .scenetree_json ()
479
- m = LambertMaterial (side = 'DoubleSide' )
480
- m .color = t ['color' ]
481
- m .opacity = t ['opacity' ]
482
- # TODO: support other attributes
483
- return m
484
-
485
- def material_from_other (self , p ):
486
- # TODO: do this without scenetree_json()
487
- t = p .scenetree_json ()['children' ][0 ]['texture' ]
488
- m = LambertMaterial (side = 'DoubleSide' )
489
- m .color = t ['color' ]
490
- m .opacity = t ['opacity' ]
491
- # TODO: support other attributes
492
- return m
493
-
494
- def geometry_from_box (self , p ):
495
- g = BoxGeometry ()
496
- g .width = p .scenetree_json ()['geometry' ]['size' ][0 ]
497
- g .height = p .scenetree_json ()['geometry' ]['size' ][1 ]
498
- g .depth = p .scenetree_json ()['geometry' ]['size' ][2 ]
499
- return g
500
-
501
- def geometry_from_sphere (self , p ):
502
- g = SphereGeometry ()
503
- g .radius = p .scenetree_json ()['children' ][0 ]['geometry' ]['radius' ]
504
- return g
505
-
506
- def geometry_from_plot (self , p ):
507
- from itertools import groupby , chain
508
- def flatten (ll ):
509
- return list (chain .from_iterable (ll ))
510
- p .triangulate ()
511
-
512
- g = FaceGeometry ()
513
- g .vertices = flatten (p .vertices ())
514
- f = p .index_faces ()
515
- f .sort (key = len )
516
- faces = {k :flatten (v ) for k ,v in groupby (f ,len )}
517
- g .face3 = faces .get (3 ,[])
518
- g .face4 = faces .get (4 ,[])
519
- return g
520
-
521
456
lights = {
522
457
'colors' : [
523
458
AmbientLight (color = (0.312 ,0.188 ,0.4 )),
@@ -534,3 +469,54 @@ def flatten(ll):
534
469
DirectionalLight (position = [- 1 ,- 1 ,- 1 ], color = [.7 ,.7 ,.7 ]),
535
470
],
536
471
}
472
+
473
+ #######################################################################
474
+ ## Sage Graphics
475
+ #######################################################################
476
+
477
+ def convert_sage_graphics (p ):
478
+ D = p .scenetree_json ()
479
+ obj = sage_handlers [D ['type' ]](D )
480
+ # TODO: make a scene, renderer, and camera, and put obj inside of the scene as a child
481
+ # return the renderer object.
482
+ return obj
483
+
484
+ def json_object (j ):
485
+ geometry = sage_handlers [j ['geometry' ]['type' ]](j ['geometry' ])
486
+ material = sage_handlers ['texture' ](j ['texture' ])
487
+ return Mesh (geometry = geometry , material = material )
488
+
489
+ def json_group (j ):
490
+ m = j ['matrix' ]
491
+ # TODO: transpose m
492
+ children = [sage_handlers [c ['type' ]](c ) for c in j ['children' ]]
493
+ return Object3d (matrix = m , children = children )
494
+
495
+ def json_texture (j )
496
+ return LambertMaterial (side = 'DoubleSide' ,
497
+ color = j ['color' ],
498
+ opacity = j ['opacity' ]
499
+ transparent = j ['opacity' ] < 1 )
500
+ def json_index_face_set (j ):
501
+ # TODO: add a facen attribute to FaceGeometry
502
+ g = FaceGeometry (vertices = j ['vertices' ],
503
+ face3 = j ['face3' ],
504
+ face4 = j ['face4' ],
505
+ facen = j ['facen' ])
506
+
507
+ def json_sphere (j ):
508
+ return SphereGeometry (radius = j ['radius' ])
509
+
510
+ def json_box (j ):
511
+ return BoxGeometry (width = j ['size' ][0 ],
512
+ height = j ['size' ][1 ],
513
+ depth = j ['size' ][2 ])
514
+
515
+ sage_handlers = {
516
+ 'object' : json_object ,
517
+ 'group' : json_group ,
518
+ 'texture' : json_texture
519
+ 'index_face_set' : json_index_face_set ,
520
+ 'sphere' : json_sphere ,
521
+ 'box' : json_box ,
522
+ }
0 commit comments