diff --git a/.gitignore b/.gitignore index a3ae241f..a590a0d5 100644 --- a/.gitignore +++ b/.gitignore @@ -19,9 +19,9 @@ examples/*.vtk examples/*.vtp examples/*.js examples/*.gz -docs/source/_static/k3d.js -docs/source/_static/standalone.js k3d/test/results/* node_modules yarn.lock -k3d/labextension/ \ No newline at end of file +k3d/labextension/ +/docs/source/_static/standalone.js +/docs/source/_static/require.js diff --git a/js/package.json b/js/package.json index b482380d..be71e991 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "k3d", - "version": "2.14.2", + "version": "2.14.3", "description": "3D visualization library", "author": "k3d team", "main": "src/index.js", diff --git a/k3d/__init__.py b/k3d/__init__.py index dd17082f..64bbeedf 100644 --- a/k3d/__init__.py +++ b/k3d/__init__.py @@ -1,4 +1,3 @@ - import json from pathlib import Path @@ -32,6 +31,7 @@ voxel_chunk) from .plot import Plot +from .objects import create_object, clone_object from .transfer_function_editor import transfer_function_editor @@ -42,12 +42,14 @@ with (HERE / "labextension" / "package.json").open() as fid: data = json.load(fid) + def _jupyter_labextension_paths(): return [{ "src": "labextension", "dest": data["name"] }] + def _jupyter_nbextension_paths(): return [{ 'section': 'notebook', diff --git a/k3d/objects.py b/k3d/objects.py index aadf14de..6b3fd987 100644 --- a/k3d/objects.py +++ b/k3d/objects.py @@ -144,6 +144,9 @@ def _ipython_display_(self, **kwargs): plot += self plot.display() + def clone(self): + return clone_object(self) + class DrawableWithVoxelCallback(Drawable): """ @@ -1548,3 +1551,53 @@ def __init__(self, **kwargs): def get_bounding_box(self): return get_bounding_box(self.model_matrix) + + +objects_map = { + 'Line': Line, + 'Label': Label, + 'MIP': MIP, + 'MarchingCubes': MarchingCubes, + 'Mesh': Mesh, + 'Points': Points, + 'STL': STL, + 'SparseVoxels': SparseVoxels, + 'Surface': Surface, + 'Text': Text, + 'Text2d': Text2d, + 'Texture': Texture, + 'TextureText': TextureText, + 'VectorField': VectorField, + 'Vectors': Vectors, + 'Volume': Volume, + 'Voxels': Voxels, + 'VoxelsGroup': VoxelsGroup +} + + +def create_object(obj, is_chunk=False): + from .helpers import from_json + + attributes = { + k: from_json(obj[k]) for k in obj.keys() if k != 'type' + } + + # force to use current version + attributes['_model_module'] = 'k3d_pro' + attributes['_model_module_version'] = version + attributes['_view_module_version'] = version + + if is_chunk: + return VoxelChunk(**attributes) + else: + return objects_map[obj['type']](**attributes) + + +def clone_object(obj): + param = {} + + for k, v in obj.traits().items(): + if "sync" in v.metadata and k not in ['id', 'type']: + param[k] = obj[k] + + return objects_map[obj['type']](**param) diff --git a/k3d/plot.py b/k3d/plot.py index 15199e01..3725f4e8 100644 --- a/k3d/plot.py +++ b/k3d/plot.py @@ -7,30 +7,8 @@ from traitlets import Unicode, Bool, Int, List, Float, Dict from ._version import __version__ as version -from .objects import (Line, Label, MIP, MarchingCubes, Mesh, Points, STL, SparseVoxels, Surface, - Text, Text2d, Texture, TextureText, VectorField, Vectors, Volume, Voxels, - VoxelsGroup, ListOrArray, Drawable, TimeSeries, VoxelChunk) - -objects_map = { - 'Line': Line, - 'Label': Label, - 'MIP': MIP, - 'MarchingCubes': MarchingCubes, - 'Mesh': Mesh, - 'Points': Points, - 'STL': STL, - 'SparseVoxels': SparseVoxels, - 'Surface': Surface, - 'Text': Text, - 'Text2d': Text2d, - 'Texture': Texture, - 'TextureText': TextureText, - 'VectorField': VectorField, - 'Vectors': Vectors, - 'Volume': Volume, - 'Voxels': Voxels, - 'VoxelsGroup': VoxelsGroup -} +from .objects import (ListOrArray, Drawable, TimeSeries, create_object) + import numpy as np @@ -453,33 +431,23 @@ def get_binary_snapshot(self, compression_level=9, voxel_chunks=[]): return zlib.compress(data, compression_level) + def load_binary_snapshot(self, data): import zlib import msgpack - from .helpers import from_json data = msgpack.unpackb(zlib.decompress(data)) self.voxel_chunks = [] - for name in ['objects', 'chunkList']: - if name in data.keys(): - for o in data[name]: - attributes = { - k: from_json(o[k]) for k in o.keys() if k != 'type' - } - - # force to use current version - attributes['_model_module'] = 'k3d' - attributes['_model_module_version'] = version - attributes['_view_module_version'] = version - - if name == 'objects': - o = objects_map[o['type']](**attributes) - self += o - else: - self.voxel_chunks.append(VoxelChunk(**attributes)) - - return self.voxel_chunks + if 'objects' in data.keys(): + for o in data['objects']: + self += create_object(o) + + if 'chunkList' in data.keys(): + for o in data['chunkList']: + self.voxel_chunks.append(create_object(o, True)) + + return data, self.voxel_chunks def get_binary_snapshot_objects(self, voxel_chunks=[]): from .helpers import to_json diff --git a/package.json b/package.json index 61d02166..2f1eacc6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "k3d", - "version": "2.14.2", + "version": "2.14.3", "description": "3D visualization library", "keywords": [ "jupyter",