Skip to content

Commit

Permalink
k3d.clone_object
Browse files Browse the repository at this point in the history
object.clone
  • Loading branch information
artur-trzesiok committed Jun 7, 2022
1 parent 2517033 commit 3b1345d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 50 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
k3d/labextension/
/docs/source/_static/standalone.js
/docs/source/_static/require.js
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 3 additions & 1 deletion k3d/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json
from pathlib import Path

Expand Down Expand Up @@ -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

Expand All @@ -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',
Expand Down
53 changes: 53 additions & 0 deletions k3d/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def _ipython_display_(self, **kwargs):
plot += self
plot.display()

def clone(self):
return clone_object(self)


class DrawableWithVoxelCallback(Drawable):
"""
Expand Down Expand Up @@ -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)
56 changes: 12 additions & 44 deletions k3d/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "k3d",
"version": "2.14.2",
"version": "2.14.3",
"description": "3D visualization library",
"keywords": [
"jupyter",
Expand Down

0 comments on commit 3b1345d

Please sign in to comment.