grebtsew / FloorplanToBlender3d

Create 3d rooms in blender from floorplans.
GNU General Public License v3.0
396 stars 103 forks source link

Does this work in Linux namely Ubuntu 18.04? #1

Closed shubhamwagh closed 5 years ago

grebtsew commented 5 years ago

The implementation is currently only tested on Windows 10. But I don't see why it wouldn't work on Ubuntu 18.04. I am not using any windows exclusive commands and all paths using windowsformat can be easily changed. Blender 3d is a requirement of course. Please do try it out and if you encounter any obstacles let me know. I could create a DockerFile for this project if that would be of intrest.

shubhamwagh commented 5 years ago

Hi @grebtsew So I had a hard time in building and installing blender3d and bpy (pip install bpy doesn't work). But finally got it. I think I will document this if required. I faced a couple of problems and also I am new to Blender 3d so will appreciate any help possible.

 contours, hierarchy = cv2.findContours(detect_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

Traceback (most recent call last): File "/home/shubham/FloorplanToBlender3d/Blender/floorplan_to_3dObject_in_blender.py", line 240, in main(sys.argv) File "/home/shubham/FloorplanToBlender3d/Blender/floorplan_to_3dObject_in_blender.py", line 114, in main create_floorplan(base_path, program_path, i) File "/home/shubham/FloorplanToBlender3d/Blender/floorplan_to_3dObject_in_blender.py", line 175, in create_floorplan obj = create_custom_mesh(boxname + wallname, wall, faces, pos=pos, rot=rot) File "/home/shubham/FloorplanToBlender3d/Blender/floorplan_to_3dObject_in_blender.py", line 83, in create_custom_mesh myobject.data.materials.append(create_mat( np.random.randint(0, 40, size=3))) #add the material to the object File "/home/shubham/FloorplanToBlender3d/Blender/floorplan_to_3dObject_in_blender.py", line 90, in create_mat mat.diffuse_color = rgb_color #change to random color ValueError: bpy_struct: item.attr = val: sequences of dimension 0 should contain 4 items, not 3

It will be great if you can provide insight on this. I tired changing the size to **size=4** (not sure what it does but was just trying it) which gives me following error

Traceback (most recent call last): File "/home/shubham/FloorplanToBlender3d/Blender/floorplan_to_3dObject_in_blender.py", line 240, in main(sys.argv) File "/home/shubham/FloorplanToBlender3d/Blender/floorplan_to_3dObject_in_blender.py", line 114, in main create_floorplan(base_path, program_path, i) File "/home/shubam/FloorplanToBlender3d/Blender/floorplan_to_3dObject_in_blender.py", line 218, in create_floorplan obj = create_custom_mesh(cornername, verts, [faces], mat=create_mat((40,1,1))) File "/home/shubham/FloorplanToBlender3d/Blender/floorplan_to_3dObject_in_blender.py", line 90, in create_mat mat.diffuse_color = rgb_color #change to random color ValueError: bpy_struct: item.attr = val: sequences of dimension 0 should contain 4 items, not 3


When  I change it to this

obj = create_custom_mesh(cornername, verts, [faces], mat=create_mat((40,1,1, 1)))


the code runs and I do get the output floorplan.blend. But I am not sure what exactly those changes did to make it work.
shubhamwagh commented 5 years ago

When I try it with custom floorplan image, I get this error.

Traceback (most recent call last):
  File "/home/shubham/FloorplanToBlender3d/create_blender_project_from_floorplan.py", line 73, in <module>
    data_paths = [execution.simple_single(image_paths[0])]
  File "/home/shubham/FloorplanToBlender3d/FloorplanToBlenderLib/execution.py", line 23, in simple_single
    fpath, fshape = generate.generate_all_files(image_path, True)
  File "/home/shubham/FloorplanToBlender3d/FloorplanToBlenderLib/generate.py", line 42, in generate_all_files
    new_shape = generate_rooms_file(imgpath, info)
  File "/home/shubham/FloorplanToBlender3d/FloorplanToBlenderLib/generate.py", line 182, in generate_rooms_file
    return get_shape(verts, scale)
  File "/home/shubham/FloorplanToBlender3d/FloorplanToBlenderLib/generate.py", line 76, in get_shape
    low = posList[0]
IndexError: list index out of range

The image I used is this which I downloaded from the internet 2_avenida-sevellia-352-laguna-woods-ca-usa_0 (1)

It will be great if you can provide some insights on this.

grebtsew commented 5 years ago

Hi @shubhamwagh!

I have solved a couple of your problems and will build a DockerFile of this implementation for ubuntu:18.04. I will push the fixes later tonight.

Some details:

Hope this is somewhat useful! I'm sorry for any inconveniences. I will comment again when these fixes are released.

grebtsew commented 5 years ago

I pushed a new version now, had some struggles with the DockerFile. Currently I wouldn't recommend using the DockerFile, seems like there are some unconsistency between ubuntu blender and windows blender I will look into it on friday. Cheers!

shubhamwagh commented 5 years ago

Hi @grebtsew, I really appreciate your help. Thanks for trying it. If you need help on the installation of blender3d and bpy for your docker file, do let me know.

You were right, I rescaled the image and it worked perfectly fine. Thanks for pushing the new code.

Also, I was trying to get this output blender file exported to .obj file or any standard CAD file format using a python script. With some StackOverflow help, I managed to come up with this.

import bpy
import os

file_export_name='export.obj'

blend_file_path = bpy.data.filepath
directory = os.path.dirname(blend_file_path)
target_file = os.path.join(directory, file_export_name)

bpy.ops.export_scene.obj(filepath=target_file)

To execute the script I used your custom function

check_output([blender_install_path, "--background", "--python", "export_obj.py"])

The problem is when I view the export.obj file on Meshlab or any cad file viewer, it does not contain the 3d model from the floorplan but instead, it contains a default cube model. Do you know what is going wrong?

shubhamwagh commented 5 years ago

I think I got it. The startup blender file contains a cube by default. I have removed the cube and saved the scene as the default startup scene.

But I am still not able to export .blender model file to .obj file, it is empty now.

grebtsew commented 5 years ago

Hi again @shubhamwagh !

I worked out the issue with the DockerFile. For some reason I had to include the "-noaudio" tag in the blender command. Changing the command means changing how I handle argv argc in my script, so fixed that. I'll push this up shortly.

Regarding your question above. When you create a new empty blender project, the standard cube model will be there, so just remove it. As seen in my demo I just let them be there, I can remove it if would help.

The script you provided seems to work fine, make sure to reference the correct object (you might currently only be saving the cube object). I'll add the script as a file in /Blender folder. It can be tested by doing:

  1. Create a file containing the script
  2. open your example blender file
  3. open Text Editor View in blender
  4. Press alt- O or Text>Open in blender
  5. Run, for example by right clicking and press run script.

However I don't think this will work if you are trying to reformat or somehow get objects from another .blender file unless you provide a couple of parameters. For instance if you want to get an object from a blender file, you would probably need to add references to the blender file and the object you want to find before you save it. You can see an example of how to handle arguments in blender scripts in "Blender/floorplan_to_blender3DObject_in_blender.py". If my implementation is confusing let me know, and I can create some sort of object retrieval script.

Sidenote, while reading through your first question I forgot to mention that the bpy lib is not necessary as it is part of Blender and always used by Blender, never run in pure python.

Cheers!

shubhamwagh commented 5 years ago

Hi @grebtsew,

Thanks for your detailed response.

Thanks for clearing this confusion between bpy and Blender. Yeah, Initially I thought that with bpy I can completely render Blender 3d models from outside blender just using this library. That's why I was looking to get this bpy lib to get install on my python(3.7) virtualenv. When I was going through your code, I realized that it has to run with blender running in the background like in subprocess. And blender has its own python dist-packages so one can run a script from within Blender. And for one to run in pure oythonr one has to run the script by running Blender in bakcground.

I see that in docker file that you are using Blender 2.79 version. The one I am using is Blender 2.80. Some of the functions in your code have also changed which I am updating it accordingly. For example bpy.context.scene.objects.link(ob), it says scene does not have link attribute. So I had to replace it with this bpy.context.scene.collection.objects.link(ob) and like this there are couple of them.

When I mentioned I was not able to export it to ".obj", I was doing it from outside blender using subprocess which did not work. May I know how to get that working? It is still not clear to me how does one explicitly give reference to .blender file and export it to .obj file. It will be great if you can create a simple example for that.

Many thanks.

shubhamwagh commented 5 years ago

Hi! I think I got it, I was messing up with the directory. So the command is something like this

check_output([blender_install_path, "--background", program_path + "/floorplan.blend", "--python", program_path + "/export_obj.py"]).

Thanks for all your help. :D

grebtsew commented 5 years ago

Nice! You grasp fast! The "check_output" above was exactly what I meant!

I will add all required versions of libraries and programs in the readme file to avoid future similar problems.

Even if you solved it yourself I'll add a simple example of how to call the save_blenderfile_to_obj script from python.

I'm just happy to help! Let me know if you got any further questions.

I'll close this issue for now. :)

grebtsew commented 5 years ago

The example can be found in /Examples/Reformat_blender_to_obj.py, also see the blender script on path /Blender/blender_export_obj_script.py.