0
$\begingroup$

As part of a larger add on, I need to save selected vertices as a text file in this format:

[
   x  y  z;
   x  y  z;
   x  y  z
]

Where x y and z are the coordinates of the vertices. This text file is called later in the main function. I am new to python and I am having trouble with:

  • getting permission to write this text file
  • getting it to write in the aforementioned format
  • calling the working directory

I have put my code down below (some of the initial imports are not used but from playing around with this). I have looked through countless platforms and similar questions and cannot find anything that helps. I made my 3 main concerns bold but please let me know if anyone has a solution to one or all of the issues, and thank you in advance!!

    import bpy
    import bmesh
    import jdata as jd
    import pathlib
    import os
    import pathlib
    import tempfile
    os.umask(0) 
    filefolder = #### call working directory or manually set it 
    selectedverts = [v for v in bpy.context.active_object.data.vertices 
    if v.select]

    init_points = []
    for v in selectedverts:
    init_points.append(v.co) #saves the verticds as init_poits 
    os.chmod(filefolder, 0o777)
    with open( filefolder,'w') as outfile:
       # text = json.dumps(data, indent = 4)
    outfile.write(init_points)
$\endgroup$
2
  • $\begingroup$ The format [x y z; x y z; x y z], is not valid JSON syntax. JSON uses specific formatting rules, such as using square brackets to denote arrays/lists and separating items within the list with commas. So you can't use the JSON serializer/parser. Do you really want it to be your format or do you want it to be standard json format? $\endgroup$ Apr 27 at 4:58
  • $\begingroup$ I actually think a text file would be okay $\endgroup$ Apr 27 at 13:51

1 Answer 1

0
$\begingroup$

The format [x y z; x y z; x y z], is not valid JSON syntax. JSON uses specific formatting rules, such as using square brackets to denote arrays or lists. So you can't use the JSON serializer or parser for this purpose. To output a standard JSON formatted string into your text file, you can use this script:

import bpy
import json
import os

os.umask(0) 

filefolder = r'/path/to/output.txt'

selectedverts = [v for v in bpy.context.active_object.data.vertices if v.select]

init_points = []
for v in selectedverts:
    init_points.append(list(v.co))

os.chmod(filefolder, 0o777)

with open(filefolder, 'w') as outfile:
    text = json.dumps(init_points, indent=4)
    outfile.write(text)

Example result for 3 selected vertices:

enter image description here

If you want to use your own custom format x y z; x y z; x y z, then you can't use JSON. But you can use the following script instead:

import bpy
import json
import os

os.umask(0) 

filefolder = r'path/to/output.txt'

selectedverts = [v for v in bpy.context.active_object.data.vertices if v.select]

os.chmod(filefolder, 0o777)

def serialize_points(verts):
    return '; '.join([f"{v.co.x} {v.co.y} {v.co.z}" for v in verts])

serialized_points = serialize_points(selectedverts)

with open(filefolder, 'w') as outfile:
    outfile.write(serialized_points)

Example result for 3 selected vertices:

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you for the response. I attempted the latter code because I want to save it as a txt file but I am still having trouble with the permission. I tried going to the file location on my windows computer and changing the settings to give myself all the permissions and it is still giving the error. Also I tried filefolder = os.getcwd() and outfile = os.path.join(filefolder, 'test.txt') $\endgroup$ Apr 27 at 14:48
  • $\begingroup$ I also added os.chmod(outfile, 0o777) and now I get an error where it cannot find the text file. (I want the code to create this text file, it doesnt exist before) $\endgroup$ Apr 27 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .