[Bf-committers] Using bpy* types in a standalone script?

Sybren A. Stüvel sybren at stuvel.eu
Wed May 2 12:13:27 CEST 2018


Hi Torsten,

You can build Blender as a Python module [1], then you can import it
into your script. As a bonus, you won't even have to pickle/unpickle, as
you can directly access the data you're intersted in.

If you don't want this for some reason, I would recommend writing your
data to a file format that is independent of Blender's data model (like
JSON or YAML).

[1] https://wiki.blender.org/index.php/User:Ideasman42/BlenderAsPyModule

Cheers,
Sybren

On 02-05-18 00:20, tmohr at s.netic.de wrote:
> Hello,
>
> Can I import bpy etc. in an own script (that is not running inside blender) to
> make use
> of these types?
>
> The background is that I would like to export some object data from within python
> using pickle.dumps() and import it in an own script to further process it there.
>
> Exporting works, but when importing (in a standalone script, not inside blender)
> then the
> used data types are unknown.
>
> I would like to export / import data this way because a python export then contains
> all data in he known format.
>
> It would be great if somebody could confirm / disconfirm if the bpy internal
> types can be used or not.
>
>
> Here is how I export the data:
> import bpy
> import pickle
> import zlib
>
> c = bpy.context
> d = bpy.data
>
> cp = pickle.dumps([c, d])
> cz = zlib.compress(cp)
> fd = open("/home/tmohr/blender/dcopy/dump.pz", "wb")
> fd.write(cz)
> fd.close()
> print("Done")
>
>
> Here is how I try to import the data again:
> import zlib
> import pickle
> import pprint
> import sys
>
> sys.path.append("/opt/blender-2.79a-rc-linux-glibc219-x86_64/2.79/scripts/modules")
>
> import bpy
>
>
> fd = open(sys.argv[1], "rb")
> cz = fd.read()
> cp = zlib.decompress(cz)
> c = pickle.loads(cp)
>
> pp = pprint.PrettyPrinter(indent=4)
> pp.pprint(c)
>
>
>
>
> Thanks for any hints,
> Torsten
>
> _______________________________________________
> Bf-committers mailing list
> Bf-committers at blender.org
> https://lists.blender.org/mailman/listinfo/bf-committers

-- 
Sybren A. Stüvel

https://stuvelfoto.nl/
https://stuvel.eu/



More information about the Bf-committers mailing list