2
$\begingroup$

Blender 2.8 windows 10

So the idea of this script is that it will look in a directory for a good 3 thousand or so object files and one by one import them do some things like recalculating normals and converting triangles. And then saving blend files. However I am very very new to python coding and am having a bit of trouble.

import math
import bpy
import mathutils
import os

# a list of all models in the directory all models
model_list = os.listdir("B:\Planetside LOA project\Assets\All Models")
# the total number of models to be used in the while loop
model_total = len(model_list)
# current selected model
model_select = 0
# path to the model directory

# stored path of directory and specific model file
target_path = "yeet"
# to store the model's name in memory for simplicity
temp_model_name = "dab"
# the name of the object minus .obj
cur_object = "yote"

while (model_select != 1):
    target_path = "'B:\\Planetside LOA project\\Assets\\All Models\\" + model_list[model_select] + "\\'"
    # imports the model
    bpy.ops.import_scene.obj(filepath=target_path)
    # gets the name of the current object
    temp_model_name = model_list[model_select]
    # figures out the length of the filename minus 4 characters
    rem_obj = len(temp_model_name) - 4
    # stores cur object as the object name as it is imported into blender
    cur_object = "bpy.data.objects['" + temp_model_name[0:rem_obj] + "']"
    # selects the object that was imported
    cur_object.select = True

    model_select = model_select + 1

The while loop has been modified for testing purposes it would normally be model_select != model_total. The problem seemed to initially be with the target path string the backslashes where causing line continuation errors so I doubled them up after doing some research. Interesting note however I entered this line into the console

>>> str = "B:\Planetside LOA project\Assets\All Models"

the output is well confusing to say the least here is what it reads.

>>> str
'B:\\Planetside LOA project\\Assets\\All Models'

I have exactly zero idea why the output would double up the \s. Entering the same code with doubled slashes leads to the same output. Just a side note that was confusing Anyway the console spits an error about the script failing and to check the info console but there is no further information. And I am completely stumped. here it is exactly not very informative

bpy.ops.text.run_script()
Python script failed, check the message in the system console

And there is no further console info. At least for my messed up string it gave me something.

$\endgroup$
1

1 Answer 1

1
$\begingroup$

Thats an "accident" of early 1980’s. The first version of MS DOS used the forward slash for command-line options. When Microsoft added support for folders in MS-DOS 2.0, the forward slash was already taken so they used a backslash instead. Still stuck with this incompatibility 39 years later.

Windows: C:\folder\file.txt

Most other OS: /folder/file.txt

But \ is an escape character in most OS, thats why you have to escape it and to escape it you use another \ so it becomes \\.

Using the variable os.path.sep will give you the correct one, so you can simply use it to build your paths. But if you know which os you are working on you can write it by hand, your choice.

$\endgroup$
7
  • $\begingroup$ right I ended up changing them all to \\ but that seemed to leave me with a different unrelated error is there a way of using IDE or some other code editor that is better then blenders text editor. $\endgroup$ Apr 2, 2019 at 23:54
  • $\begingroup$ Its the same in any editor, its operating system related as stated in my answer. If you use it only once like "\" the following character will be interpreted as a control character (new line, tabulator) and you dont want that, because it will lead straight to an error. You know that "\n" means new line? If your filename will be numbers.txt the path will be c:\\folder\numbers.txt and the included sequence "\n" will be interpreted as a new line, but it isnt. Your path would end after c:\\folder $\endgroup$
    – MBrain
    Apr 3, 2019 at 1:27
  • $\begingroup$ Right I see that and I fixed that error I think you misunderstood my question. I believe there are different unrelated errors as I use os.path.sep variables in place of \s and os.pathse in place of the :s however I still get errors that the console is vary vague about I am wondering if I can import the code into a different editor one that can figure out my mistakes better then just a plain text editor. $\endgroup$ Apr 3, 2019 at 21:09
  • $\begingroup$ you can run it in the console, you will see all errors. Maybe you can show your error log. $\endgroup$
    – MBrain
    Apr 6, 2019 at 19:21
  • 1
    $\begingroup$ The error message refers to the blender system console, which you can view from menubar -> Window -> Toggle Console but i dont mean the console thats integrated into blender. your system console like xterm or cmd. just use the relevant code, no bpy. Just for understanding the error with your path. $\endgroup$
    – MBrain
    Apr 7, 2019 at 18:50

You must log in to answer this question.

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