X3D Import does not handle Materials #71232

Closed
opened 2019-10-30 23:18:15 +01:00 by Jose Xavier · 26 comments

System Information
Operating system: Windows 10
Graphics card: GT 1070

Blender Version
Broken: 2.90, 0330d1af29c0

Short description of error
When an X3D file is imported and that file has a Material tag no corresponding material is added in blender. Materials are supported for other formats like OBJ.

Example:

   <Material DEF="MA_Material"  
        diffuseColor="1.000 0.0 0.0"
        specularColor="0.401 0.401 0.401"
        emissiveColor="0.000 0.000 0.000"
        ambientIntensity="0.000"
        shininess="0.500"
        transparency="0.0"/>

Exact steps for others to reproduce the error

  • File -> New -> General
  • remove the existing cube
  • File -> Import -> X3D ...
  • Select the imported cube (note the color is still the default gray)
  • Select the cube, look at its material, there is no material.

red-cube.x3d

red-cube.blend

Material import was more or less disabled in e8da70ab73

**System Information** Operating system: Windows 10 Graphics card: GT 1070 **Blender Version** Broken: 2.90, 0330d1af29c0 **Short description of error** When an X3D file is imported and that file has a Material tag no corresponding material is added in blender. Materials are supported for other formats like OBJ. Example: ``` <Material DEF="MA_Material" diffuseColor="1.000 0.0 0.0" specularColor="0.401 0.401 0.401" emissiveColor="0.000 0.000 0.000" ambientIntensity="0.000" shininess="0.500" transparency="0.0"/> ``` **Exact steps for others to reproduce the error** * File -> New -> General * remove the existing cube * File -> Import -> X3D ... * Select the imported cube (note the color is still the default gray) * Select the cube, look at its material, there is no material. [red-cube.x3d](https://archive.blender.org/developer/F8954834/red-cube.x3d) [red-cube.blend](https://archive.blender.org/developer/F8954831/red-cube.blend) Material import was more or less disabled in e8da70ab73
Author

Added subscriber: @jose.xavier

Added subscriber: @jose.xavier

#81519 was marked as duplicate of this issue

#81519 was marked as duplicate of this issue

blender/blender#81437 was marked as duplicate of this issue

blender/blender#81437 was marked as duplicate of this issue
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'
Member

Hi there and thx for the report (sorry this has been lying around).

Possibly normals are inverted, but to really investigate, could you share one of those .wrl files so we can check?

Hi there and thx for the report (sorry this has been lying around). Possibly normals are inverted, but to really investigate, could you share one of those .wrl files so we can check?

Added subscriber: @easyw

Added subscriber: @easyw

@lichtwerk
you can test any of the KiCAD 3D model wrl files...
I used to import those files in Blender and it worked fine till 2.7.9
With Blender 2.8 the wrl just loses colors.
(tested with blender-2.81a-linux-glibc217-x86_64)
i.e. you can try this file
https://github.com/KiCad/kicad-packages3D/raw/master/Capacitor_SMD.3dshapes/CP_Elec_10x10.5.wrl
or
https://raw.githubusercontent.com/KiCad/kicad-packages3D/master/Capacitor_Tantalum_SMD.3dshapes/CP_EIA-7343-30_AVX-N.wrl

@lichtwerk you can test any of the KiCAD 3D model wrl files... I used to import those files in Blender and it worked fine till 2.7.9 With Blender 2.8 the wrl just loses colors. (tested with blender-2.81a-linux-glibc217-x86_64) i.e. you can try this file https://github.com/KiCad/kicad-packages3D/raw/master/Capacitor_SMD.3dshapes/CP_Elec_10x10.5.wrl or https://raw.githubusercontent.com/KiCad/kicad-packages3D/master/Capacitor_Tantalum_SMD.3dshapes/CP_EIA-7343-30_AVX-N.wrl
Member

Changed status from 'Needs User Info' to: 'Needs Developer To Reproduce'

Changed status from 'Needs User Info' to: 'Needs Developer To Reproduce'

Added subscriber: @adammak

Added subscriber: @adammak

The cause of that is return in 2699th line of code in import_x3d.py which returns the method. Moreover, the color vectors need alpha value to be added.
I submitted bug, there is temporary fix you can use:

def appearance_CreateMaterial(vrmlname, mat, ancestry, is_vcol):
    bpymat = bpy.data.materials.new(vrmlname)
    diff_color = mat.getFieldAsFloatTuple('diffuseColor', [0.5, 0.5, 0.5, 1], ancestry)
    
    if(len(diff_color) == 3):
        # .wrl does not have alpha, we need to add 4th element
        diff_color = diff_color+[1]
    bpymat.diffuse_color = diff_color

    spec_color = mat.getFieldAsFloatTuple('specularColor', [0.0, 0.0, 0.0, 1], ancestry)

    if(len(spec_color) == 3):
        # .wrl does not have alpha, we need to add 4th element
        spec_color = spec_color + [1]
    bpymat.specular_color = diff_color

    if False and is_vcol:
        bpymat.use_vertex_color_paint = True
    return bpymat
The cause of that is return in 2699th line of code in import_x3d.py which returns the method. Moreover, the color vectors need alpha value to be added. I submitted bug, there is temporary fix you can use: ``` def appearance_CreateMaterial(vrmlname, mat, ancestry, is_vcol): bpymat = bpy.data.materials.new(vrmlname) diff_color = mat.getFieldAsFloatTuple('diffuseColor', [0.5, 0.5, 0.5, 1], ancestry) if(len(diff_color) == 3): # .wrl does not have alpha, we need to add 4th element diff_color = diff_color+[1] bpymat.diffuse_color = diff_color spec_color = mat.getFieldAsFloatTuple('specularColor', [0.0, 0.0, 0.0, 1], ancestry) if(len(spec_color) == 3): # .wrl does not have alpha, we need to add 4th element spec_color = spec_color + [1] bpymat.specular_color = diff_color if False and is_vcol: bpymat.use_vertex_color_paint = True return bpymat ```
Member

Added subscriber: @ankitm

Added subscriber: @ankitm

Added subscriber: @mont29

Added subscriber: @mont29

Note that proper solution would be to add support for shader nodes, but that is a fairly big project...

Note that proper solution would be to add support for shader nodes, but that is a fairly big project...

Added subscriber: @khavin

Added subscriber: @khavin

Any update on this? It may be something else, but I'm still experiencing the lack of colors on 2.90.1 running on Windows 10 2004. The .wrl was generated by KiCAD 5.1.6.

Any update on this? It may be something else, but I'm still experiencing the lack of colors on 2.90.1 running on Windows 10 2004. The .wrl was generated by KiCAD 5.1.6.
Member

Changed status from 'Needs Developer To Reproduce' to: 'Confirmed'

Changed status from 'Needs Developer To Reproduce' to: 'Confirmed'
Philipp Oeser changed title from Import WRL shows no color to X3D Import does not handle Materials 2020-10-05 10:19:41 +02:00
Member

Added subscriber: @the_magnet_girl

Added subscriber: @the_magnet_girl
Member

Added subscriber: @Justin.kedl

Added subscriber: @Justin.kedl

Added subscriber: @marioamb

Added subscriber: @marioamb

Added subscriber: @jr-br

Added subscriber: @jr-br

Thanks a lot for this workaround, @adammak!
I fixed two little issues for myself when I applied it, one is that your code assigened diff_color to bymat.specular_color instead of spec_color, and the second is that that field does not seem to like the alpha value:

    bpymat.specular_color = diff_color
ValueError: bpy_struct: item.attr = val: sequences of dimension 0 should contain 3 items, not 4

... which makes it:

def appearance_CreateMaterial(vrmlname, mat, ancestry, is_vcol):
    bpymat = bpy.data.materials.new(vrmlname)
    diff_color = mat.getFieldAsFloatTuple('diffuseColor', [0.5, 0.5, 0.5, 1], ancestry)

    if(len(diff_color) == 3):
        # .wrl does not have alpha, we need to add 4th element
        diff_color = diff_color+[1]
    bpymat.diffuse_color = diff_color

    spec_color = mat.getFieldAsFloatTuple('specularColor', [0.0, 0.0, 0.0], ancestry)

    bpymat.specular_color = spec_color

    return bpymat

I am currently using Blender 2.90.1.

In #71232#929226, @adammak wrote:
The cause of that is return in 2699th line of code in import_x3d.py which returns the method. Moreover, the color vectors need alpha value to be added.
I submitted bug, there is temporary fix you can use:

def appearance_CreateMaterial(vrmlname, mat, ancestry, is_vcol):
    bpymat = bpy.data.materials.new(vrmlname)
    diff_color = mat.getFieldAsFloatTuple('diffuseColor', [0.5, 0.5, 0.5, 1], ancestry)
    
    if(len(diff_color) == 3):
        # .wrl does not have alpha, we need to add 4th element
        diff_color = diff_color+[1]
    bpymat.diffuse_color = diff_color

    spec_color = mat.getFieldAsFloatTuple('specularColor', [0.0, 0.0, 0.0, 1], ancestry)

    if(len(spec_color) == 3):
        # .wrl does not have alpha, we need to add 4th element
        spec_color = spec_color + [1]
    bpymat.specular_color = diff_color

    if False and is_vcol:
        bpymat.use_vertex_color_paint = True
    return bpymat
Thanks a lot for this workaround, @adammak! I fixed two little issues for myself when I applied it, one is that your code assigened diff_color to bymat.specular_color instead of spec_color, and the second is that that field does not seem to like the alpha value: ``` bpymat.specular_color = diff_color ValueError: bpy_struct: item.attr = val: sequences of dimension 0 should contain 3 items, not 4 ``` ... which makes it: ``` def appearance_CreateMaterial(vrmlname, mat, ancestry, is_vcol): bpymat = bpy.data.materials.new(vrmlname) diff_color = mat.getFieldAsFloatTuple('diffuseColor', [0.5, 0.5, 0.5, 1], ancestry) if(len(diff_color) == 3): # .wrl does not have alpha, we need to add 4th element diff_color = diff_color+[1] bpymat.diffuse_color = diff_color spec_color = mat.getFieldAsFloatTuple('specularColor', [0.0, 0.0, 0.0], ancestry) bpymat.specular_color = spec_color return bpymat ``` I am currently using Blender 2.90.1. > In #71232#929226, @adammak wrote: > The cause of that is return in 2699th line of code in import_x3d.py which returns the method. Moreover, the color vectors need alpha value to be added. > I submitted bug, there is temporary fix you can use: > > > ``` > def appearance_CreateMaterial(vrmlname, mat, ancestry, is_vcol): > bpymat = bpy.data.materials.new(vrmlname) > diff_color = mat.getFieldAsFloatTuple('diffuseColor', [0.5, 0.5, 0.5, 1], ancestry) > > if(len(diff_color) == 3): > # .wrl does not have alpha, we need to add 4th element > diff_color = diff_color+[1] > bpymat.diffuse_color = diff_color > > spec_color = mat.getFieldAsFloatTuple('specularColor', [0.0, 0.0, 0.0, 1], ancestry) > > if(len(spec_color) == 3): > # .wrl does not have alpha, we need to add 4th element > spec_color = spec_color + [1] > bpymat.specular_color = diff_color > > if False and is_vcol: > bpymat.use_vertex_color_paint = True > return bpymat > ```

Added subscriber: @RigoLigo

Added subscriber: @RigoLigo

I tried to port appearance_CreateMaterial and appearance_CreateDefaultMaterial to shader nodes and seem to work fine. I'll see if I can submit the patch after taking some time refining it...

Basically tuple size can be solved by expanding it with (*diff_color, 1.0);

By setting bpymat.use_nodes to True enables node tree, then you can modify the nodes with something like bpymat.node_tree.nodes['Principled BSDF'].inputs['Base Color'].default_value = (*diff_color, 1.0).

Haven't tried to port image textures yet, but should be pretty trivial to add, as linking node tree is also simple.

I tried to port `appearance_CreateMaterial` and `appearance_CreateDefaultMaterial` to shader nodes and seem to work fine. I'll see if I can submit the patch after taking some time refining it... Basically tuple size can be solved by expanding it with `(*diff_color, 1.0)`; By setting `bpymat.use_nodes` to `True` enables node tree, then you can modify the nodes with something like `bpymat.node_tree.nodes['Principled BSDF'].inputs['Base Color'].default_value = (*diff_color, 1.0)`. Haven't tried to port image textures yet, but should be pretty trivial to add, as linking node tree is also simple.

This issue was referenced by 0573bc7dae

This issue was referenced by 0573bc7daeb1e5fc1e0d0e6a37c5efc043aebb13

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Bastien Montagne self-assigned this 2021-10-27 12:27:51 +02:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
12 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#71232
No description provided.