Is it possible to save shadows as image?


#1

Is it possible to do the following with Sketchup Ruby API?

  1. Create plane (the plane is created on a mountains mesh).
  2. Set Shadows hour.
  3. Project the shadows onto the surface of the plane
  4. Save the shadows on the plane as image
    I would like to get white plane covered with gray shadows.

I ask because I would like to do it many times for different hours and different planes.

Don’t ask me why I want this. I would like to try some fancy things with masks… This is for image editing program.


#3

Export with Ruby? But how to do the stuff with Shadows settings?

Ok, I add here the code needed to export the image, at least:

view = Sketchup.active_model.active_view
status = view.write_image "test.jpg"
keys = {
  :filename => "c:/tmp/write_image.png",
  :width => 640,
  :height => 480,
  :antialias => false,
  :compression => 9.0,
  :transparent => true
}
view = Sketchup.active_model.active_view
view.write_image keys

#5

you can change shadowOptions = model.shadow_info.to_a

but if you want a shadow catcher @TIG has a plugin that creates geometry from them…

then you can set there materials. etc…

john


#6

I can try to write some basic plugin. But how do you clear console?


#7

tricky one…

 SKETCHUP_CONSOLE.clear

you can also use the up arrow to bring up old versions to modify and try again…

even after clear…

john


#8

Ah, but not in SU8


#9

Here is my first part of the plugin so far.

### http://ruby.sketchup.com/Sketchup/ShadowInfo.html

def barr_shadows
  Sketchup.active_model.start_operation "Barracuda Shadows"
	si = Sketchup.active_model.shadow_info  
  si["Light"] = 85;
  si["Dark"] = 45;
  ### value = si["ShadowTime"]=Time.local(2017, 7, 21, 14);
  value = si["ShadowTime"]=Time.gm(2017, 7, 21, 14, 00);
  si["TZOffset"] = -1.0;
  UI.messagebox value
  si["EdgesCastShadows"] = false;
  si["DisplayShadows"] = true;
  
  Sketchup.active_model.commit_operation
	return
end

barracuda_shadows = UI.menu("Plugins")
if not (file_loaded? "shadows.rb")
	barracuda_shadows.add_item("Barracuda Shadows") {barr_shadows}
end

Everything except the hour of the day works. Hover is 12:00 instead of 14:00. Also the shadows seems to be cca 4x longer then normal.


#10

get a mac and v17 and then my answer will be correct… [sometimes]

I wrote a plugin that gives me all the ruby calls for changing the settings…

I have no idea if it can work on such a SU version running on XP…

maybe you can adapt it?

john


#11

for clearing the console look >>>HERE<<<

john


#12

I update the code and used #gm so I got correct time now. But the shadows are still longer. It could be because I am using UTC-1:00 instead UTC+1:00. The UTC+1:00 does not show shadows: This is not clear why it happens. I think I will measure real shadow tomorrow to see what is happening.


#13

Is it possible to save the plane with textures projected on it’s surface? The reason why I would prefer to do it is that this would skip me the need to use raster editor to remove mesh.


#14

it’s possible because both @TIG and @kirill200777 [maybe more] have done it in their extensions…

but I never have, so look at their code if possible and ask them questions if you get stuck…

john


#15

I have found only Elev45shadows.rb from @TIG but AFAIK this does not include the functionality to generate texture or to make the shadow part of the texture.


#16

I know of nothing that will make a shadow part of a texture on a face…
This would be very complex to achieve - but for what end ?

The tool I made makes a separate face with a ‘gray’ transparent texture for each shadow cast…
It’s not intended to merge textures etc !

https://sketchucation.com/pluginstore?pln=TIG_shadowProjector


#17

TIG-shadowProjector is the one I was thinking of…

john

TIG beat me to it…


#18

The purpose was to get the image with dimensions of the plane and shadows on it. It would save me cropping of the image which I need to export.


#19

I’m sorry, but your explanations is still not entirely clear…
Can you try and explain your needs in simple steps…
I fear there is not a simple solution.
But until we have clarity, then who knows ?


#20

I will write code tomorrow and show you what it does. Just wait till it be ready.


#21

OK I measured the shadow and I corrected the UTC, set geolocation anow it it pretty close, but not exact.

value = shadowinfo["ShadowTime"]=Time.gm(2017, 7, 19, 12, 03);
shadowinfo["TZOffset"] = +1.0;
shadowinfo["Latitude"] = lat
shadowinfo["Longitude"] = lon

Eg. For UTC+1:00 (Middle Europe, Lat 49.779147, Lon 18.439736) The correct shadow length is 655:1160.
The shadows are 0,564 times longer (akka 2x shorter)

In SU8 I got the ratio
0,55950266429840142095914742451155
So there is cca -0,005 mistake.


#22

Final code

def centerCamera(perspective = false, zoomOut = 150)
  cam = Sketchup.active_model.active_view.camera;
  cam.perspective = perspective;
  Sketchup.send_action('viewTop:');  
  Sketchup.send_action('viewZoomToSelection:');
end;

def barr_shadows( ground=false )
  Sketchup.active_model.start_operation "Barracuda Shadows"
	si = Sketchup.active_model.shadow_info  
  si["Light"] = 85;
  si["Dark"] = 45;
  ### value = si["ShadowTime"]=Time.local(2017, 7, 21, 14);
  value = si["ShadowTime"]=Time.gm(2017, 7, 19, 12, 03);
  ### UI.messagebox value
  si["TZOffset"] = +1.0;
  si["Latitude"] = 40.779147
  si["Longitude"] = 28.439736
  si["EdgesCastShadows"] = false;
  si["DisplayOnGroundPlane"] = ground;  
  si["DisplayShadows"] = true;
  
  Sketchup.active_model.commit_operation
	return
end

def write_img(width=800, height=600)
  keys = {
    :filename => "U:/tmp/01.png",
    :width => width,
    :height => height,
    :antialias => false,
    :compression => 0.9,
    :transparent => true
  }
  view = Sketchup.active_model.active_view
  view.write_image keys
end
barracuda_shadows = UI.menu("Plugins")
if not (file_loaded? "shadows.rb")
	barracuda_shadows.add_item("Barracuda Shadows") { barr_shadows; centerCamera(); }
end

However - would it be possible to crop the image?