Closed shubhamwagh closed 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.
Opencv function problem
img, contours, hierarchy = cv2.findContours(detect_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)
This was pretty easy. I think this was due to different opencv version. So where ever there is use of cv2.findContours functions, I removed img as below
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
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
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.
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
It will be great if you can provide some insights on this.
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:
Concerning installation. I uninstalled my implementation to reinstall it. And noticed that the requirement.txt file was completely wrong, I'm sorry about that, it will be fixed now.
Concerning opencv-python. I experienced the same error you got during execution. It's related to different opencv versions just as you suggest. I have for now set a fixed version to opencv-python==3.4.1.15. However if you want to use later versions of opencv you will have to edit the code some just as you suggest. I can change this too if you want.
Concerning bpy and blender3d problems. I could not recreate this problem so this might be a blender3d version problem or OS related, hopefully my dockerfile will fix this.
Concerning your example floorplan and error. The error you got happens if my program can't detect any rooms in the floorplan, resulting in an empty array. I fixed this error so program won't crash but you will get a quite bad result. The problem is that this program can't detect stuff in your example image due to the image size. How to solve this problem? As I see it, you have 2 alternatives, and asking me to develop some autoscale as a third alternative, this might take a while though since I'm currently mainly working on other projects.
Hope this is somewhat useful! I'm sorry for any inconveniences. I will comment again when these fixes are released.
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!
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?
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.
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:
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!
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.
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
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. :)
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.
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.