Lighting and material settings via API?


(Xiuming Zhang) #1

1. Lighting settings via API

By adapting this script, I am able to change the background of my model to #111111 via the API. The setting snippet looks like this.

settings = {
    'background': '{"color": "#111111"}',
    'environment': '{"uid": "df380da788ee444885722735039b0c09"}
}

As you can see, in the meanwhile, I am also trying to fix on one HDRI for environment lighting, but it's not working. Specifically, I want the Studio HDRI as my environment lighting. What am I doing wrong here? I got the UID by inspecting the HTML source of a model that has the Studio HDRI.

2. Material settings via API

I also want to be able to change material settings via API in the same way. Specifically, I want to turn on all the PBR maps (actually, I am not sure why some of them, e.g., normal map, are turned off by default, especially given that I've followed Sketchfab's naming conventions such that the maps have been automatically assigned to the corresponding PBR slots).

I believe it's possible, since I've successfully changed the background via the API. Just patching the following doesn't have any effects.

settings = {
    'NormalMap': '{"enable": "true"}'
}

I kinda expect this not to work, as I have two objects with two materials, which makes it impossible for the program to know for which material I want to turn on the normal map, but how can I fix this?

Many thanks in advance for the thoughts!


Post-Processing and Material Presets
#2

I recommend inspecting the network requests when you Save Settings in the editor. This will give you the proper JSON object structure and parameter names.


(Xiuming Zhang) #3

Thanks James for the advice!

Just want to make sure this is theoretically possible before I start hacking it. I can imagine if you guys forbid such changes via the API, there will be no way for me to hack through.


#4

Most things are possible, just keep in mind that it's not officially supported, and if we add an option to the editor / object to the JSON, it will break your request.


(Xiuming Zhang) #5

I am inspecting my network requests with Chrome's developer tool.

When I rotate my scene, I do see new network requests being made. However, when I touch the settings, no network requests show up (notice the list on the right remains empty no matter what settings I make). Any ideas why?

Thanks a lot.


(Xiuming Zhang) #6

Inspecting the HTML, I found 'background': '{"color": "#111111"}' gets patched correctly to

prefetchedData[ "/i/models/d27c7447486b460fb20677b6f4d9cd5c" ] = {
...

            "environment": {"backgroundExposure": 1, "enable": true, "uid": "d348dfd2a8104ab0b8528f885d645eb3", "shadowEnable": false, "blur": 0.1, "shadowBias": 0.005, "rotation": 0, "exposure": 2.1402}, "vr": {"floorHeight": 13.491239980506897, "scale": 0.9068293352733361, "initialCamera": {"position": [0.08103972646236413, -2.0762001369882364, 13.491239980506897], "rotation": [0, 0, 0, 1]}},
...
            "background": {"color": [0.06666666666666667, 0.06666666666666667, 0.06666666666666667], "enableVR": false, "enable": "color", "uid": "51af6a870cce449eb75b0345feebaebb"},
...};

The environment line is in the same level of hierarchy as background, I am so confused why background line can get patched, but 'environment': '{"uid": "df380da788ee444885722735039b0c09"} can't.

Thanks for your advice in advance. @james


#7

I meant to choose all the settings you want, then hit Save Settings and look at the PATCH payload.


(Xiuming Zhang) #8

Thanks for the explanation! @james

I found the PATCH payload using one model, and then I quickly tried patching it to my other model. It says "Patch successful", but none of the settings are taking effects.

I understand that not all settings will take effects, as some of them are object/model-specific (tagged with id's). I definitely need to replace the id's in my payload template with the real id's in my model.

But shouldn't those non-model-specific settings (such as background, which doesn't have model-specific id's associated with it) work?


#9

Yes, scene settings should not matter. Only materials have model-specific IDs.

Could you post your script so I can take a look?


(Xiuming Zhang) #10

@james Sure! Here are the relevant snippets.

import sketchfab_v3_upload as sf
...
def setup(UID, settingFile, token):
    settings = readSettings(settingFile)
    modelURI = 'https://api.sketchfab.com/v3/models/%s' % UID
    # Wait for the model to be processed
    if sf.poll_processing_status(modelURI):
        sf.patch_model_options(modelURI, settings) # authentification provided in function

sketchfab_v3_upload is just https://gist.github.com/fredericcambon/24326cadf3edad96839d4ec701925960#file-sketchfab-v3-upload-py

The contents of the settingFile are

{"options":{"version":2,"background":{"enable":"color","enableVR":false,"uid":"51af6a870cce449eb75b0345feebaebb","color":[0.06666666666666667,0.06666666666666667,0.06666666666666667]},"environment":{"uid":"d348dfd2a8104ab0b8528f885d645eb3","enable":false,"exposure":2.1402,"rotation":0,"blur":0.1,"backgroundExposure":1,"shadowEnable":false,"shadowBias":0.005},"lighting":{"enable":true,"lights":[{"type":"DIRECTION","enable":true,"color":[0.7806122448979592,0.7985214493960848,1],"intensity":0.96,"ground":[0.3,0.2,0.2],"intensityGround":1,"matrix":[-0.9815804500275482,0.1909922167095499,-0.004668327333101102,0,0.13548347102422797,0.7131126062169096,0.6878332937084056,0,0.13469984856318096,0.6745312327912614,-0.7258536813891331,0,0.2764793650767136,1.078885802792896,13.000759350324298,1],"falloff":1.187541845331568,"attachedToCamera":true,"angle":45,"hardness":0.5,"castShadows":true,"shadowBias":0.005,"shadowSize":1024},{"type":"DIRECTION","enable":true,"color":[1,0.8783319450229073,0.7091836734693877],"intensity":1,"ground":[0.3,0.2,0.2],"intensityGround":1,"matrix":[0.7755211495104662,-0.0666602860002539,0.6277924441503967,0,0.2690483737361956,0.9344846523281437,-0.233133882464682,0,-0.5711216326246741,0.3497067926613838,0.7426474533145686,0,-0.747616034294845,0.6075892729952792,15.131447202854693,1],"falloff":1.187541845331568,"attachedToCamera":true,"angle":45,"hardness":0.5,"castShadows":true,"shadowBias":0.005,"shadowSize":1024},{"type":"HEMI","enable":true,"color":[0.423469387755102,0.7352665556018328,1],"intensity":0.12,"ground":[0.9642857142857143,0.6484128636877489,0.2607507288629738],"intensityGround":0.06,"matrix":[0.6903299131022926,-0.7076363669144057,-0.15064986988498263,0,-0.011089698239978026,0.19785137301620165,-0.9801692980238416,0,0.7234097246160907,0.6783108479272498,0.12873524736872857,0,1.1306543819885668,1.0843697482030679,14.240705446640208,1],"falloff":1.187541845331568,"attachedToCamera":false,"angle":45,"hardness":0.5,"castShadows":true,"shadowBias":0.005,"shadowSize":1024}]},"scene":{"fov":1,"nearFarRatio":0.005,"postProcess":{"enable":true,"grain":{"enable":false,"animated":false,"factor":0.15},"sharpen":{"enable":false,"factor":0.2},"chromaticAberration":{"enable":false,"factor":0.05},"vignette":{"enable":false,"lens":[0.8,0.25]},"bloom":{"enable":false,"factor":0.5,"threshold":0,"radius":0.7},"toneMapping":{"enable":false,"method":"default","exposure":1,"brightness":0,"contrast":0,"saturation":1},"colorBalance":{"enable":false,"low":[0,0,0],"mid":[0,0,0],"high":[0,0,0]},"dof":{"enable":false,"blurNear":0.5,"blurFar":0.5,"focusPoint":[0,0,0]},"ssao":{"enable":false,"bias":0.009068293352733361,"intensity":0.5,"radius":0.04534146676366681}}},"materials":{"d47e69e8-04c7-4b9a-a025-c86be71f6bb7":{"version":3,"stateSetID":0,"shadeless":false,"id":"d47e69e8-04c7-4b9a-a025-c86be71f6bb7","name":"human.001_0","cullFace":"DISABLE","reflection":0.1,"channels":{"DiffuseColor":{"color":[1,1,1],"factor":1,"enable":true},"DiffuseIntensity":{"color":[1,1,1],"factor":1,"enable":true},"SpecularColor":{"color":[0.5,0.5,0.5],"factor":0,"enable":true},"SpecularHardness":{"color":[1,1,1],"factor":12.5,"enable":true},"Displacement":{"color":[1,1,1],"factor":0,"enable":true},"NormalMap":{"color":[1,1,1],"factor":1,"enable":false,"flipY":true},"BumpMap":{"color":[1,1,1],"factor":1,"enable":false},"Opacity":{"color":[1,1,1],"factor":1,"enable":true,"type":"alphaBlend","invert":false},"EmitColor":{"color":[1,1,1],"factor":0,"enable":true,"type":"additive"},"DiffusePBR":{"color":[1,1,1],"factor":1,"enable":true},"AlbedoPBR":{"color":[1,1,1],"factor":1,"enable":false},"SpecularPBR":{"color":[1,1,1],"factor":0.05,"enable":true},"GlossinessPBR":{"color":[1,1,1],"factor":0.4,"enable":false},"RoughnessPBR":{"color":[1,1,1],"factor":0.6,"enable":true},"MetalnessPBR":{"color":[1,1,1],"factor":0,"enable":false},"SpecularF0":{"color":[1,1,1],"factor":0.5,"enable":false},"AOPBR":{"color":[1,1,1],"factor":1,"enable":false,"occludeSpecular":false},"CavityPBR":{"color":[1,1,1],"factor":1,"enable":false}}},"8d06432a-9927-4748-982c-d1b31d85d2ed":{"version":3,"stateSetID":1,"shadeless":false,"id":"8d06432a-9927-4748-982c-d1b31d85d2ed","name":"Material.001","cullFace":"DISABLE","reflection":0.1,"channels":{"DiffuseColor":{"color":[1,1,1],"factor":1,"enable":true},"DiffuseIntensity":{"color":[1,1,1],"factor":1,"enable":true},"SpecularColor":{"color":[0.5,0.5,0.5],"factor":0,"enable":true},"SpecularHardness":{"color":[1,1,1],"factor":12.5,"enable":true},"Displacement":{"color":[1,1,1],"factor":0,"enable":true},"NormalMap":{"texture":{"magFilter":"LINEAR","minFilter":"LINEAR_MIPMAP_LINEAR","wrapS":"REPEAT","wrapT":"REPEAT","textureTarget":"TEXTURE_2D","internalFormat":"RGB","texCoordUnit":0,"uid":"251c0badfd924b8a8cb08c087e6da38d"},"factor":1,"enable":true,"flipY":true},"BumpMap":{"color":[1,1,1],"factor":1,"enable":false},"Opacity":{"color":[1,1,1],"factor":1,"enable":true,"type":"alphaBlend","invert":false},"EmitColor":{"color":[1,1,1],"factor":0,"enable":true,"type":"additive"},"DiffusePBR":{"texture":{"magFilter":"LINEAR","minFilter":"LINEAR_MIPMAP_LINEAR","wrapS":"REPEAT","wrapT":"REPEAT","textureTarget":"TEXTURE_2D","internalFormat":"RGB","texCoordUnit":0,"uid":"8eeb9ad270f444c192548d8a8e5a6976"},"factor":1,"enable":true},"AlbedoPBR":{"color":[1,1,1],"factor":1,"enable":false},"SpecularPBR":{"texture":{"magFilter":"LINEAR","minFilter":"LINEAR_MIPMAP_LINEAR","wrapS":"REPEAT","wrapT":"REPEAT","textureTarget":"TEXTURE_2D","internalFormat":"RGB","texCoordUnit":0,"uid":"aa3aa7c807424fe09f6de04420200b45"},"factor":1,"enable":true},"GlossinessPBR":{"texture":{"magFilter":"LINEAR","minFilter":"LINEAR_MIPMAP_LINEAR","wrapS":"REPEAT","wrapT":"REPEAT","textureTarget":"TEXTURE_2D","internalFormat":"LUMINANCE","texCoordUnit":0,"uid":"e26b58d50de146909143ca2fb94ce1f4"},"factor":1,"enable":true},"RoughnessPBR":{"color":[1,1,1],"factor":0.20000000298023224,"enable":false},"MetalnessPBR":{"color":[1,1,1],"factor":0,"enable":false},"SpecularF0":{"color":[1,1,1],"factor":0.5,"enable":false},"AOPBR":{"color":[1,1,1],"factor":1,"enable":true,"occludeSpecular":false},"CavityPBR":{"color":[1,1,1],"factor":1,"enable":true}}}},"orientation":{"matrix":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]},"camera":{"position":[-0.11948612417396776,-3.244330914375282,-200.12585703226537],"target":[0.015371265170614823,0.11829936504364014,14.100300312042236]},"animation":{"order":[],"speed":1,"cycleMode":"one","autoplay":false},"hotspot":{"hotspots":[]},"vr":{"scale":0.9068293352733361,"initialCamera":{"position":[0.08103972646236413,-2.0762001369882364,13.491239980506897],"rotation":[0,0,0,1]},"floorHeight":13.491239980506897},"shading":{"pointSize":2,"type":"lit","renderer":"pbr","vertexColor":{"enable":false,"useAlpha":false,"colorSpace":"srgb"}},"wireframe":{"enable":false,"color":"000000FF"}}}

Thanks again!


#11

The settingFile is really big. Do you need to patch all of that? materials, lights, VR, wireframe, animation etc.? You can remove any unneeded children of options and the patch should still work.


(Xiuming Zhang) #12

Right, there are redundant options that I can remove. Do you think settingFile being too big might cause the issue? I will try a smaller settingFile.


#13

I don't think the size is causing it to fail, but there's no need to have them in there if you're not using them and it might help debug.

It's really strange if the PATCH returned success but the options didn't change. You're sure your model UID was correct?


(Xiuming Zhang) #14

@james

Just checked and tried one more time. Yes, the UID is correct. Here is the output when I patch the model.

Start polling processing status for model   
Try polling processing status (attempt #0) ... 
Processing successful. Check your model here: https://api.sketchfab.com/v3/models/379849f211894456994252bfc7611e0f             
PATCH options successful.

(Sandlerseating) #15

I've got a similar thing working and was able to PATCH pretty much everything I needed to.

Though I only got it working by using API V2 - https://api.sketchfab.com/v2/models/


#16

Ah yes of course. I wasn't thinking. Most of these things have not been implemented in V3 yet.


(Xiuming Zhang) #17

Many thanks for this tip! @sandlerseating

Let me try V2 and report back if it's working. :slight_smile: @james


(Sandlerseating) #18

Appreciate you guys left them open in API V2, really helped us a lot last week.

We've used this knowledge with caution and always do tests before any looping through in bulk :slight_smile:


(Xiuming Zhang) #19

I second that. Please keep API V2 open until V3 can support everything V2 does. Thanks!


(Xiuming Zhang) #20

And V2 indeed works!

For future readers' reference, I used what @james wrote in another post:

import requests
import json

headers={'content-type': 'application/json'}
data = {'options' : {'shading' : {'type' : 'shadeless','renderer' : 'classic'}}}

r = requests.patch('https://api.sketchfab.com/v2/models/MODEL_UID?token=API_TOKEN', headers=headers, data=json.dumps(data))

print r.status_code

Thanks @sandlerseating again for pointing me to the deprecated (but working!) V2.