Isotropix Forums

Getting a project's materials

Clarisse Scripting related topics

Getting a project's materials

Unread postby gerdhofer » Wed Jul 17, 2019 4:33 pm

Hello,

Still trying to dig more into Python with Clarisse, but I'm really having some issues to find my way through the SDK and get what I'm lookign for. The SDK appears very technical and confusing for a beginner like me.

What I want to do is to
    collect all of a scene's materials
    find out if it's really a material
    get the types of the materials (dieletric, standard, diffuse, etc.)

Is this possible?

Thanks,
Gerd
gerdhofer
 
Posts: 41
Joined: Thu Aug 30, 2018 11:05 am

Re: Getting a project's materials

Unread postby dboude » Wed Jul 17, 2019 5:20 pm

Hi,

Here is a snippet that does what you're after

Code: Select all
materials = ix.api.OfObjectVector()
filter = "*"
type = ["Material"]
for i in type:
    ix.application.get_matching_objects(materials, filter, i)
   
for mat in materials:
    print mat
    print mat.get_class_name()


Cheers ;)
Démian
Isotropix
Technical Artist - Clarisse Specialist
User avatar
dboude
 
Posts: 1079
Joined: Mon Jul 03, 2017 10:51 am

Re: Getting a project's materials

Unread postby gerdhofer » Thu Jul 18, 2019 7:31 am

Thanks for your - as always - fast reply.

It's a bit off-topic now, but your code illustrates very well what I'm struggling with when it comes to Python in Clarisse: I often don't know which class I have to use, e.g. when is it "api", when "application"? I'm also wondering why some functions use upper case notation, while others are written in lower cases like "OfObjectVector" vs. "get_class_name()"? Another thing is that there are some inconsitencies: Why, for example is there a "SetValue" function, but no "GetValue" counterpart?

I'm just trying to understand the mechanisms behind Clarisse's Python to get a better understanding of how to use this feature. I think examples, covering common programming tasks, will be very helpful.

Best,
Gerd
gerdhofer
 
Posts: 41
Joined: Thu Aug 30, 2018 11:05 am

Re: Getting a project's materials

Unread postby dcourtois » Thu Jul 18, 2019 8:49 am

Hi,

OfObjectVector() is a constructor and as such is spelled as the class, and all classes use CamelCase notation.
get_class_name() is a method / function, and all functions use lowercase + underscores.

SetValues() and such is a special case. Those are commands that are pushed onto the execution stack and can be undone through ctrl+z. Don't ask me why they use CamelCase notation instead of lowercase+underscore, I've no idea :) But, this is why you don't have a corresponding GetValues(): it doesn't make any sense to push a "get" command into the execution stack; how do you get the result since execution is differed to the next event loop, undoing a "get" operation doesn't make any sense, etc.

Basically: if you want things to be undoable, only use commands. Otherwise, you can directly change things using the objects/attributes/whatever's available methods. And to query values, use the getter methods on objects, attributes, etc.

Regards,
Damien
User avatar
dcourtois
 
Posts: 38
Joined: Tue Jul 25, 2017 3:15 pm

Re: Getting a project's materials

Unread postby gerdhofer » Thu Jul 18, 2019 9:48 am

Thanks for the explanation Damien.

Most of what you're writing is pretty much beyond my current Python horizon, and this is basically the essence of my problems with Clarisse's Python. Please don't get me wrong: I really admire your capabilities and it's amazing what modern development is able to achieve, but maybe you demand a bit too much from beginners? To me as a user these subtle differences under the hood are not really important. It's more important to have an user-friendly access to the commands without having to browse huge amounts of SDK pages - and then I have to think about execution stacks... Once I've found something I could use, I still ask myself how to implement everything. Then I have to go through cryptic error messages, trying to find out what went wrong.

Therefore it would really be nice to have more examples from the real world. Most of the things I've read so far appear like from developers for developers. I read about "Asynchronous command execution", but I have no idea how to query all attributes and values of an object or material, for example. Of course you're all super helpful and your support is AAA, but there are things I just want to find out myself in order to get a better understanding. And that's often quite difficult and/or time consuming, because I can't see a real starting point.

I also don't want to start an endless discussion here, and I'm sure you understand what I'm saying here. I assume (or better: hope) that I'm not the only user struggling with Clarisse's Python, and I can imagine that they have very similar problems when getting started.

Thanks for listening ;)
gerdhofer
 
Posts: 41
Joined: Thu Aug 30, 2018 11:05 am

Re: Getting a project's materials

Unread postby dboude » Thu Jul 18, 2019 10:12 am

Yes I understand, I was and am like you in front of Clarisse Python's world. That's why I write in a text file all useful tips I found on the forum and reuse them for new scripts.

Remember that you have an auto-completion feature by pressing CTRL + Space while writing code in the script editor.

Hope this can help you.

Cheers ;)
Démian
Isotropix
Technical Artist - Clarisse Specialist
User avatar
dboude
 
Posts: 1079
Joined: Mon Jul 03, 2017 10:51 am

Re: Getting a project's materials

Unread postby gerdhofer » Thu Jul 18, 2019 10:42 am

Compiling a personal repository with scripts from the forum is definitely a very good idea. And, yes, I do remember the auto-completion and the Python commands in the log window. Both have been very helpful already.
gerdhofer
 
Posts: 41
Joined: Thu Aug 30, 2018 11:05 am

Re: Getting a project's materials

Unread postby dboude » Thu Jul 18, 2019 1:11 pm

Ok, feel free to ask if you encounter some blocking paths.

Cheers
Démian
Isotropix
Technical Artist - Clarisse Specialist
User avatar
dboude
 
Posts: 1079
Joined: Mon Jul 03, 2017 10:51 am

Re: Getting a project's materials

Unread postby gerdhofer » Fri Jul 19, 2019 7:15 am

There's one thing in my project I couldn't solve so far: is there a method to read out map files and texture nodes (e.g. saturation, hue, color, etc.) attached to a material? For example: I want to find out the name of a map file that is linked to a material's diffuse front color attribute.
gerdhofer
 
Posts: 41
Joined: Thu Aug 30, 2018 11:05 am

Re: Getting a project's materials

Unread postby dcourtois » Fri Jul 19, 2019 8:39 am

The following snippet can be used:

python code

front_color = ix.application.get_factory().find_attribute("project://scene/your_material.front_color")
if front_color.is_textured() is True:
texture = front_color.get_texture()
print(texture.get_attribute("filename").get_string())
User avatar
dcourtois
 
Posts: 38
Joined: Tue Jul 25, 2017 3:15 pm

Next

Return to Scripting
cron