0
$\begingroup$

I have a blender python script that I used to produce a lot of different dataset for my machine learning project. The problem that I have is that I use a lot of nested loop to simulate a lot of different scene. This is my full script

import bpy
import os
import glob
from math import pi

scene = bpy.context.scene
directory = '/home/cgal/testimageSFS/Hasil_blender/'
list_ob = '/home/cgal/3d_scans/*.ply'
lampu = bpy.data.objects["RGB"]
lampu_utama = bpy.data.objects["Lamp"]

counter = 0

rgb = {'r':(1,0,0) ,'g': (0,1,0) ,'b' : (0,0,1)}

for img in glob.glob(list_ob):

    #create face object
    bpy.ops.object.select_all(action='DESELECT')
    bpy.ops.import_mesh.ply(filepath=img)
    obj = bpy.context.selected_objects[0]
    nama = str(counter)
    obj.name = nama

    #modify face
    face = bpy.data.objects[nama]
    face.scale =(0.00001,0.00001,0.00001)
    face.rotation_euler.x = pi/2
    face.location.z = 3.23

    #attach skin
    mat = bpy.data.materials.new(name = "Skin")
    mat.use_nodes = True #Make so it has a node tree

    #Add the vertex color node
    vc = mat.node_tree.nodes.new('ShaderNodeVertexColor')
    #Assign its layer
    vc.layer_name = "Col"
    #Get the shader
    bsdf = mat.node_tree.nodes["Principled BSDF"]
    #Link the vertex color to the shader
    mat.node_tree.links.new( vc.outputs[0], bsdf.inputs[0] )
    face.data.materials.append(mat)
    #take Pic
    for x in range(-10,11,2):
        for y in range(-7,-2,1):
            for z in range(-6,11,2):
                #change center_lamp position
                lampu_utama.location.x = x
                lampu_utama.location.y = y
                lampu_utama.location.z = z
                xs = str(x+10)
                xs = xs.zfill(2)
                zs = str(z+6)
                zs = zs.zfill(2)
                utama_loc =xs + str(y+7) + zs
                for pow in [1000,5000,10000]:
                    #change RGB power
                    lampu.data.energy = pow
                    power = str(pow)
                    power = power.zfill(5)
                    for i in range(-5,6,1):
                        #change RGB light position
                        lampu.location.x = i

                        #take normal picture
                        for ob in scene.objects:
                            if ob.type == 'CAMERA':
                                lampu.hide_render=True
                                bpy.context.scene.camera = ob
                                file = os.path.join(directory,nama+'_'\
                                +'_'+utama_loc+'_'+str(i+5)+'_'+power+'_a' )
                                bpy.context.scene.render.filepath = file
                                bpy.ops.render.render( write_still=True )

                        for n,color in rgb.items():
                            lampu.data.color = color
                            col=n

                            #record RGB
                            for ob in scene.objects:
                                if ob.type == 'CAMERA':
                                    lampu.hide_render = False
                                    bpy.context.scene.camera = ob
                                    file = os.path.join(directory,nama+'_'\
                                    +'_'+utama_loc+'_'+ str(i+5)+'_'+power+'_'+col )
                                    bpy.context.scene.render.filepath = file
                                    bpy.ops.render.render( write_still=True )

    #delete face object
    bpy.data.objects[nama].select_set(True)
    bpy.ops.object.delete()
    counter=counter+1
    break

The problem is I keep having my PC freeze at some point (not fixed sometimes in 6 min sometimes in 7 min) even when I run the script without GUI, this is the command that I used blender myscene.blend --background --python myscript.py. So is there any solution to fix this besides reducing the number of possible scene on my script? Thank you

My PC specs :
ubuntu 18.04
i7 3770
gtx 1080ti
16 GB of RAM

Full blend file

link to one of .ply I used : download here

$\endgroup$
17
  • $\begingroup$ How many ply files? Some parts could be put before the main loop. like deselecting all, getting the camera list. Also could create one material for all as it seems it does not depend on anything. You can also loop over the files in cli instead of doing it inside blender. $\endgroup$
    – lemon
    Commented Mar 12, 2020 at 8:56
  • $\begingroup$ There is a 10 .ply files but the freeze already happened when I just used 1 ply files as you can see I put break at bottom. For material, I am not quite sure what you suggest since it will be different model later on when I erase the break and I don't know how to make a loop with CLI since all the object are blender, can you elaborate on that? $\endgroup$ Commented Mar 12, 2020 at 9:09
  • $\begingroup$ if crashes on the first file, the CLI "trick" is not the point. How many render results do you have before the crash? Also what is happening if you render manually without script? $\endgroup$
    – lemon
    Commented Mar 12, 2020 at 9:13
  • $\begingroup$ Around 10.000 images with size of 256x256 although sometimes before reaching that already freeze. I don't understand what you mean by render manually, if I render it manually it works fine. I used to run this script without the x y z loop and it run just fine until the last .ply file but since I need more variation for my dataset I add those $\endgroup$ Commented Mar 12, 2020 at 9:19
  • $\begingroup$ 10000 means you don't go to the end, I presume. Trying to help, few suggestions: look at memory/cpu usage when running it. Reduce the loops (ranges, steps, etc. for test purpose) to see what is happening. Eventually try it without the very low scaling (just an intuition). $\endgroup$
    – lemon
    Commented Mar 12, 2020 at 9:51

1 Answer 1

1
$\begingroup$

Ok so apparently when I run it on same PC but with windows OS it can run without any crash so far. So I will submit this into bug for linux later on. Thank you for all the helpful comment especially lemon who go as far as trying my code in his/her PC

$\endgroup$
4
  • $\begingroup$ FYI that's probably the same case as blender.stackexchange.com/a/170197/19156 . Could you give it a try under Linux? $\endgroup$
    – lemon
    Commented Mar 19, 2020 at 13:14
  • $\begingroup$ Actually I forgot to update but my windows also crash when I tried to run on 2nd time. So I tried using it on my friend's PC and it can run without problem. Maybe the problem is the PC I am using $\endgroup$ Commented Mar 21, 2020 at 5:46
  • $\begingroup$ Consider "placing lampu.hide_render=True" (or False) outside of the "for ob in scene.objects:" loops. $\endgroup$
    – lemon
    Commented Mar 21, 2020 at 8:02
  • $\begingroup$ Yeah thanks again for the help, I will change it for future creation $\endgroup$ Commented Mar 23, 2020 at 3:34

You must log in to answer this question.

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