Sketchfab Forum

Default Background

feature-request

(Ian4) #1

Is it possible to have a default background colour for all uploads, e.g. #FFFFFF, as this would be a really usefull feature


#2

Not at the moment, but it's a good idea!

It is possible to set the background with the API, but not choose a default.


#3

After the model has been successfully processed, you need to send a PATCH request to

https://api.sketchfab.com/v2/models/MODEL_ID?token=API_TOKEN

With data like:

{
    "options": {
        "background": {
            "enable": "color", // or "fixed" or "environment" or "none"
            "color": [0, 0, 0], // RGB values
            "uid": "78fa317e46024a5283765aa34df5e508" // fixed image uid (must exist but does not matter if using color)
        }
    }
}

Please keep in mind that this part of the API is unstable and undocumented. V3 is coming soon and will be better supported.


(Reflection3dt) #4

Hey James,

A quick question for you. How can we get the uid of a fixed image or environment? I did try the one "78fa317e46024a5283765aa34df5e508" you provided and it worked and change my background to "Light Dirty Grey". However, when I am trying to find uid for others through the Swagger UI it all failed..
Thanks very much for your help!

Regards,

Tantan


#5

You can inspect the element in the editor and get the data-value, or use the API:

GET https://api.sketchfab.com/v3/me/backgrounds

By the way, you should move to V3 rather than V2 whenever possible. Full documentation here:

https://docs.sketchfab.com/data-api/v3/index.html


(Reflection3dt) #6

Hey James,

Thanks very much and it works perfectly with the V2 Data API. However, when I try to use the V3 API it does not work..

The following is my basic V2 code in Python to send the PATCH request and it works without any issue:

setting = {'options' : {'shading' : {'type' : 'shadeless','renderer' : 'classic'},'background': {'enable': 'fixed', 'uid': '9ef3e5b6af3440c8ade81d1e53654097'}}}
PATCH_URL='https://api.sketchfab.com/v2/models/{0}?token={1}'.format(MODEL_UID,SKETCHFAB_API_TOKEN)
headers={'content-type': 'application/json'}
requests.patch(PATCH_URL, headers=headers, data=json.dumps(setting))

The following is my basic V3 version trying to do the same thing but it does not work:

setting = {'shading' : {'type' : 'shadeless','renderer' : 'classic'},'background': {'enable': 'fixed', 'uid':'9ef3e5b6af3440c8ade81d1e53654097'}}
PATCH_URL='https://api.sketchfab.com/v3/models/{0}/options?token={1}'.format(MODEL_UID,SKETCHFAB_API_TOKEN)
headers={'content-type': 'application/json'}
requests.patch(PATCH_URL, headers=headers, data=json.dumps(setting))

Could you please help take a look at why the V3 one is not working?
Much appreciated!

Regards,

Tantan


(Reflection3dt) #7

Hey James,

Can you give an simple example of how we can using the V3 patch request? Trying to move to V3 but apparently examples are lacking..
Thanks.

Regards,

Tantan


(Reflection3dt) #8

Hey James,

In the below thread you were still recommending use V2 api for PATCH request. Is it still a valid recommendation at this moment?

I manage to have the V3 PATCH request to change either the background image or color and return Response 204 but nothing changes in the Viewer. However, while using the Swagger UI it worked.

Thanks.

Thanks.


#9

Hey,

In V3, you can't pass your API Token as a URL parameter like you did in V2. It needs to be in the headers:

Authorization: Token API_TOKEN


(Reflection3dt) #10

Hmm. I tried to create a dict class including my token and assigned to the header as follow

headers={'Authorization': 'Token {}'.format(SKETCHFAB_API_TOKEN)}
requests.patch(PATCH_URL, headers=headers, data=data)

However, I am starting to get 401.. Am I still missing anything?

Thanks James!


(Tribble42) #11

@reflection3dt hi there :slight_smile:

Here is a request that works


headers = {'Authorization': 'Token XXXX', 'content-type': 'application/json'} requests.patch('https://sketchfab.com/v3/models/XXXXX/options', headers=headers, data=json.dumps({'shading': 'lit'}))

Tell me if you have any more issues with this.

Best

Fred


(Reflection3dt) #12

Awesome. It works! All I was missing is the 'content-type': 'application/json' in headers. Thanks a lot!

However, once I am changing the background to dark color it is not working.. My code is:

headers = {'Authorization': 'Token XXXX', 'content-type': 'application/json'}
requests.patch('https://sketchfab.com/v3/models/XXXXX/options', headers=headers, data=json.dumps({'background' : {"color": "#000000"}}))

The error comes back with 400 messages:

"detail":{"background":["Options key background is not json"]}}'

Could you please confirm my code is correct?

Much appreciated!


(Tribble42) #13

@reflection3dt This was a tricky one, I agree it's not obvious. I'll add an example in the documentation for these calls.

Here is a request that worls:


requests.patch('https://sketchfab.com/v3/models/XXXX/options', data=json.dumps({'background': '{"color": "#000000"}'}), headers=headers)

Don't hesitate to ask if you have further issues

Best,

Fred


(Avinash Srivastava) #14

Hi Tribble,

I am facing similar issue. I am using javascript to call the api but everytime i am getting error as "Authentication credentials were not provided" even though i am passing token to header. Same token is working for another api i.e. /v3/models/{uid} but in case of calling "/v3/models/{uid}/options" api, i am getting authorization error. I am pasting my code snippet for your reference. Please check and let me know if i am missing something in the code.

Code Snippet:

var options = {'background': '{"color": "#ffffff"}'};
var modelId = 'fc018a19e5044aabab562464269464a0';
var data = JSON.stringify(options);

    $.ajax({
        url: 'https://api.sketchfab.com/v3/models/' + modelId + '/options',
        ajaxSettings: { headers: {'Authorization': 'Token XXX'}},
        data: data,
        type: 'PATCH',
        contentType: 'application/json',
        traditional: true,

        success: function( response ) {
            console.log( 'PATCH successful: ' + modelId );
        },

        error: function( response ) {
            console.log( 'Description PATCH failed: ' + response );
        }
    });

Please help.


(Tribble42) #15

Hi,

I think your code works for /v3/models/uid because this endpoint accepts both a formData and JSON payload and somehow passes validation.

The options endpoint is stricly limited to json, and it seems like there's an issue with ajaxSettings. I managed to make it work with the following sample:

$.ajax({
    url: 'https://api.sketchfab.com/v3/models/' + modelId + '/options',
    data: JSON.stringify({'background': '{"color": "#ffffff"}'}),
    type: 'PATCH',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Token XXXX'
    },
    traditional: true,

    success: function( response ) {
        console.log( 'PATCH successful: ' + modelId );
    },

    error: function( response ) {
        console.log( 'Description PATCH failed: ' + response );
    }
});

Best,

Fred


(Avinash Srivastava) #16

Hi Tribble,

Thanks a lot. Its working now. I had also posted an issue regarding orientation of my 3D model which i have posted to James. Seems he couldn't get time to look into this issue. I am sharing that post link with you. I would request you to please have a look into this issue and help me out.

Your quick response would be highly appreciated.

Thanks
Avinash