Page 1 of 2
get texture script
Posted: 25 Oct 2012, 13:23
by caledonian_tartan
dear community
i have the follwing problem i dont know how to solve...
i have 1000 objects, each with its own material with a texture. now i would like to have the same texture as texturemap,
so i can work with just one shader and pipe the texture information via "color map lookup" into the shader.
now, how the heck can i create a texturemap for every object and get automatic the right texture i use in the existing shader as the texture in the texturemap-property?
searched the hole web, nothing... so a script might be the path, but... im not able to do it, my scripting abilities are to poor...
any help is very welcome!
thank you very much
Re: get texture script
Posted: 25 Oct 2012, 13:47
by EricTRocks
This question is sitting on the fence so I'll ask you to clarify your question with a reminder to read this before posting in this forum:
viewtopic.php?f=16&t=2532
You need to be asking specific programming questions and posting code that you are attempting to put together to complete the task you have in front of you. This is not a forum to ask someone to write tools for you.
Where are you stuck programming-wise? Do you know how to create a texture map via scripting on all 1000 objects? Do you need to know how to look up the image clip used in the shader to then use the same clip in the texture map?
Other moderator edit: A bit harsh maybe?
As a moderator you might also want to read this post again... - HB
Re: get texture script
Posted: 25 Oct 2012, 14:05
by caledonian_tartan
you're right eric, i'm sorry
i was a bit in a rush...
so here's what i have so far (in Python)
here's the code logged when connecting a texture to the image node:
Code: Select all
Application.SIConnectShaderToCnxPoint("Clips.Engines_jpg", "Sources.Materials.DefaultLib.Material.Image.tex", False)
i' trying to derive the code for "get the materials texture." this should be possible to evaluate from the SDK help. i hope.
connect a texture to a texture map:
Code: Select all
Application.SetValue("sphere.Texture_Map.ImageClipName", "Clips.Engines_jpg", "")
here's what i'm stuck on:
can i use the selection model to tell the script: "get the texture of the selected objects material and apply that to the same objects texture map"?
or do i need to iterate over the selection in some matter?
thank you
Re: get texture script
Posted: 25 Oct 2012, 14:22
by EricTRocks
You're going to have to iterate over the selection:
Code: Select all
for eachObj in Application.Selection:
log(eachObj.FullName) # logs the full name of the object
You'll have to get the material from eachObj while looping over it:
From that material you'll need to get the image clips used in it:
Code: Select all
collImgClips = oMat.ImageClips #returns an xsi collection, you should loop over it maybe?
from that collImgClips you'll be able to loop over that and log each value. If you're 100% sure there is only 1 image clip used then you can just pick the first one on the collection:
Hopefully that info will point you in the right direction. My other advice is to use Application.LogMessage() a lot and see what it prints out.
Re: get texture script
Posted: 25 Oct 2012, 14:28
by caledonian_tartan
you're too kind. give me a second to understand your script.
as the SDK examples aren't very python friendly (unfortunately i had to) switch to VB script.
here's what i have so far:
Code: Select all
set oSelection = Application.Selection
for each oItem in oSelection
set oMaterial = oItem.Material
SetValue oItem.Name & ".Texture_Map.ImageClipName", oMaterial.CurrentImageClip
next
p.s.:my script doesn't work so i will continue with your suggestion and python(!)
Code: Select all
' ERROR : Variable uses an Automation type not supported in VBScript: 'SetValue' - [line 11]
Re: get texture script
Posted: 25 Oct 2012, 14:50
by caledonian_tartan
if i try:
Code: Select all
for eachObj in Application.Selection:
log(eachObj.FullName)
i get
Code: Select all
# NameError: name 'log' is not defined
so another try with VB: the script runs with no error. but nothing happens?!
Code: Select all
set oSelection = Application.Selection
for each oItem in oSelection
set oMaterial = oItem.Material
SetValue oItem.Name & ".Texture_Map.ImageClipName", "oMaterial.CurrentImageClip"
next
Re: get texture script
Posted: 25 Oct 2012, 14:54
by Letterbox
Carry on with what your doing, but there might be a much nicer way.... i'm working on it now, when/if done, I'll post it up.
Re: get texture script
Posted: 25 Oct 2012, 15:09
by caledonian_tartan
hi letterbox
thank you. really looking forward to your solution.
i noticed that my script does indeed change the texture map image. but i changes it to the noIcon_pic...
also it logs:
Code: Select all
SetValue "sphere.Texture_Map.ImageClipName", "oMaterial.CurrentImageClip"
but it should be:
Code: Select all
SetValue "sphere.Texture_Map.ImageClipName", "Clips.Engines_jpg"
if i do
Code: Select all
LogMessage oMaterial.CurrentImageClip
it logs
which is what i want, but in the context of the script it doesn't work.
somehow...
Re: get texture script
Posted: 25 Oct 2012, 15:15
by xsisupport
Here's a hint.
Code: Select all
from siutils import si
si = si() # win32com.client.Dispatch('XSI.Application')
from siutils import log # LogMessage
from siutils import disp # win32com.client.Dispatch
from siutils import C # win32com.client.constants
Application.SelectObj("SomeMeshWithAnImageAndATextureMap", "", "")
o = si.Selection(0)
mat = o.Materials(0)
i = mat.ImageClips(0)
txmap = o.LocalProperties.Filter("TextureProp")(0)
txmap.Parameters("ImageClipName").Value = i.Name
I often start by writing a snippet like this, and then I generalize it (and add error checking).
Re: get texture script
Posted: 25 Oct 2012, 15:31
by caledonian_tartan
hi and thanks steve
how can i do the following in python?
VB example:
Code: Select all
SetValue oItem.Name & ".Texture_Map.ImageClipName", "oMaterial.CurrentImageClip"
(it's the [ & ] i'm interested in...)
i try to adapt your parameters into
Code: Select all
Application.SetValue("sphere.Texture_Map.ImageClipName", "Clips.Engines_jpg", "")
that's my suggestion, but i doesn't seem to work:
Code: Select all
from siutils import si
si = si() # win32com.client.Dispatch('XSI.Application')
from siutils import log # LogMessage
from siutils import disp # win32com.client.Dispatch
from siutils import C # win32com.client.constants
o = si.Selection(0)
mat = o.Materials(0)
i = mat.ImageClips(0)
txmap = o.LocalProperties.Filter("TextureProp")(0)
txmap.Parameters("ImageClipName").Value = i.Name
for eachObj in o:
Application.SetValue "i", "Clips" + i.Name, "")
Re: get texture script
Posted: 25 Oct 2012, 16:00
by xsisupport
Code: Select all
"%s.Texture_Map.ImageClipName" % oItem.Name
Re: get texture script
Posted: 25 Oct 2012, 16:07
by caledonian_tartan
i have it working for the first selected object only:
Code: Select all
from siutils import si
si = si() # win32com.client.Dispatch('XSI.Application')
from siutils import log # LogMessage
from siutils import disp # win32com.client.Dispatch
from siutils import C # win32com.client.constants
o = si.Selection(0)
mat = o.Materials(0)
i = mat.ImageClips(0)
txmap = o.LocalProperties.Filter("TextureProp")(0)
txmap.Parameters("ImageClipName").Value = i.Name
Application.SetValue(txmap.Parameters("ImageClipName"), "Clips." + i.Name, "")
now i should iterate over the selection, which confuses me...
PS.: as you can see i used "+". i assume this is the same as "%"
Re: get texture script
Posted: 25 Oct 2012, 16:19
by xsisupport
caledonian_tartan wrote:i have it working for the first selected object only:
Code: Select all
from siutils import si
si = si() # win32com.client.Dispatch('XSI.Application')
from siutils import log # LogMessage
from siutils import disp # win32com.client.Dispatch
from siutils import C # win32com.client.constants
o = si.Selection(0)
mat = o.Materials(0)
i = mat.ImageClips(0)
txmap = o.LocalProperties.Filter("TextureProp")(0)
txmap.Parameters("ImageClipName").Value = i.Name
Application.SetValue(txmap.Parameters("ImageClipName"), "Clips." + i.Name, "")
now i should iterate over the selection, which confuses me...
PS.: as you can see i used "+". i assume this is the same as "%"
My snippet works on the first object in the selection ("si.Selection(0)", note the "(0)").
If I wanted to turn it into a loop, I would do something like this:
Code: Select all
for o in si.Selection:
mat = o.Materials(0)
i = mat.ImageClips(0)
txmap = o.LocalProperties.Filter("TextureProp")(0)
txmap.Parameters("ImageClipName").Value = i.Name
This crude snippet works on the first material, the first image clip, and first texturemap property found.
My last line is setting the parameter value, but through the OM instead of with SetValue.
ciao
Re: get texture script
Posted: 25 Oct 2012, 17:03
by caledonian_tartan
whatever i do the loop doesn't work:
in eric's version it logs 3 objects, but only one material/texture/clip.
Code: Select all
from siutils import si
si = si() # win32com.client.Dispatch('XSI.Application')
from siutils import log # LogMessage
from siutils import disp # win32com.client.Dispatch
from siutils import C # win32com.client.constants
for eachObj in Application.Selection:
log(eachObj.FullName)
oMat = eachObj.Material
collImgClips = oMat.ImageClips
oImgClip = collImgClips(0)
print oMat
print collImgClips
print oImgClip
steve's version is understandable but even with the suggested "for o in si.Selection:" it sets all texture maps to noIcon_pic
Code: Select all
from siutils import si
si = si() # win32com.client.Dispatch('XSI.Application')
from siutils import log # LogMessage
from siutils import disp # win32com.client.Dispatch
from siutils import C # win32com.client.constants
for o in si.Selection:
mat = o.Materials(0)
i = mat.ImageClips(0)
txmap = o.LocalProperties.Filter("TextureProp")(0)
txmap.Parameters("ImageClipName").Value = i.Name
#Application.SetValue(txmap.Parameters("ImageClipName"), "Clips." + i.Name, "")
print txmap.Parameters("ImageClipName").Value
Re: get texture script
Posted: 25 Oct 2012, 17:17
by caledonian_tartan
OK. It's done!
wouldn't have been possible without you guys, thanks a lot:
Code: Select all
from siutils import si
si = si() # win32com.client.Dispatch('XSI.Application')
from siutils import log # LogMessage
from siutils import disp # win32com.client.Dispatch
from siutils import C # win32com.client.constants
for o in si.Selection:
mat = o.Materials(0)
i = mat.ImageClips(0)
txmap = o.LocalProperties.Filter("TextureProp")(0)
txmap.Parameters("ImageClipName").Value = "Clips." + i.Name
# log all object names
for obj in Application.Selection :
Application.LogMessage( obj.name )
# log object count
oObjectCount = len(Application.Selection)
Application.LogMessage("%s Objects edited" %(oObjectCount))
i just had to change
i.Name
to
"Clips." + i.Name
again, thx
Re: get texture script
Posted: 26 Oct 2012, 00:21
by EricTRocks
Glad you finally got it working. Also a word of advice, don't use VBScript. No one I know uses it any more. Python is the way to go so stick to that in the future.