when bind XCAFDoc_VisMaterialPBR to shape , Only paint part of workpiece when Export to gltf

Dear,
I am a newer to OCCT , I'm aming to convert "step" file to "gltf" file with material and colors directly via pythonocc. official RWGltf_CafWriter examples in pythonocc won't export material as default. after a lot of works ,I find one way to solve it. but I encounter a strange problem. the key point is add pbrmaterial to XCAFDoc_Document via XCAFDoc_DocumentTool_VisMaterialTool. below are my steps.
1. Get vmtool based on XCAFDoc_DocumentTool_VisMaterialTool
2. Create pbrmaterial and apply it to shape as below code,please see attachment

then i get a strang problem. only part of workpieces are painted as excepted. Is there any limits about PbrMaterial?

LeonBaoYang's picture

Sorry i missed the final gltf file, please see here attachment.

gkv311 n's picture

Before exporting result document into glTF - try displaying it in 3D viewer to see if materials are correctly applied. And please share code as text (forum supports formatting of source code), not as an image screenshot.

LeonBaoYang's picture

Hi, gkv311 n
Thanks a lot for your reply! I display the whole shapes in 3D view as you suggessed. then the more strange things happend. it looks like all materials havn't been applied to the shapes. but part materials are truly applied to these workpieces in the exported gltf file . beside I'm not sure about how to post code corretly in forum. Below are my all codes, i also post it as an attachment,


from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.TCollection import TCollection_ExtendedString, TCollection_AsciiString
from OCC.Core.XCAFDoc import (
XCAFDoc_DocumentTool_ShapeTool,
XCAFDoc_DocumentTool_LayerTool,
XCAFDoc_DocumentTool_VisMaterialTool,
XCAFDoc_DocumentTool_ColorTool,
XCAFDoc_VisMaterial,
XCAFDoc_VisMaterialPBR
)
from OCC.Core.TopAbs import TopAbs_SOLID, TopAbs_FACE, TopAbs_SHAPE, TopAbs_COMPOUND, TopAbs_COMPSOLID, TopAbs_SHELL, \
TopAbs_VERTEX, TopAbs_EDGE, TopAbs_WIRE
from OCC.Core.TColStd import TColStd_IndexedDataMapOfStringString
from OCC.Core.Message import Message_ProgressRange
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
from OCC.Extend.DataExchange import read_step_file_with_names_colors
# GLTF export
from OCC.Core.RWGltf import RWGltf_CafWriter, RWGltf_WriterTrsfFormat

from OCC.Core.Quantity import (
Quantity_Color,
Quantity_ColorRGBA,
Quantity_TOC_RGB,
Quantity_NOC_BLUE1
)
from OCC.Core.TCollection import TCollection_ExtendedString, TCollection_AsciiString
from OCC.Display.SimpleGui import init_display

display, start_display, add_menu, add_function_to_menu = init_display()

# create a document
doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc"))
shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
layer_tool = XCAFDoc_DocumentTool_LayerTool(doc.Main())
color_tool=XCAFDoc_DocumentTool_ColorTool(doc.Main())
vm_tool=XCAFDoc_DocumentTool_VisMaterialTool(doc.Main())

filename = "as1_pe_203.stp"
shapes_labels_colors = read_step_file_with_names_colors(filename)

id=0
for a_shape in shapes_labels_colors:
s, c = shapes_labels_colors[a_shape]

# Test Set Material
vismat= XCAFDoc_VisMaterial()
visMatPbr=XCAFDoc_VisMaterialPBR()
visMatPbr.BaseColor=Quantity_ColorRGBA(Quantity_Color(c.Red(),c.Green(),c.Blue(),Quantity_TOC_RGB))
visMatPbr.IsDefined=True
vismat.SetPbrMaterial(visMatPbr)
matlabel=vm_tool.AddMaterial(vismat,TCollection_AsciiString("mymat"+str(id)))
vm_tool.SetShapeMaterial(a_shape,matlabel)

# Test Add Color
color_tool.AddColor(Quantity_Color(c.Red(),c.Green(),c.Blue(),Quantity_TOC_RGB))
color_tool.SetInstanceColor(a_shape,0,Quantity_Color(c.Red(),c.Green(),c.Blue(),Quantity_TOC_RGB),True)
color_tool.SetInstanceColor(a_shape,1,Quantity_Color(c.Red(),c.Green(),c.Blue(),Quantity_TOC_RGB),True)
color_tool.SetInstanceColor(a_shape,2,Quantity_Color(c.Red(),c.Green(),c.Blue(),Quantity_TOC_RGB),True)

msh_algo = BRepMesh_IncrementalMesh()
msh_algo.SetShape(a_shape)
msh_algo.Perform()
shape_tool.AddShape(a_shape)
display.DisplayShape(a_shape)

id+=1
print(id)

start_display()
# GLTF options
a_format = RWGltf_WriterTrsfFormat.RWGltf_WriterTrsfFormat_Compact
force_uv_export = True

# metadata
a_file_info = TColStd_IndexedDataMapOfStringString()
a_file_info.Add(
TCollection_AsciiString("Authors"), TCollection_AsciiString("pythonocc")
)

#
# Binary export
#
binary = True
binary_rwgltf_writer = RWGltf_CafWriter(TCollection_AsciiString("box.glb"), binary)
binary_rwgltf_writer.SetTransformationFormat(a_format)
binary_rwgltf_writer.SetForcedUVExport(force_uv_export)
pr = Message_ProgressRange() # this is required
binary_rwgltf_writer.Perform(doc, a_file_info, pr)

#
# Ascii export
#
binary = False
ascii_rwgltf_writer = RWGltf_CafWriter(TCollection_AsciiString("box.gltf"), binary)
ascii_rwgltf_writer.SetTransformationFormat(a_format)
ascii_rwgltf_writer.SetForcedUVExport(force_uv_export)
pr = Message_ProgressRange() # this is required
ascii_rwgltf_writer.Perform(doc, a_file_info, pr)

class RWGltf_CafWriterNew(RWGltf_CafWriter):
def writeMaterials():
pass

Dmitrii Pasukhin's picture

Hello,

As I can understand, you iterate on existed color shape labels, that connected to the color. You create new document for this proposal. I think it can be a problem. You can destroy assemble tree and remove any attributes(names, metadata and other). I think the best way use document from read process and upgrade it. Moreover you can connect color or material to shape only after setting shape to the document (your call order is incorrect, you need to call AddShape before working with this label)*.
*This remark only for case when you create new document. In other cases you don't need AddShape for tesselating result, tesselator updates shapes directly you don't need do something after converting.

Best regards, Dmitrii.

LeonBaoYang's picture

Dear Dmitri
Thank you a lot for your great help. it works! As a conclusion, the shape object need to be added to the document before been applied the material.

gkv311 n's picture

Please use 'insert code' or Markdown editor on forum when sharing code to make it readable

LeonBaoYang's picture

Thank you very much, I will learn about how to post code later~